Select to view content in your preferred language

Using AttachmentManager to Download Attachments Saves in Separate Folders

4474
8
Jump to solution
05-26-2021 10:52 AM
cschooley
Occasional Contributor

The issue I'm having is with the AttachmentManager module. I wrote a quick code to download all of the attachments for a feature layer stored on ArcGIS online. 

 

############# Get logged in and set up ###############

from arcgis.gis import GIS
PortalUrl = "https://www.arcgis.com"
username = ""
password = ""

FeatureLayerID = "e5c2bdc5755e4e21ab578550f2fa12a3"

SavePath= r"C:\Users\cschooley\Documents\Work\Heritage\Photos"

################# End Set-up #######################

if username == "" and password == "":
    gis = GIS()
else:
    gis = GIS(PortalUrl, username, password)

FeatureLayer = gis.content.get(FeatureLayerID)

layer = FeatureLayer.layers[0]

layer.attachments.download(oid = 1, save_path=SavePath)

 

It works just fine, but the issue I am having is that when it saves into the path I created, it creates a new folder for the ObjectID of that feature and the AttachmentID of that feature, nesting my photos behind two of their own folders. 

 

['C:\\Users\\cschooley\\Documents\\Work\\Heritage\\Photos\\1\\18\\2015-04-11 10.35.42.jpg']

 

I am wondering if there is a way to work around this. I would like to have the photos save directly into the 'photos' folder without the creation of the other folders. 

I suppose using the os module could work, but that's a little over my head. I found this  article which has been kind of helpful, but I couldn't get his code to work for me, which is why I wrote this one above. It was simpler and didn't have the extra stuff I didn't need. I only need to download the photos, not necessarily have all the information (ObjectIDs, etc.) attached to them. 

Thank you. 

 

P.S. The feature ID used in the code is a public one, not the one I'm actually trying to use since that one is protected within my organization. It's from the JoshuaTreeHike feature server. It's the same set-up as mine though, where the Attachment IDs don't necessarily match the ObjectIDs. 

 

0 Kudos
1 Solution

Accepted Solutions
FrédéricPRALLY
Esri Contributor

Hi cshooley,

Maybe you can used this workaround with os and shutil module to copy jpg after downloaded at Photos dirname as below :

 

############# Get logged in and set up ###############
import os
import shutil
from arcgis.gis import GIS
PortalUrl = "https://www.arcgis.com"
username = ""
password = ""
FeatureLayerID = "e5c2bdc5755e4e21ab578550f2fa12a3"
SavePath= r"C:\Users\cschooley\Documents\Work\Heritage\Photos"

################# End Set-up #######################
if username == "" and password == "":
    gis = GIS()
else:
    gis = GIS(PortalUrl, username, password)
FeatureLayer = gis.content.get(FeatureLayerID)
layer = FeatureLayer.layers[0]
layer.attachments.download(oid = 1, save_path=SavePath)

################# Copy image #######################
for root, dirs, files in os.walk(SavePath):
    for f in files:
        if f.endswith('.jpg'):
            print("Copy jpg: {0}".format(str(f)))
            pathOriginal = os.path.join(root,f)
            pathTarget = os.path.join(SavePath,f)
            shutil.copyfile(pathOriginal, pathTarget)

### remove folders into Photos if you want
##for d in os.listdir(SavePath):
##    if os.path.isdir(os.path.join(SavePath,d)):
##        shutil.rmtree(os.path.join(SavePath,d))

 

Hope this help you,

Best regards,

Fred

View solution in original post

8 Replies
FrédéricPRALLY
Esri Contributor

Hi cshooley,

Maybe you can used this workaround with os and shutil module to copy jpg after downloaded at Photos dirname as below :

 

############# Get logged in and set up ###############
import os
import shutil
from arcgis.gis import GIS
PortalUrl = "https://www.arcgis.com"
username = ""
password = ""
FeatureLayerID = "e5c2bdc5755e4e21ab578550f2fa12a3"
SavePath= r"C:\Users\cschooley\Documents\Work\Heritage\Photos"

################# End Set-up #######################
if username == "" and password == "":
    gis = GIS()
else:
    gis = GIS(PortalUrl, username, password)
FeatureLayer = gis.content.get(FeatureLayerID)
layer = FeatureLayer.layers[0]
layer.attachments.download(oid = 1, save_path=SavePath)

