How do I perform a multipart upload?
The Upload API supports 3 ways to upload a file:
- Multipart uploads (recommended for performance)
- Single-part binary uploads (simple)
- Single-part form-data uploads (simple)
Options 2 and 3 only require a single HTTP request.
Option 1 requires multiple HTTP requests.
To implement multipart uploads in another language:
- Calculate the file's size.
- Call the begin multipart upload endpoint, passing the file size.
- Open the file for reading.
- Read a section of the file. (This must be the same section specified by the "inclusiveStart" and "inclusiveEnd" fields from the HTTP response from step 2 or step 10.)
- Make an HTTP "PUT" request, using the data from step 4 as the request body, and the "uploadUrl" from the HTTP response in step 2 / step 10 as the URL.
- If "uploadParts.count = 1", then add the following HTTP request headers, changing image/jpeg and example.jpg as necessary. (If "uploadParts.count > 1", add no additional request headers.)
Content-Type: image/jpeg Content-Disposition: inline; filename="example.jpg"; filename*=UTF-8''example.jpg
- Read the "etag" HTTP response header from step 5.
- Call complete upload part, passing the "etag", "fileId", and "uploadPartIndex".
- If "uploadPartIndex = uploadParts.count - 1", then exit (the upload is complete), else continue:
- Call get upload part, passing an incremented "uploadPartIndex".
- Repeat from step 4, using the "uploadPartIndex", "uploadUrl", "inclusiveStart", and "inclusiveEnd" returned by step 10.