Add Attachments via python / service

2228
11
02-04-2020 12:39 PM
jaykapalczynski
Frequent Contributor

I am trying to research how to add attachments via Python.

I see something about Generating an Attachment Match Table and then Add Attachment.

I eventually want to run this python script via a service

Can I pass the service the local parameters to the image, identify the gdb etc from a mobile environment??? 

Where does this Match table come into play?  Would it reside on the mobile device?  PC?  Server?

I am not sure how this all plays together?

0 Kudos
11 Replies
AnttiKajanus
New Contributor III

I would tackle this type of use case with using Feature Service Attachments functionality. Ie. creating a Feature Class "UserImages" to a gdb, publish a Feature Service with attachments enabled out and then using the out of the box tools/apis to handle the attachments. 

Depending what kind of application you are building you are using, I would either use the APIs or REST-endpoint directly to add the attachment to the service. If you need to do some post-processing in between then using GP would provide one way to go. 

In this workflow you could store the image to the phone as you want and then upload it to a existing feature or most likely to create a new point (the location of the picture) and then add the attachment to that feature. The end result would be that you a new feature in your geodatabase with a location and the image. This way you can also add any other attributes to the features as well.

If you need more information on this flow, let me know and I can walk it through in more detail.

0 Kudos
by Anonymous User
Not applicable

Hi,

If you have a portal you can use arcgis python API to do it, sample here Layer Attachments | ArcGIS for Developers .  Also if you do not have access to portal you can use anything like this following code snippet to do this.


import os 
import requests 

url = "https://yourfeatureserviceurl" 
object_id = 1 
attachment = r"13291732970228.jpg"

files = {'attachment': (os.path.basename(attachment), open(attachment, 'rb'), 'application/jpeg')} 
params = { "f": "json", "token": "" }

r = requests.post( f"{url}/{object_id}/addAttachment", data=params, files=files ) 
r.json()


Sucessfull request will have this type of response

{'addAttachmentResult': {'objectId': 7, 'globalId': None, 'success': True}}
0 Kudos