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

1896
7
Jump to solution
05-14-2018 10:39 AM
Lake_Worth_BeachAdmin
Occasional Contributor III

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)
0 Kudos
1 Solution

Accepted Solutions
Lake_Worth_BeachAdmin
Occasional Contributor III

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

0 Kudos
7 Replies
JonathanQuinn
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>'
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 

Lake_Worth_BeachAdmin
Occasional Contributor III

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.

0 Kudos
RyanHefley
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? 

JoshuaBixby
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
inolaroch
New Contributor III

Hi @Lake_Worth_BeachAdmin,

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 🙂

 

0 Kudos
BruceBacia1
New Contributor II

Thanks for posting - this will be helpful!

0 Kudos
ErnestoCarreras2
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?