How to extract feature service data with attachments enabled from ArcGIS Online

There may be a need to make a local copy of attachments stored in a hosted feature service within an ArcGIS Online organisation account, for instance for backup purposes. The instructions below will guide you through the steps of how to do this.

Create a replica

Log in to your AGOL account, go to 'My Content' and browse to the attachment enabled Feature Service you need to extract from.

For small datasets:

1. From the description page of the hosted feature service with attachments enabled, follow the 'Service URL'.

replica01.png

2. Click on the 'FeatureServer' link at the top (under ArcGIS REST Services Directory).

replica02.png

3. Select the 'Create Replica' operation (bottom of the page).

replica03.png

4. Give the replica a name with no spaces.

5. Give 'Layers' a value of 0 (zero)

6. Set

    a) Return Attachments to 'True'

    b) Sync Model to 'none'

    c) Data Format to 'FILEGDB'

replica04.png

7. Click 'Create Replica'

8. Follow the Response URL to download the .zip file

replica05.png

9. Use 7-Zip to extract the content (The 'Extract...' command built in to Windows won`t work!)

10. Add the .gdb extension to the folder name

For large datasets:

Follow steps 1-5 as shown above.

6. Set

    a) Return Attachments to 'True'

    b) Create Replica Asynchronously to 'True'

    c) Sync Model to 'none'

    d) Data Format to 'FILEGDB'

replica06.png

7. Click 'Create Replica'

8. Follow the Response URL to download the .zip file

9. Use 7-Zip to extract the content

10. Add the .gdb extension to the folder name

Extract attachments into a folder

At the moment, there is no out-of the box tool in ArcGIS Desktop to extract attachments; however, you can perform this function through the use of a python script. You need to use the arcpy.da (data access) module to identify and iterate through the attachments table using a Search Cursor and save the blob objects, that store the images, into physical files. (The arcpy.da module is only available in v10.1 and 10.2!)

Disclaimer: The below script is provided as-is and Esri UK accepts no responsibility of issues that arise from incorrect usage of this code.

from arcpy import da

import os

tbl = r"C:\TestProject\Attachments\test.gdb\yourFCname__ATTACH"

fldBLOB = 'DATA'

fldAttName = 'ATT_NAME'

outFolder = r"C:\TestProject\Attachments\files"

with da.SearchCursor(tbl,[fldBLOB,fldAttName]) as cursor:

   for row in cursor:

      binaryRep = row[0]

      fileName = row[1]

      # save to disk

      open(outFolder + os.sep + fileName, 'wb').write(binaryRep.tobytes())

      del row, binaryRep, filename

Now you have a copy of your data and attachments to use locally, or for backup purposes.

Note: You can extract the attachments using ArcObjects too, please see the API reference for more information.