2

I'm uploading a jpg file into my google drive account. It works fine, but I need it to upload to a specific folder but am not sure how to set the parents parameter in the metadata.

Here's my code:

data = {"file": open(filedirectory, 'rb').read(), "title" : filename, "parents" : [{"id": "<folderid>"}]}
drive_url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=media"
drive_r = requests.post(drive_url, data=data, headers={"Authorization": "Bearer " + access_token, "Content-type": "image/jpeg"})

2 Answers 2

7

I believe your goal as follows.

  • You want to upload a file to the specific folder in Google Drive.
  • You want to achieve this using requests of python.

Modification points:

  • In the case of uploadType=media, the official document says as follows.

    Simple upload (uploadType=media). Use this upload type to quickly transfer a small media file (5 MB or less) without supplying metadata. To perform a simple upload, refer to Perform a simple upload.

  • So in order to upload the file content and file metadata, please use uploadType=multipart.

  • And also, in your endpoint, Drive API v3 is used. But "parents" : [{"id": "<folderid>"}] is for Drive API v2. It is required to also modify it.

When your script is modified for uploadType=multipart, it becomes as follows.

Modified script:

When you use this script, please set the variables of filedirectory, filename, folderid, access_token.

import json
import requests

filedirectory = '###'
filename = '###'
folderid = '###'
access_token = '###'

metadata = {
    "name": filename,
    "parents": [folderid]
}
files = {
    'data': ('metadata', json.dumps(metadata), 'application/json'),
    'file': open(filedirectory, "rb").read()  # or  open(filedirectory, "rb")
}
r = requests.post(
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
    headers={"Authorization": "Bearer " + access_token},
    files=files
)
print(r.text)

Note:

  • This modified script supposes that your access token can be used for uploading the file to Google Drive.

References:

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much for the quick answer! I saw the multipart section of the docs but was really confused. Appreciate you writing out the example for me too because it's been hard for me to find something to base my code on.
@tammi Thank you for replying. I'm glad your issue was resolved.
But the problem is that the access token expires. Is there any way to make it permanent?
@ThuggyTM About your question, can you post it as a new question? Because I think that your question will be useful for other users. If you can cooperate with it, I'm glad. Can you cooperate?
-1

it seems I have been able to get the Javascript SDK to react, with this code

                                gapi.client.drive.files.create({
                                    name: 'multapart.jpg',   //OPTIONAL
                                    uploadType: 'multipart',
                                },{body:'your content here',content:'your content here'})

where media is bytes representation for an image

enter image description here

in chronium edge it complained that

Access to XMLHttpRequest at 'https://content.googleapis.com/drive/v3/files?name=resumable.jpg&body=%EF%BF%BD%EF%....%BD&uploadType=multipart&alt=json&key=[you cant see this haha]' from origin 'https://content.googleapis.com' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, edge, https, chrome-untrusted.

in edge legacy it said it was a malformed and when I did uploadType=media, it said is was using https://content.googleapis.com/drive/v3/files instead of https://content.googleapis.com/upload/drive/v3/files, so the JS SDK is unreliable and riddled with bugs, glad I got it to react, if only someone can find the logic to give it the right URL because I believe the js SDK is not buggy, google doesnt want ppl to use it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.