Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading