hydrotasmaniaGIS

Python upload of local CSV file returns error - Error:500 Item does not have a file

Discussion created by hydrotasmaniaGIS on Jan 13, 2014
Latest reply on Jan 15, 2014 by arekmatoszka
Hi,

Help with the following issue (Error:500 Item does not have a file) would be greatly appreciated

I have written the following python script with the assistance of some sample code I found and with the aid of ArcGIS Online Rest help documentation. You will note I have used 2 alternative methods to upload the same locally stored CSV file onto my orginisation's ArcGIS Online site. Both methods return the status of "Success" specifically with a message such as "{"success" : true,"id" : "0ded368614754acd88d00084d5804a7d","folder" : null}"

The upload at first glance looks fine as on the ArcGIS Online site a new item appears added to 'My Contents' and is correctly named with properties correctly populated by the python code. However, when I try to open the CSV file ArcGIS Online returns the error - Error:500 'Item does not have a file'.

Can someone please take a look at the code and suggest what may be the cause of error? I tried to upload different files including some in KML file format and got the exact same error.

The code below has dummy user-name and password details added and currently assumes that the local file is stored "'E:\sample.csv"

#----------------------------------------
#Sample Python Script

import json, urllib, urllib2
import requests
from json import JSONEncoder

def gentoken(username, password, referer, expiration=60):
#Re-usable function to get a token required for Admin changes
referer = "http://www.arcgis.com/"
query_dict = {'username': username,
'password': password,
'expiration': str(expiration),
'client': 'referer',
'referer': referer,
'f': 'json'}
tokenUrl = "https://www.arcgis.com/sharing/rest/generateToken"
tokenResponse = urllib.urlopen(tokenUrl, urllib.urlencode(query_dict))
token = json.loads(tokenResponse.read())
if "token" not in token:
print token['messages']
import sys
sys.exit()
else:
# Return the token to the function which called for it
print str(token)
return token['token']

def uploadCSVfile1(token, ThePlaceTOupload):

files = {'file': open('E:\sample.csv','rb'),
'type':'CSV',
'filename':'E:\sample.csv',
'title':'AA_Upload_CSV_File_Method_1',
'async': 'false',
'accessInformation' : 'Copyright:? 2014 Hydro Tasmania',
'spatialReference':'GDA 94',
'licenseInfo':'This work is licensed to Hydro Tasmania',
'description': 'CSV description',
'tags':'web, mapping, application',
'snippet' : 'This new snippet',
'typeKeywords': 'Static',
'f': 'json',
'token': token
}
r = requests.post(ThePlaceTOupload, files)
print r.content
print "Method 1 Returned Message"


def uploadCSVfile2(token, ThePlaceTOupload):

query_dict = {'file': open('E:\sample.csv','rb'),
'type':'CSV',
'filename':'E:\sample.csv',
'title':'AA_Upload_CSV_File_Method_2',
'async': 'false',
'accessInformation' : 'Copyright:? 2014 Hydro Tasmania',
'spatialReference':'GDA 94',
'licenseInfo':'This work is licensed to Hydro Tasmania',
'description': 'CSV description',
'tags':'web, mapping, application',
'snippet' : 'This new snippet',
'typeKeywords': 'Static',
'f': 'json',
'token': token
}
attachResponse = urllib.urlopen(ThePlaceTOupload, urllib.urlencode(query_dict))
msg = json.loads(attachResponse.read())
print "Method 2 Returned Message"
print msg
return

def main():
USERNAME = "My_Org_User_Name"
PASSWORD = "My_Org_Password"
REFFER = "http://www.arcgis.com"


ThePlaceTOupload = "https://ht.maps.arcgis.com/sharing/rest/content/users/My_Org/addItem/"

token = gentoken(USERNAME, PASSWORD, REFFER)

uploadCSVfile1(token, ThePlaceTOupload) # Method 1
uploadCSVfile2(token, ThePlaceTOupload) # Method 2

if __name__ == '__main__':


main()

Outcomes