1
votes

I've been trying to learn how to use the Google Drive API to update a file in the Google Drive by using a resumable session.

I received a 'Forbidden' response to the upload content request.

Could you help me find missing or misused steps?

  1. User is authorized with permissions:

  2. Execute a request to create resumable session:

    PATCH 'https://www.googleapis.com/upload/drive/v3/files/1XIU63B-U8b9Fe1_UFFVvd7OOdS_ANqAj?uploadType=resumable

  3. Retrieve session url:

    https://www.googleapis.com/upload/drive/v3/files/1XIU63B-U8b9Fe1_UFFVvd7OOdS_ANqAj?uploadType=resumable&upload_id=AEnB2Uqew...

  4. Send content by using resumable session:

    PUT https://www.googleapis.com/upload/drive/v3/files/1XIU63B-U8b9Fe1_UFFVvd7OOdS_ANqAj?uploadType=resumable&upload_id=AEnB2Uqew...

    I didn't find anything specific related to this step in the documentation, so I use regular upload documentation https://developers.google.com/drive/api/v3/manage-uploads#upload-resumable to update file in "Multiple chunks"

  5. I get 403 error status code with 'Forbidden' reason and header with upload_id:

    X-GUploader-UploadID: AEnB2Uqewr...

1

1 Answers

2
votes
  • 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 "[email protected]" \
  "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 "[email protected]" \
  "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: