Uploads large files in multiple chunks. Also has the ability to resume if the upload is interrupted.
Typical usage:
- Send a POST request to
/upload
with the first chunk of the file and receive an upload id in return. - Repeatedly PATCH subsequent chunks using the upload id to identify the upload in progress and an offset representing the number of bytes transferred so far.
- After each chunk has been uploaded, the server returns a new offset representing the total amount transferred.
- After the last chunk commit the upload by passing its id to another endpoint such as
POST /upload/commit/:id
:
POST /upload/commit/20140806036f1378006746bcb40a0e3981257552 HTTP/1.1
Host: api.example.com
Accept: application/vnd.example.v1+json
Chunks can be any size up to 150 MB. A typical chunk is 4 MB. Using large chunks will mean fewer calls to /upload and faster overall throughput. However, whenever a transfer is interrupted, you will have to resume at the beginning of the last chunk, so it is often safer to use smaller chunks.
If the offset you submit does not match the expected offset on the server, the server will ignore the request and respond with a 400 error that includes the current offset. To resume upload, seek to the correct offset (in bytes) within the file and then resume uploading from that point.
A chunked upload can take a maximum of 48 hours before expiring.
POST /upload
Request
POST /upload HTTP/1.1
Host: api.example.com
Content-Type: application/offset+octet-stream
Content-Length: 221993
File-Name: video.mp4
File-Size: 443987
Offset: 0
...[binary data]...
Parameters
Name | Type | Description |
---|---|---|
File-Name | string |
The name of the file that you're uploading. |
File-Size | number |
The full/final size of the file in bytes. |
Content-Length | number |
The length in bytes of the chunk you are sending. |
Checksum-Type | string |
(Optional) Passing checksum type signals to the server that you'd like it to return a checksum generated via: MD5 , SHA1 or SHA2 . The default is MD5 |
Response
HTTP/1.1 201 Created
Checksum: d41d8cd98f00b204e9800998ecf8427e
Checksum-Type: MD5
{
"id": "20140806036f1378006746bcb40a0e3981257552",
"offset": 221993,
"expires": "2014-08-02T22:43:49Z"
}
PATCH /upload/:id
Request
POST /upload/20140806036f1378006746bcb40a0e3981257552 HTTP/1.1
Host: api.example.com
Content-Type: application/offset+octet-stream
Content-Length: 221993
Offset: 221993
...[binary data]...
Parameters
Name | Type | Description |
---|---|---|
Offset | number |
The byte offset of this chunk, relative to the beginning of the full file. The server will verify that this matches the offset it expects. If it does not, the server will return an error with the expected offset. |
Checksum-Type | string |
(Optional) Passing checksum_type signals to the server that you'd like it to return a checksum generated via: MD5 , SHA1 or SHA2 |
Response
HTTP/1.1 200 OK
Checksum: d41d8cd98f00b204e9800998ecf8427e
Checksum-Type: MD5
{
"id": "20140806036f1378006746bcb40a0e3981257552",
"offset": 443986,
"expires": "2014-08-02T22:43:49Z"
}
Errors
404 - The upload id does not exist or has expired.
400 - The offset parameter does not match up with what the server expects. The body of the error response will be JSON similar to the above, indicating the correct offset to upload.
POST /upload/:id
Request
POST /upload/20140806036f1378006746bcb40a0e3981257552 HTTP/1.1
Host: api.example.com
Response
HTTP/1.1 200 OK
{
"id": "20140806036f1378006746bcb40a0e3981257552",
"offset": 221993,
"expires": "2014-08-02T22:43:49Z"
}
Nice! How does it handle if chunks arrive out of order?