日本時間5月16日のContent Cloud Summitで、カスタムアプリにBox AI APIを活用する方法を紹介します。

詳細を表示

アップロードセッションのコミット

ガイド アップロード 分割アップロード アップロードセッションのコミット

アップロードセッションのコミット

分割アップロードの最後の手順はセッションのコミットです。

ファイルアップロードセッションをコミットするには、コミットするアップロード済みパーツのリストを指定してPOST /files/upload_sessions/:id/commitを呼び出します。

cURL
curl -i -X POST "https://api.box.com/2.0/files/upload_sessions/F971964745A5CD0C001BBE4E58196BFD/commit" \
     -H "authorization: Bearer <ACCESS_TOKEN>" \
     -H "digest: sha=fpRyg5eVQletdZqEKaFlqwBXJzM=" \
     -H "content-type: application/json" \
     -d '{
       "parts": [
         {
           "part_id": "BFDF5379",
           "offset": 0,
           "size": 8388608,
	     "sha1": "134b65991ed521fcfe4724b7d814ab8ded5185dc"
         },
		     {
           "part_id": "E8A3ED8E",
           "offset": 8388608,
           "size": 1611392,
	     "sha1": "234b65934ed521fcfe3424b7d814ab8ded5185dc"
         }
       ],
       "attributes": {
         "content_modified_at": "2017-04-08T00:58:08Z"
       }
     }'
Java
//Creates the file hash
byte[] digestBytes = digest.digest();
//Base64 encoding of the hash
String digestStr = Base64.encode(digestBytes);

//Commit the upload session. If there is a failure, abort the commit.
BoxFile.Info fileInfo = session.commit(digestStr, parts, null, null, null);
Python
import hashlib

sha1 = hashlib.sha1()
# sha1 should have been updated with all the bytes of the file

file_atributes = {
    'description': 'A file uploaded via Chunked Upload',
}

upload_session = client.upload_session('11493C07ED3EABB6E59874D3A1EF3581')
uploaded_file = upload_session.commit(sha1.digest(), file_atributes=file_atributes)
print(f'Successfully uploaded file {uploaded_file.id} with description {uploaded_file.description}')
Node
// Finalize upload session 93D9A837B45F
client.files.commitUploadSession(
	'93D9A837B45F',
	fileHash.digest('base64'),
	{description: 'A file I uploaded in chunks!'},
	callback
);

加えて、ファイルのattributespartsとともに渡すことで、ファイルに情報を追加できます。詳細については、POST /files/content APIを参照してください。

レスポンス

成功すると、APIはHTTP 201 CreatedステータスコードとFileオブジェクトを返します。

場合によっては、パーツの作成がまだ準備できておらず、代わりに202 Acceptedステータスコードが返されることがあります。この場合、アプリケーションはretry-afterヘッダーを確認し、指定された秒数後にコミットを再試行する必要があります。