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

1228
7
05-14-2018 10:39 AM
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)
1 Solution

Accepted Solutions
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

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

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'}
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)

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.

7 Replies
Esri Notable 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>'
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
print(addItemResponse)‍‍‍‍‍‍‍‍‍‍‍

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

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

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

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'}
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)

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.

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?

MVP Esteemed Contributor
 Argument Description url Optional 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: :/// (Portal Example)https://gis.example.com/portal
New Contributor III

I want to publish 30 pdfs to Arcgis Online, I use the same code but i want to modifiy the propreties like this :

Name of one of my pdfs : Map_community_A3_TEST_61001

Template :  ([DOCUMENT_TYPE]_[OBJECT]_[FORMAT]_[NAME]_[CODE])

I want to modifiy pdf_propreties from

pdf_propreties = {'type':'PDF', 'description':'PDF upload for map viewer','title':pdf_name, 'tags':'OPENDATA', 'access':'Public'}

To :

pdf_propreties = {'type':'PDF', 'description':'DOCUMENT_TYPE, FORMAT, NAME','title':NAME, 'tags':'DOCUMENT_TYPE, FORMAT, NAME, CODE', 'access':'Public'}

Do u think there's a way to do it ?

Thank you 🙂

New Contributor II

Thanks for posting - this will be helpful!

New Contributor III

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