Select to view content in your preferred language

Exporting attachments (photos) from AGOL

729
2
02-13-2019 11:48 AM
DaveStewart1
Regular Contributor

Hello all,

I'm currently working through how to export attachments from a layer on AGOL. There is only one thing I am looking for: How to make the file names match that of IDs from a field. Basically I have a list of IDs in a field called {SeasonalMeter}. I want the file names to mirror these IDs. Currently my file names are ATT###_attachment1.jpg where I'd like them to be like this for example: P70344613-H_attachment1 (P70344613-H is a value in my SeasonalMeter field column). The end goal is to bulk dump these attachment into our existing system based on these IDs.

I've used the following python code and instructions from the following:

https://support.esri.com/en/technical-article/000011912

import arcpy
from arcpy import da
import os

inTable = arcpy.GetParameterAsText(0)
fileLocation = arcpy.GetParameterAsText(1)

with da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID']) as cursor:
    for item in cursor:
        attachment = item[0]
        filenum = "ATT" + str(item[2]) + "_"
        filename = filenum + str(item[1])
        open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())
        del item
        del filenum
        del filename
        del attachment

I am not a programmer/ developer and do not know python. I assume that where it shows filename = filenum + str(item[1]) I would need to replace it with something to the affect -> filename = {SeasonalMeter}

I'm not familiar with coding syntax that would make this happen.

I will continue researching but I know this forum has always been a great help!

Thank you.

Tags (1)
0 Kudos
2 Replies
RandyBurton
MVP Alum
0 Kudos
DaveStewart1
Regular Contributor

Thanks Randy!

I ended up using this python code:

import arcpy
from arcpy import da
import os

inTable = arcpy.GetParameterAsText(0)
fileLocation = arcpy.GetParameterAsText(1)

with da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID', 'SeasonalMeter2']) as cursor:
    for item in cursor:
        attachment = item[0]
        filenum = "ATT" + str(item[2]) + "_"
        filename = filenum + str(item[3])
        open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())
        del item
        del filenum
        del filename
        del attachment

Just a minor tweak. One thing I had to do was join the Seasonal_Meters__ATTACH table to the data that has the actual field I wanted to include. Then I added a new field to the table, used field calculator to transfer values over (I was having a hard time editing the source), removed the join and ran the script. Another thing, when I exported the files they did not have file extensions. This was easily remedied by another bat file:

@echo off

ren * *.jpeg

I'm sure you could include that in the original python code as well.

0 Kudos