I am trying to upload large files (greater than 5 MB) to Google Drive. Based Google's documentation, I would need to setup a resumable upload session. If the session is started successfully, you will get a response with a session URI. Then send another request to the URI with what I presume is your file.
I have been able to successfully set up the resumable session but I am unclear where you specify the location of your file for uploading with this method. Please see my code below.
What Google wants to start Resumable Upload
POST https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable HTTP/1.1
Authorization: Bearer [YOUR_AUTH_TOKEN]
Content-Length: 38
Content-Type: application/json; charset=UTF-8
X-Upload-Content-Type: application/octet-stream
{
"name": "myObject"
}
How I did it in Python
import requests
from oauth2client.service_account import ServiceAccountCredentials
credentials = ServiceAccountCredentials.from_json_keyfile_dict(
keyfile_dict=[SERVICE_ACCOUNT_JSON],
scopes='https://www.googleapis.com/auth/drive')
delegated_credentials = credentials.create_delegated([EMAIL_ADDRESS])
access_token = delegated_credentials.get_access_token().access_token
url = "https://www.googleapis.com/upload/drive/v3/files"
querystring = {"uploadType": "resumable"}
payload = '{"name": "myObject", "parents": "[PARENT_FOLDER]"}'
headers = {
'Content-Length': "38",
'Content-Type': "application/json",
'X-Upload-Content-Type': "application/octet-stream",
'Authorization': "Bearer " + access_token
}
response = requests.request(
"POST", url, data=payload, headers=headers, params=querystring)
print(response.headers['Location'])
A successful response location URI
https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=[SOME_LONG_ID]
PUT Request Google wants
PUT https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=[SOME_LONG_ID] HTTP/1.1
Content-Length: 2000000
Content-Type: application/octet-stream
[BYTES 0-1999999]
PUT request in python - This is where I start to get lost
uri = response.headers['Location']
headers = {
'Content-Length': "2000000",
'Content-Type': "application/json"
}
response = requests.request(
"PUT", uri, headers=headers)
I would like to know how to complete this PUT request with the path to my file and any other information that is needed. Thanks for the help.