I swear to the LMS gods that the following Python requests code was working just yesterday:
import requests
# Step 1: Tell Canvas you're going to upload a file
filename = 'test.svg'
folderPath = '/myFolder/test'
size = os.path.getsize(filename)
data = { 'name' : filename,
'size' : str(size),
'content_type' : 'image/svg+xml',
'parent_folder_path' : folderPath }
response = requests.post(filesURL, json=data, headers=headers)
response.raise_for_status()
response = response.json()
# Step 2: Upload the file
files = list(response['upload_params'].items()) # Get all upload params
file_content = open('svg/'+filename, 'rb').read() # Read in file data
files.append((u'file', file_content)) # Add file data to payload
response = requests.post(response['upload_url'], files=files)
response.raise_for_status()
id = str(response.json()['id'])
However, in step 1, when I get the first response, it looks nothing like it used to or what's in the documentation:
{
'file_param': 'file',
'progress': None,
'upload_url': 'https://inst-fs-iad-prod.inscloudgate.net/files?token=<crazy long token>',
'upload_params':
{
'filename': 'c02s04n02a.svg',
'content_type': 'image/svg+xml'
}
}
The code produces the following error:
500 Server Error: Internal Server Error for url: https://inst-fs-iad-prod.inscloudgate.net/files?token=<blah etc.>
Am I missing something? Has a change of file repository occurred from AWS?
Would appreciate some help!
Thanks
Solved! Go to Solution.
That definitely helps clarify the issue, thanks. Right now, I can't access Canvas at all -- getting server back end issues or the site cannot be reached. At least that is documented on their status page. Even if I could access it, I probably won't be able to help with this one. The only issues I've personally encountered with inscloudgate was getting messages about blocked third-party cookies. I was just trying to tell if it was related to the other file upload issues going on and it doesn't sound like it. Hopefully someone else has ideas.
I had the same errors, and found that a "filename" parameter which Requests adds to multipart/form-data parts was the problem.
To prevent Requests from adding this, the files argument for requests.post can be provided with tuples of values, giving None in the first position, so we have a list like [('Name', (None, 'value')), ('Name2', (None, 'value2'))].
So in the code sample in the original post, it might help changing line 21 to
files = [(key, (None, val)) for key, val in response['upload_params'].items()] # Get all upload params
I figured this out from this Stackoverflow answer, so thanks to Martijn Pieters!
I figured this out finally. It appears that posting to inscloudgate requires that you include the "filename" as part of the form data of the actual file (even though this is already specified as separate form data per Canvas uploading files documentation).
So instead of:
Ah, thank you! This has solved the issue for me! :smileygrin:
What a strange format! But, this definitely seems like the best solution as it is somewhat resilient to future changes in the API.
You have saved us!