The photos space will sunset in early January 2018. Your users can continue to access Google Photos via the drive space by enabling the Google Photos folder in My Drive in the Drive client settings

Performing a Resumable Upload

This page describes how to make a resumable upload request in the Drive API. This protocol allows you to resume an upload operation after a communication failure interrupts the flow of data. Use this option if:

  • You are transferring large files.
  • The likelihood of a network interruption or some other transmission failure is high (for example, if you are uploading a file from a mobile app).

Resumable uploads can also reduce your bandwidth usage when there is a network failure, because you don't have to restart large file uploads from the beginning.

If you are sending small files over a reliable network connection, you can use simple upload or multipart upload instead.

Learn about request URIs

When you upload media, you use a special URI. In fact, methods that support media uploads have two URI endpoints:

  • The /upload URI, for the media. The format of the /upload endpoint is the standard resource URI with an /upload prefix. Use this URI when transferring the media data itself. Example: POST /upload/drive/v3/files.
  • The standard resource URI, for the metadata. If the resource contains any data fields, those fields are used to store metadata describing the uploaded file. You can use this URI when creating or updating metadata values. Example: POST /drive/v3/files/.

Initiating a resumable upload session

To initiate a resumable upload session:

  1. Create a request to the method's /upload URI. To create a new file, use POST. To update an existing file, use PUT.
  2. Add the query parameter uploadType=resumable.

    For example:

    POST https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable

    or:

    PUT https://www.googleapis.com/upload/drive/v3/files/[FILE_ID]?uploadType=resumable

  3. If you have metadata for the file, add the metadata to the request body in JSON format. Otherwise, leave the request body empty.

  4. Add the following HTTP headers:

    • X-Upload-Content-Type. Optional. Set to the MIME type of the file data, which will be transferred in subsequent requests. If the MIME type of the data is not specified in metadata or through this header, the object will be served as application/octet-stream.
    • X-Upload-Content-Length. Optional. Set to the number of bytes of file data, which will be transferred in subsequent requests.
    • Content-Type. Required if you have metadata for the file. Set to application/json; charset=UTF-8.
    • Content-Length. Required unless you are using chunked transfer encoding. Set to the number of bytes in the body of this initial request.
  5. Send the request.

Example: Initiating a resumable upload session

The following example shows how to initiate a resumable session to upload a new file:

POST https://www.googleapis.com/drive/v3/files?uploadType=resumable HTTP/1.1
Authorization: Bearer [YOUR_AUTH_TOKEN]
Content-Length: 38
Content-Type: application/json; charset=UTF-8
X-Upload-Content-Type: image/jpeg
X-Upload-Content-Length: 2000000

{
  "name": "myObject"
}

The next section describes how to handle the response.

Saving the resumable session URI

If the session initiation request succeeds, the response includes a 200 OK HTTP status code. In addition, it includes a Location header that specifies the resumable session URI. Use the resumable session URI to upload the file data and query the upload status.

Copy and save the resumable session URI so you can use it for subsequent requests.

Example: Saving the resumable session URI

The following example shows a response that includes a resumable session URI:

HTTP/1.1 200 OK
Location: https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=xa298sd_sdlkj2
Content-Length: 0

Uploading the file

There are two ways to upload a file with a resumable session:

  1. In a single request. This approach is usually best, since it requires fewer requests and thus has better performance.
  2. In multiple chunks. Use this approach if:

    • You need to reduce the amount of data transferred in any single request. You might need to do this when there is a fixed time limit for individual requests, as is true for certain classes of Google App Engine requests.
    • You need to provide a customized indicator showing the upload progress.

Single request

To upload the file in a single request:

  1. Create a PUT request to the resumable session URI.
  2. Add the file's data to the request body.
  3. Add a Content-Length HTTP header, set to the number of bytes in the file.
  4. Send the request.

If the upload request is interrupted, or if you receive a 5xx response, follow the procedure in Resuming an interrupted upload.

Multiple chunks

To upload the file in multiple chunks:

  1. Create a PUT request to the resumable session URI.
  2. 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.
  3. Add the following HTTP headers:

    • Content-Length. Set to the number of bytes in the current chunk.
    • Content-Range: Set to show which bytes in the file you are uploading. For example, Content-Range: bytes 0-524287/2000000 shows that you are uploading the first 524,288 bytes (256 x 1024 x 2) in a 2,000,000 byte file.
  4. Send the request, and process the response.

    If the upload request is interrupted, or if you receive a 5xx response, follow the procedure in Resuming an interrupted upload.

  5. Repeat steps 1 through 4 for each remaining chunk in the file. Use the Range header in the response to determine where to start the next chunk. Do not assume that the server received all bytes sent in the previous request.

    When the entire file upload is complete, you receive a 200 OK or 201 Created response, along with any metadata associated with the resource.

Example: Uploading the file

Single request

The following example shows a resumable request to upload an entire 2,000,000-byte JPEG file, using the resumable session URI obtained in the previous step:

PUT https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=xa298sd_sdlkj2 HTTP/1.1
Content-Length: 2000000
Content-Type: image/jpeg

[BYTES 0-1999999]

If the request succeeds, you receive a 200 OK or 201 Created response, along with any metadata associated with the resource.

Multiple chunks

The following example shows a request that sends the first 524,288 bytes (512 KB) of the file, using the resumable session URI obtained in the previous step:

PUT https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=xa298sd_sdlkj2 HTTP/1.1
Content-Length: 524288
Content-Type: image/jpeg
Content-Range: bytes 0-524287/2000000

[BYTES 0-524287]

If the request succeeds, the server responds with 308 Resume Incomplete, along with a Range header that identifies the total number of bytes that have been stored so far:

HTTP/1.1 308 Resume Incomplete
Content-Length: 0
Range: bytes=0-524287

Use the upper value returned in the Range header to determine where to start the next chunk. Continue to PUT each chunk of the file until the entire file has been uploaded.

When the entire upload is complete, you receive a 200 OK or 201 Created response, along with any metadata associated with the resource.

Resuming an interrupted upload

If an upload request is terminated before receiving a response, or if you receive a 503 Service Unavailable response, then you need to resume the interrupted upload. To do this:

  1. To request the upload status, create an empty PUT request to the resumable session URI.
  2. Add a Content-Range header indicating that the current position in the file is unknown.

    For example, set the Content-Range to */2000000 if your total file length is 2,000,000 bytes.

    If you don't know the full size of the file, set the Content-Range to */*.

  3. Send the request.

  4. Process the response.

    • A 200 OK or 201 Created response indicates that the upload was completed, and no further action is necessary.
    • A 308 Resume Incomplete response indicates that you need to continue uploading the file.
    • A 404 Not Found response indicates the upload session has expired and the upload needs to be restarted from the beginning.
  5. If you received a 308 Resume Incomplete response, process the response's Range header, which specifies which bytes the server has received so far. The response will not have a Range header if no bytes have been received yet.

    For example, a Range header of bytes=0-42 indicates that the first 43 bytes of the file have been received.

  6. Now that you know where to resume the upload, continue uploading the file, either by sending the remaining data or by sending the next chunk. Include a Content-Range header indicating which portion of the file you are sending.

    For example, Content-Range: bytes 43-1999999/2000000 indicates that you are sending bytes 43 through 1,999,999.

Example: Resuming an interrupted upload

The following example shows a request for the upload status:

PUT https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=xa298sd_sdlkj2 HTTP/1.1
Content-Length: 0
Content-Range: bytes */2000000

The server's response uses the Range header to indicate that it has received the first 43 bytes of the file so far:

HTTP/1.1 308 Resume Incomplete
Content-Length: 0
Range: bytes=0-42

You can then send a request to resume the upload by sending the remaining bytes of the file, starting at byte 43:

PUT https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=xa298sd_sdlkj2 HTTP/1.1
Content-Length: 1999957
Content-Range: bytes 43-1999999/2000000

[BYTES 43-1999999]

Handling errors

When uploading media, be sure to follow these best practices related to error handling:

  • Resume or retry uploads that fail due to connection interruptions or any 5xx errors, including:
    • 500 Internal Server Error
    • 502 Bad Gateway
    • 503 Service Unavailable
    • 504 Gateway Timeout
  • Resume or retry uploads that fail due to 403 rate limit errors.
  • Use an exponential backoff strategy if any 5xx server error is returned when resuming or retrying upload requests. These errors can occur if a server is getting overloaded. Exponential backoff can help alleviate these kinds of problems when there is a high volume of requests or heavy network traffic.
  • Restart uploads if a 404 Not Found error is received after attempting to resume or upload a chunk. This indicates the upload session has expired and must be restarted from the beginning. Upload sessions expire after 1 week of inactivity.

For additional details, see Handling API Errors

发送以下问题的反馈:

此网页
Drive REST API
Drive REST API