RuntimeError when trying to download attachments using Python API

515
1
Jump to solution
01-27-2022 09:08 AM
Manu
by
Occasional Contributor

I tried downloading attachments from an ArcGIS Online Feature Layer using the following code from within the Python Terminal in ArcGIS Pro:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 20 16:34:30 2022

@author: manuel
"""import os

## ArcGIS log-in
import arcgis, math
from arcgis.gis import GIS
gis = GIS(None, "*******", "*******", verify_cert = False)
ArcGIS_content = "My_survey"

## get ArcGIS Online attachments
search_result = gis.content.search(ArcGIS_content, "Feature Layer")
item = search_result[0]
FeatureLayer = item.layers[0]
FeatureObjectIDs = FeatureLayer.query(where = "1=1", return_ids_only = True)

tmp_dir ="C:\\Users\\Manuel\\Desktop\\test\\"
for objID in FeatureObjectIDs["objectIds"]:
    attachments_list = FeatureLayer.attachments.get_list(oid = objID)
    if len(attachments_list) > 0:
        for attmID in range(1, len(attachments_list) + 1):
           obj_dir = os.path.join(tmp_dir, str(objID))
           os.makedirs(obj_dir)
           FeatureLayer.attachments.download(oid = objID,\
               attachment_id = attmID,\
               save_path = obj_dir)

However, it gave me the following error after creating the first folder with attachments and subsequently creating the second folder:

---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
In [94]:
Line 62: save_path = obj_dir)

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\features\managers.py, in download:
Line 355: raise RuntimeError

RuntimeError:
---------------------------------------------------------------------------

I am not sure what to do with this error. The script creates one folder, downloads the images I want, then creates the next folder and returns this error. Doesn't make much sense to me. Is it about some connection timeout? How can I solve this issue?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Manu
by
Occasional Contributor

I found the attachment IDs are not just consecutive integers, but I had to use something like

attachment_IDs = [a["id"] for a in attachments_list]

View solution in original post

1 Reply
Manu
by
Occasional Contributor

I found the attachment IDs are not just consecutive integers, but I had to use something like

attachment_IDs = [a["id"] for a in attachments_list]