Resumable download with google drive api, python put request

I am trying to use resumable download with put request and file_metadata is fully ignored by google drive api, this code creates Untitled file in root of my google drive, I apologize for my poor English skills.

    file_metadata = {"name": "test.txt",
                     'parents': ["###myFolderId###"]}
    data = {
        "MetaData": (
            "metadata",
            json.dumps(file_metadata),
            "application/json; charset=UTF-8",
        ),
        "Media": open(saver_path+"file_id.txt", "rb"),
    }
    params = {"uploadType": "resumable"}

    sizeOfFile = str(os.path.getsize(saver_path+"file_id.txt")) #Measures the size of the file
    putHeaders = {'Authorization': 'Bearer ' + access_token, 'Content-Length': sizeOfFile} #Headers

    put = requests.put(location_url, files=data, params=params, headers=putHeaders)
    print("Put request ", put.text)

Api answer:

  "kind": "drive#file",
  "id": "###fileId###",
  "name": "Untitled",
  "mimeType": "application/octet-stream"

My goal :
I need to create a file in a specific folder on google drive and name it specifically

>Solution :

From your question My goal : I need to create a file in a specific folder on google drive and name it specifically and showing script, I guessed that you wanted to upload a file with the resumable upload. If my understanding is correct, how about the following modification?

Modified script:

import json
import requests
import os

access_token = "###" # Please set your access token.
folder_id = "###" # Please set your folder ID.
filename = saver_path+"file_id.txt" # Please set your path.

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

# 2. Upload the file.
sizeOfFile = os.path.getsize(filename)
headers = {"Content-Range": "bytes 0-" + str(sizeOfFile - 1) + "/" + str(sizeOfFile)}
r = requests.put(location, headers=headers, data=open(filename, 'rb'))
print(r.text)
  • In this modification, a simple resumable upload is used. First, the location URL is retrieved and uploaded metadata and as the 2nd step, the file content is uploaded.

  • By the way, if you want to upload a file using multipart, you can also use the following modification.

    import json
    import requests
    
    access_token = "###" # Please set your access token.
    folder_id = "###" # Please set your folder ID.
    filename = saver_path+"file_id.txt" # Please set your path.
    
    location_url = "https://www.googleapis.com/upload/drive/v3/files"
    headers = {"Authorization": "Bearer "+access_token}
    file_metadata = {"name": "test.txt", 'parents': [folder_id]}
    params = {"uploadType": "multipart"}
    r = requests.post(location_url, files={'data': ('metadata', json.dumps(file_metadata), 'application/json; charset=UTF-8'), 'file': open(filename, "rb")}, params=params, headers=headers)
    print(r.text)
    

Reference:

Leave a Reply