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

詳細を表示

ファイルのダウンロード

ガイド ダウンロード ファイルのダウンロード

ファイルのダウンロード

ファイルをダウンロードするには、取得するコンテンツが含まれるファイルのIDをGET /files/:id/contentに渡します。

cURL
curl -i -L -X GET "https://api.box.com/2.0/files/12345/content" \
     -H "authorization: Bearer <ACCESS_TOKEN>" \
.NET
Stream fileContents = await client.FilesManager.DownloadStreamAsync(id: "11111");
Java
BoxFile file = new BoxFile(api, "id");
BoxFile.Info info = file.getInfo();

FileOutputStream stream = new FileOutputStream(info.getName());
file.download(stream);
stream.close();
Python
file_id = '11111'
file_content = client.file(file_id).content()
Node
var fs = require('fs');
client.files.getReadStream('12345', null, function(error, stream) {

	if (error) {
		// handle error
	}

	// write the file to disk
	var output = fs.createWriteStream('/path/to/file');
	stream.pipe(output);
});
iOS
let url = FileManager.default.homeDirectoryForCurrentUser

let task: BoxDownloadTask = client.files.download(fileId: "11111", destinationURL: url) { (result: Result<Void, BoxSDKError>) in
    guard case .success = result else {
        print("Error downloading file")
        return
    }

    print("File downloaded successfully")
}

// To cancel download
if someConditionIsSatisfied {
    task.cancel()
}

ダウンロードURL

SDKを使用しない場合、このAPIコールでは、HTTP 302 Foundステータスコードとともに、次のようなダウンロードURLへのリンクを含むlocationヘッダーが返されます。

https://dl.boxcloud.com/d/1/[long-random-string]/download

cURLで-Lフラグを使用することで、自動的にこのリダイレクトに従うことができます。

SDKでは、結果として、バイナリデータがダウンロードされます。APIでは、ダウンロードURLがlocationヘッダーを介して返されます。

また、SDKを介してダウンロードURLを取得することも可能です。

ダウンロードURLの有効期限

このダウンロードURLは、ファイルのダウンロードを許可するためにユーザーのブラウザに渡すことができますが、このURLが期限切れになると、その後でダウンロードするには再度リクエストする必要があります。

ファイルの準備ができていない

ファイルをダウンロードする準備がまだできていない場合は、クライアントがファイルをダウンロードできるようになるまでの秒数を示すretry-afterヘッダーが返されます。

このレスポンスは、ダウンロードリクエストの直前にファイルがアップロードされた場合に発生することがあります。