Google Drive API - PHP Client Library - Setting Uploadtype to Resumable Upload

Unable to update file in the Google Drive by using resumable approach

  • You want to update the existing file in Google Drive with the resumable upload method.

Unfortunately, from your question, I couldn't understand about the detail request body of your test. By this, I cannot replicate your situation. So in this answer, I would like to propose a sample flow for updating the existing file with the resumable upload.

Sample situation:

In this answer, as a sample situation, it supposes that a text file in Google Drive is updated by the resumable upload with the multiple chunks. And as the method for requesting, I use the curl command.

I prepared 2 files for 2 chunks. As the test situation, the 2 chuncs of 262,144 bytes and 37,856 bytes are uploaded. So total upload size is 300,000 bytes.

When you use the resumable upload, please be careful the following point.

Add the chunk's data to the request body. Create chunks in multiples of 256 KB (256 x 1024 bytes) in size, except for the final chunk that completes the upload. Keep the chunk size as large as possible so that the upload is efficient. Ref

Flow for updating a file with the resumable upload:

1. Initiate a resumable upload session

Create the session for uploading with the resumable upload. In this case, the existing file is updated, so the endpoint is PUT https://www.googleapis.com/upload/drive/v3/files/[FILE_ID]?uploadType=resumable. But as an important point, please use the method of PATCH instead of PUT. When PUT is used, location is not included in the response header. I thought that the official document might be not correct.

$ curl -X PATCH -i \
-H "Authorization: Bearer ###accessToken###" \
"https://www.googleapis.com/upload/drive/v3/files/[FILE_ID]?uploadType=resumable"

If you want to update the file as the multipart upload, please use the following sample command. In this case, the filename is changed.

$ curl -X PATCH -i \
-H "Authorization: Bearer ###accessToken###" \
-H "Content-Type: application/json; charset=UTF-8" \
-d '{"name":"updatedFilename.txt"}' \
"https://www.googleapis.com/upload/drive/v3/files/[FILE_ID]?uploadType=resumable"
  • When above sample command is run, 200 OK is returned, and the response header includes location like location: https://www.googleapis.com/upload/drive/v3/files/[FILE_ID]?uploadType=resumable&upload_id=###. For uploading the data, location is used as the endpoint.

2. Upload the 1st chunk

$ curl -X PUT -i \
-H "Content-Length: 262144" \
-H "Content-Range: bytes 0-262143/300000" \
-H "Content-Type: text/plain" \
-F "file=@data1.txt" \
"https://www.googleapis.com/upload/drive/v3/files/[FILE_ID]?uploadType=resumable&upload_id=###"
  • When this curl command is run, 308 Resume Incomplete is returned. By this, it is found that the chunk could be correctly uploaded.

3. Upload the 2nd chunk (This is the last chunk of this sample flow.)

$ curl -X PUT -i \
-H "Content-Length: 37856" \
-H "Content-Range: bytes 262144-299999/300000" \
-H "Content-Type: text/plain" \
-F "file=@data2.txt" \
"https://www.googleapis.com/upload/drive/v3/files/[FILE_ID]?uploadType=resumable&upload_id=###"
  • When this curl command is run, 200 OK is returned, and the file metadata is also returned. By this, it is found that the resumable upload could be correctly done.

Note:

  • In this case, the file is updated as the overwrite. So please be careful this.
  • In my environment, even when PUT is modified to PATCH for uploading the chunks, I could confirm that the above flow worked.
    • If in your environment, an error occurs, please try to test this modification.
  • About above sample situation, if you want to upload one chunk of 300,000 bytes, please use -H "Content-Length: 300000" -H "Content-Range: bytes 0-299999/300000".

References:

  • Perform a resumable upload

Uploading file to Google Drive using resumable API

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

  • Perform a resumable upload

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

How to upload a file on google drive with php Google Drive API

A service account is NOT you. A service account has its own google drive account. The file is being uploaded to its google drive account.

If you want it to upload to your google drive account then take the service account email address add share a directory in your google drive account with it. Then take the file id / drive id of that folder and set it as the parent in the file metadata when you upload the file and it will be uploaded to your drive account and not the service accounts.

remember to have the service account grant you permissions on that file after its been uplaoded the file will be owned by the service account.



Related Topics



Leave a reply



Submit