Add 2k+ pdf's to specific folder in my content

275
5
Jump to solution
05-14-2018 10:39 AM
Highlighted
Regular Contributor

I need a method to upload roughly 2,000 PDF's into a specific folder in my content (and share to public)

All PDF's will be inside the same directory on a network drive.

I assume python can handle this but not sure, I see I can upload a geodata (shapefile, layer, map etc.) but not positive about images/PDF's.

Tags (3)
Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Regular Contributor

I was able to do this utilizing the Python API, the code is below for anyone else who may need this in the future

import arcgis
from arcgis.gis import *
import os
import glob
import time

gis = GIS(None, "username", "password")
contentManager = arcgis.gis.ContentManager(gis)
contentManager.create_folder('Folder Name',None)
pdf_list = glob.glob(r'C:\path\to\folder\*.pdf')
additem = arcgis.gis.ContentManager(gis)


for pdf in pdf_list:
    file_name = os.path.basename(pdf)
    pdf_properties = {'type':'PDF','title':file_name.split(".")[0],'tags':'pdf, arcgis,', 'access':'Public'}
    additem.add(pdf_properties,pdf,folder='Historic PDFs')
    time.sleep(1)


item_search = gis.content.search(query='', item_type='PDF')
item_search


for item in item_search:
    item.share(everyone=True)
    time.sleep(1)

I had to use the

time.sleep(1)

otherwise updating the share properties would skip some of the files, I am guessing the calls were being sent to fast for the server to pick up.

View solution in original post

Reply
0 Kudos
5 Replies
Highlighted
Esri Frequent Contributor

The easiest way to upload files is the requests module:

Post a multi-part encoded file

It's far easier than the urllib2 modules:

Make an http POST request to upload a file using python urllib/urllib2 - Stack Overflow

What I typically do is run through the operation through your browser first and monitor the network traffic. After that, recreate the request in Python.

Something like this would likely work:

addItemURL = 'https://<portal>.<domain>.com/portal/sharing/rest/content/users/<user>/addItem'
item = '<path to item>'
uploadFile = open(item,"rb")
theFile = {'file':uploadFile}
data = dict(token=<token>,type=.....) #not sure about what parameters are used for uploading a PDF. My suggestion above will come in handy here)
r = requests.post(addItemURL, params=data, files=theFile, verify=False)‍‍‍‍‍‍‍‍‍‍‍‍ #verify=False ignores certificate errors
addItemResponse = json.loads(r.text)
print(addItemResponse)‍‍‍

Once you upload an individual item, just loop through your directory using os.walk or os.listdir.

15.1. os — Miscellaneous operating system interfaces — Python 2.7.15 documentation 

Highlighted
Regular Contributor

I was able to do this utilizing the Python API, the code is below for anyone else who may need this in the future

import arcgis
from arcgis.gis import *
import os
import glob
import time

gis = GIS(None, "username", "password")
contentManager = arcgis.gis.ContentManager(gis)
contentManager.create_folder('Folder Name',None)
pdf_list = glob.glob(r'C:\path\to\folder\*.pdf')
additem = arcgis.gis.ContentManager(gis)


for pdf in pdf_list:
    file_name = os.path.basename(pdf)
    pdf_properties = {'type':'PDF','title':file_name.split(".")[0],'tags':'pdf, arcgis,', 'access':'Public'}
    additem.add(pdf_properties,pdf,folder='Historic PDFs')
    time.sleep(1)


item_search = gis.content.search(query='', item_type='PDF')
item_search


for item in item_search:
    item.share(everyone=True)
    time.sleep(1)

I had to use the

time.sleep(1)

otherwise updating the share properties would skip some of the files, I am guessing the calls were being sent to fast for the server to pick up.

View solution in original post

Reply
0 Kudos
Highlighted
New Contributor III

This appears to be just for AGO, do you know how I could modify the gis=GIS() connector string for ArcGIS Enterprise Portal? 

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

See arcgis.gis module - GIS — arcgis 1.5.0 documentation:

ArgumentDescription
urlOptional string. If URL is None, then the URL will be ArcGIS Online. This should be a web address to either a local Portal or to ArcGIS Online in the form: <scheme>://<fully_qualified_domain_name>/<web_adaptor> (Portal Example)https://gis.example.com/portal
Highlighted
New Contributor II

Very useful information… I am trying to do the same but instead of uploading, I am trying to update the PDF files that inside a specific folder. Can someone point me to the right direction?

Reply
0 Kudos