HOW TO

Calculate the total size of attachments in a hosted feature layer in ArcGIS Online or Portal for ArcGIS

Last Published: January 18, 2024

Summary

Attachments can be added to a layer in ArcGIS Pro, as described in the following documentation, ArcGIS Pro: Add or remove file attachments, before publishing to ArcGIS Online or Portal for ArcGIS. When a layer contains multiple attachments, it can be tedious to manually calculate the total size of attachments. In addition, the total size may vary considering the different sizes when in disk, during publishing, and the conversion from bytes to kilobytes. This article provides the steps to use ArcGIS API for Python to calculate the total size in a stand-alone Python script or ArcGIS Notebooks.

Procedure

  1. Import the necessary modules.
import arcgis
from arcgis import GIS
import pandas as pd
  1. Connect to ArcGIS Online or Portal for ArcGIS.
#For ArcGIS Online
gis = GIS('https://arcgis.com', 'username', 'password')
print("Connected")

#For Portal for ArcGIS
gis = GIS('https://<machine>.<domain>/<web adaptor name>', 'username', 'password', verify_cert=False)
print("Connected")
  1. Get the desired layer from the hosted feature layer ID.
item = '<item_ID>'
layer = gis.content.get(item)
ids = layer.layers[0]
fs = ids.query(where="1=1")
oids = [f.attributes["OBJECTID"] for f in fs.features]
Note:
Edit the index value in 'ids = item.layers[0]' based on the layers that hold the attachments. For example, to calculate the total attachment size in the second layer, replace 0 with 1 in the code.
  1. Print all the attributes with attachments.
attached = [ids.attachments.get_list(oid=oid) for oid in oids]
print(attached)
  1. Calculate the total size of attachments and display them in the result console.
print("Total size: " + str(ids.attachments.search(as_df=True)['SIZE'].sum()))

#If additional word is unnecessary, the following line can be used:
print(ids.attachments.search(as_df=True)['SIZE'].sum())

The following is a working sample script and the result console. The calculated file size is in bytes.

import arcgis
from arcgis import GIS
import pandas as pd

gis = GIS('https://arcgis.com', 'user1', 'password1!')
print("Connected")

item = '80d00e2301844ou2435p9284fu87bf'
layer = gis.content.get(item)
ids = layer.layers[0]
fs = ids.query(where="1=1")
oids = [f.attributes["OBJECTID"] for f in fs.features]

attached = [ids.attachments.get_list(oid=oid) for oid in oids]
print (attached)

print("Total size: " + str(ids.attachments.search(as_df=True)['SIZE'].sum()))
The result console displaying the total size of attachments

Article ID:000031782

Software:
  • ArcGIS Online
  • ArcGIS Enterprise 11 2
  • ArcGIS Enterprise 11 1
  • ArcGIS Enterprise 11 0
  • Portal for ArcGIS

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options