Multipart file upload

To upload a file larger than 10 MB in size to a batch, you must use multipart upload. Multipart upload involves three endpoints and the following steps:

  1. Start a multipart upload session, specifying the batch ID, file name, and file size. This call returns a session ID and maximum part_size to reference when uploading each part.

  2. Split the file into parts, according to the specified part_size, and upload each part individually.

  3. When all parts are uploaded, commit the session.

Multipart upload example request (Python)

The following Python script performs each of the tasks required for multipart file upload, when <LOCAL_FILEPATH> is defined as the full path to the file in the machine that’s running the script. The API_ROOT and API_TOKEN must also be defined.

1headers = {
2 'Authorization': f'Bearer {API_TOKEN}'
3}
4# 1. create session
5local_filepath = '<LOCAL_FILEPATH>'
6size = os.path.getsize(local_filepath)
7resp = requests.post(f'<API_ROOT>/v2/batches/multipart-upload',
8 headers=headers,
9 data=json.dumps({'path': destination_filepath, 'file_size': size}))
10session_endpoint = resp.headers['location']
11part_size = resp.json()['part_size']
12
13# 2. upload parts
14parts = []
15part_num = 1
16with open(local_filepath, 'rb') as input_file:
17 part = input_file.read(part_size)
18while part:
19 part_resp = requests.put(f'{session_endpoint}/parts/{part_num}', headers=headers, data=part)
20 parts.append({'part_num': part_num, 'part_id': part_resp.json()['part_id']})
21 part = input_file.read(part_size)
22 part_num += 1
23
24# 3. Commit all the uploaded parts.
25commit_resp = requests.post(session_endpoint, headers=headers, data=json.dumps({'action': 'commit', 'parts': parts}))

Start multipart upload session

MethodSyntax
POSTAPI_ROOT/v2/batches/multipart-upload

Description

Start a multipart upload session. Use this endpoint when you need to upload a file larger than 10 MB to a batch.

Request body

KeyTypeDescription
batch_idintThe batch ID.
filenamestringA file name for the uploaded file, including the file extension. Maximum of 255 characters.
file_sizeintThe file size, in bytes.

Response status

StatusMeaning
201 CreatedThe multipart upload session was initiated.
404 Not FoundBatch with <BATCH-ID> doesn’t exist.

Response headers

NameDescriptionValues
LocationOptional. Present if the status is 201 Created.The session endpoint URL to use in the subsequent multipart upload requests, in the form: API_ROOT/v2/batches/multipart-upload/sessions/<SESSION_ID>.

Response schema

KeyTypeDescription
part_sizeintThe number of bytes each part should be when uploading to the session. Each part should match the part_size, except for the final part, which can be smaller than the part_size.

Examples

See the full multipart upload request.

Upload part to session

MethodSyntax
PUTAPI_ROOT/v2/batches/multipart-upload/sessions/<SESSION_ID>/parts/<PART_NUM>

Description

Upload part of a file to the multipart upload session, where each part’s size matches the part_size returned by the Start multipart upload session call. Each part should match the part_size, except for the final part, which can be smaller than the part_size.

<SESSION_ID> can be obtained in the Location header from the Start multipart upload session response, and <PART_NUM> is an increasing consecutive integer sequence starting at 1 for every part uploaded.

Request body

Raw content of the part to be uploaded.

Response status

StatusMeaning
201 CreatedThe part was successfully uploaded.

Response schema

KeyTypeDescription
part_idintID of uploaded part.
part_numintThe part number of the uploaded part, indicating upload order. Identical to <PART_NUM> in the request URL.

Examples

See the full multipart upload request.

Commit session

MethodSyntax
POSTAPI_ROOT/v2/batches/multipart-upload/sessions/<SESSION_ID>

Description

After uploading all parts to a multipart upload session, use this endpoint to commit and close the multipart upload session, or to cancel the session.

Request body

KeyTypeDescription
actionstringString literal of either commit or abort.
partsListA list of the uploaded parts.
parts/part_numintThe part number of the uploaded part.
parts/part_idintThe part ID of the uploaded part.

Response status

StatusMeaning
204 No ContentThe part was successfully committed to the multipart upload session.

Response schema

There is no response body.

Examples

See the full multipart upload request.