################# Copy image #######################
for root, dirs, files in os.walk(SavePath):
    for f in files:
        if f.endswith('.jpg'):
            print("Copy jpg: {0}".format(str(f)))
            pathOriginal = os.path.join(root,f)
            pathTarget = os.path.join(SavePath,f)
            shutil.copyfile(pathOriginal, pathTarget)

### remove folders into Photos if you want
##for d in os.listdir(SavePath):
##    if os.path.isdir(os.path.join(SavePath,d)):
##        shutil.rmtree(os.path.join(SavePath,d))

 

Hope this help you,

Best regards,

Fred

cschooley
Occasional Contributor

Thank you! That worked great. I had figured out how to get a list of all my files with this code:

list_dir = []
for root, dirs, files, in os.walk(SavePath):
    for file in files:
        list_dir.append(file)

But couldn't get any further than that. Now I think I can use your

for root, dirs, files in os.walk(SavePath):
    for f in files:
        if f.endswith('.jpg'):
            print(r"Copy jpg: {0}")
            PathOriginal = os.path.join(root,f)
            PathTarget = os.path.join(SavePath, f)
            shutil.copyfile(PathOriginal, PathTarget)

To get all the files, not just jpegs. Something like this:

for root, dirs, files in os.walk(SavePath):
    for f in files:
       print("Copy jpg: {0}".format(str(f))
       PathOriginal = os.path.join(root,f)
       PathTarget = os.path.join(SavePath, f)
       shutil.copyfile(PathOriginal, PathTarget)

 

Thanks again!

0 Kudos
cschooley
Occasional Contributor

A secondary issue, and I don't know if you can help me here, but if some photos have the same name, there should be a way to rename the doubles right? So that they don't overwrite each other?

0 Kudos
FrédéricPRALLY
Esri Contributor

Best way is to check if jpg name exist before copy it.

See https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/checking-for-existence.htm

0 Kudos
cschooley
Occasional Contributor

Right. So, that checks if something exists, but doesn't help with the issue of the shutil.copy overwriting if something exists. I'm downloading attachments from a feature layer where the attachments on different layers have the same name, but aren't the same picture and I want all of the pictures. 

From my reading I need to add something kind of like this to rename them before they save:

 

while os.path.exists(f + "_" + str(i)):
     i+=i

 

Not that exactly of course, but to add a number or something on the end of it so it doesn't overwrite the previous image. 

0 Kudos
FrédéricPRALLY
Esri Contributor

Hi,

Try complete script below:

############# Get logged in and set up ###############
import os
import shutil
from arcgis.gis import GIS
PortalUrl = "https://www.arcgis.com"
username = ""
password = ""
FeatureLayerID = "e5c2bdc5755e4e21ab578550f2fa12a3"
SavePath= r"C:\Users\cschooley\Documents\Work\Heritage\Photos"

################# End Set-up #######################
if username == "" and password == "":
    gis = GIS()
else:
    gis = GIS(PortalUrl, username, password)
FeatureLayer = gis.content.get(FeatureLayerID)
layer = FeatureLayer.layers[0]
layer.attachments.download(oid = 1, save_path=SavePath)

################# Copy image #######################
for root, dirs, files in os.walk(SavePath):
    for f in files:
        if f.endswith('.jpg'):
            print("Copy jpg: {0}".format(str(f)))
            pathOriginal = os.path.join(root,f)
            pathTarget = os.path.join(SavePath,f)
            
            if os.path.exists(pathTarget):
                pathTarget = pathTarget[:-4] + "_copy" + pathTarget[-4:]
                
            shutil.copyfile(pathOriginal, pathTarget)

### remove folders into Photos if you want
for d in os.listdir(SavePath):
    if os.path.isdir(os.path.join(SavePath,d)):
        shutil.rmtree(os.path.join(SavePath,d))

Hope this help you,

Regards,

Fred

cschooley
Occasional Contributor

Thank you.  I was able to go through this one and make one that worked out for me. 

 

0 Kudos
UriGilad_EsriAu
Esri Contributor

Hi @cschooley 

Have a look at this tool I created - might be helpful:

https://community.esri.com/t5/arcgis-pro-blog/export-attachments-toolbox/ba-p/1022018

If this answer solved your question or if you found it helpful please mark it accordingly to help others who have the same question.
0 Kudos