Simple Script of Resumable Upload with Google Drive API for Python


This is a simple sample script for achieving the resumable upload to Google Drive using Python. In order to achieve the resumable upload, at first, it is required to retrieve the location, which is the endpoint of upload. The location is included in the response headers. After the location was retrieved, the file can be uploaded to the location URL.

In this sample, a PNG file is uploaded with the resumable upload using a single chunk.

Sample script

Before you use this, please set the variables.

import json
import os
import requests

access_token = '###'  ## Please set the access token.
filename = './sample.png'

filesize = os.path.getsize(filename)

# 1. Retrieve session for resumable upload.
headers = {"Authorization": "Bearer "+access_token, "Content-Type": "application/json"}
params = {
    "name": "sample.png",
    "mimeType": "image/png"
r =
location = r.headers['Location']

# 2. Upload the file.
headers = {"Content-Range": "bytes 0-" + str(filesize - 1) + "/" + str(filesize)}
r = requests.put(
    data=open(filename, 'rb')