Esri / arcgis-python-api

Documentation and samples for ArcGIS API for Python
https://developers.arcgis.com/python/
Apache License 2.0
1.88k stars 1.1k forks source link

Attachment Manager in ArcGIS Online Notebooks cannot add DOCX or XLSX Files (Still) #1412

Closed sirws closed 1 year ago

sirws commented 1 year ago

Describe the bug It appears that the notebooks hosted in ArcGIS Online (v. 2.0.1) do not allow you to attach .xlsx files or .docx files. This is a rehash of this closed issue: https://github.com/Esri/arcgis-python-api/issues/1315

To Reproduce Upload an xlsx file or docx file to the files area in ArcGIS Online (/arcgis/home). Try to attach the file to a feature service.

attachresult = attach_mgr_target.add(oid=1,file_path=r"/arcgis/home/test.xlsx")

error:

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 attachresult = attach_mgr_target.add(oid=1,file_path=r"/arcgis/home/test.xlsx")

File /opt/conda/lib/python3.9/site-packages/arcgis/features/managers.py:522, in AttachmentManager.add(self, oid, file_path, keywords)
    503 def add(self, oid: str, file_path: str, keywords: Optional[str] = None):
    504     """
    505     Adds an attachment to a :class:`~arcgis.features.FeatureLayer`
    506 
   (...)
    520 
    521     """
--> 522     return self._layer._add_attachment(oid, file_path, keywords=keywords)

File /opt/conda/lib/python3.9/site-packages/arcgis/features/layer.py:539, in FeatureLayer._add_attachment(self, oid, file_path, keywords)
    537         attach_url = self._url + "/%s/addAttachment" % oid
    538     files = {"attachment": file_path}
--> 539     res = self._con.post(path=attach_url, postdata=params, files=files)
    540     return res
    541 else:

File /opt/conda/lib/python3.9/site-packages/arcgis/gis/_impl/_con/_connection.py:1407, in Connection.post(self, path, params, files, **kwargs)
   1405 if return_raw_response:
   1406     return resp
-> 1407 return self._handle_response(
   1408     resp=resp,
   1409     out_path=out_path,
   1410     file_name=file_name,
   1411     try_json=try_json,
   1412     force_bytes=kwargs.pop("force_bytes", False),
   1413 )

File /opt/conda/lib/python3.9/site-packages/arcgis/gis/_impl/_con/_connection.py:900, in Connection._handle_response(self, resp, file_name, out_path, try_json, force_bytes, ignore_error_key)
    898             return data
    899         errorcode = data["error"]["code"] if "code" in data["error"] else 0
--> 900         self._handle_json_error(data["error"], errorcode)
    901     return data
    902 else:

File /opt/conda/lib/python3.9/site-packages/arcgis/gis/_impl/_con/_connection.py:923, in Connection._handle_json_error(self, error, errorcode)
    920                 # _log.error(errordetail)
    922 errormessage = errormessage + "\n(Error Code: " + str(errorcode) + ")"
--> 923 raise Exception(errormessage)

Exception: The given key was not present in the dictionary.
(Error Code: 400)

Screenshots image

Expected behavior Based on the UI in ArcGIS Online, you should be able to attach .xlsx or .docx files. When you attach via the web UI, it uses this mime type for xlsx files: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Platform (please complete the following information):

Additional context In looking at the mimetypes in ArcGIS Online hosted notebooks, I do not see a .xlsx or .docx in there. If I run the following code, it allows me to attach the files.


import mimetypes
mimetypes.types_map['.xlsx'] = r"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
attachresult = attach_mgr_target.add(oid=1,file_path=r"/arcgis/home/test.xlsx")
sirws commented 1 year ago

ArcGIS Online uses the following Content-Type for docx: Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document


mimetypes.types_map['.docx'] = r"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
nanaeaubry commented 1 year ago

This is being looked at by the Notebooks team and we will update with any progress or information.

nanaeaubry commented 1 year ago

@sirws We were able to figure out what was happening. We created a fix to this 9 months ago and it was integrated into 2.1.0 of the python API. However, Online runs version 2.0.1 and it is not set to update until mid 2023.

A parameter that was not required before became required and that is why you are seeing the error. The best workaround until the update is made is to use the Python API 2.1.0 version in a local notebook or script.