# Updating a File with Resumable Upload using Drive API

Gists

This is a sample flow for updating a file with the resumable upload using Drive API.

## 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 chunks of 262,144 bytes and 37,856 bytes are uploaded. So total upload size is 300,000 bytes. Those filenames are data1.txt and data2.txt, respectively.

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"}' \

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

$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" \

• 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".