Uploading file to Google Drive using resumable API

Sandesh Bhusal :

I have a quite large file that I am trying to upload to google drive using the API. I am trying to do it with a sample image for learning purposes. Uploading the image as a multiplart upload or a single file upload works without hesitation, but the moment I try to do it using the resumable upload endpoint, the code gives me a error as:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "badContent",
    "message": "Unsupported content with type: image/jpeg"
   }
  ],
  "code": 400,
  "message": "Unsupported content with type: image/jpeg"
 }
}

The code that I am using is as follows:

import requests
import os
filesize = os.path.getsize('./photo.jpeg')
print("File size is: ", filesize)

headers = {"Authorization" : "Bearer "+"<MY API KEY HERE>",
        "Content-Length": str(filesize),
        "Content-Type": "image/jpeg"}
params = {
        "name": "sample.png",
        "parents": ['1CxrbEfy5y3ZyBVF6k2IFIuOk_Z0wjZAo']
        }
files  = {
        'data': ('metadata', json.dumps(params), 'image/jpeg'),
        'file': open('./photo.jpeg', 'rb')
        }

r = requests.post(
        "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
        headers = headers,
        files = files
        )
print(r.text)

Please help.

Tanaike :
  • You want to upload an image file of photo.jpeg using the resumable upload.
  • You want to achieve this using requests with python.
  • Your access token can upload a file to Google Drive.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification points:

  • In your script,

    • params is not used.
    • The file is directly send to https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable.
    • "Content-Type": "image/jpeg" is used in the request headers. By this, the error occurs.
    • You try to upload './photo.jpeg'. But you try to set the filename as sample.png.
  • In order to upload a file with the resumable upload, at first, it is required to retrieve location which is the URL for uploading the file. This is included in the response headers. Ref

  • After location was retrieved, the data can be uploaded to the endpoint of location.

Sample script:

In the following sample script, an image (image/jpeg) is uploaded with the resumable upload. In this case, as a simple test, the file is uploaded by one chunk. Before you use this, please set the variables of access_token, filename

import json
import os
import requests

access_token = '###'  # Please set your access token.
filename = './photo.jpeg'  # Please set the filename with path.

filesize = os.path.getsize(filename)
print("File size is: ", filesize)

# 1. Retrieve session for resumable upload.
headers = {"Authorization": "Bearer "+access_token, "Content-Type": "application/json"}
params = {
    "name": "sample.jpeg",
    "parents": ['1CxrbEfy5y3ZyBVF6k2IFIuOk_Z0wjZAo'],
    "mimeType": "image/jpeg"
}
r = requests.post(
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
    headers=headers,
    data=json.dumps(params)
)
location = r.headers['Location']

# 2. Upload the file.
headers = {"Content-Range": "bytes 0-" + str(filesize - 1) + "/" + str(filesize)}
r = requests.put(location, headers=headers, data=open(filename, 'rb'))
print(r.text)
  • Please also confirm whether the parent ID of 1CxrbEfy5y3ZyBVF6k2IFIuOk_Z0wjZAo is correct, again.

Reference:

If I misunderstood your question and this was not the direction you want, I apologize.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=24960&siteId=1