新しいファイルのアップロード
新しいファイルのアップロード
直接アップロードによってBoxにファイルをアップロードするには、ファイルのコンテンツ、目的のファイル名、フォルダIDを使用して、POST /files/content APIにAPIコールを実行します。
curl -i -X POST "https://upload.box.com/api/2.0/files/content" \
-H "authorization: Bearer <ACCESS_TOKEN>" \
-H "content-type: multipart/form-data" \
-F attributes='{"name":"Contract.pdf", "parent":{"id":"11446498"}}' \
-F file=@<FILE_NAME>const fs = require('fs');
const attrs = { name: 'filename.txt', parent: { id: '0' } };
const body = {
attributes: attrs,
file: fs.createReadStream('filename.txt'),
};
const files = await client.uploads.uploadFile(body);
const file = files.entries[0];
console.log(`File uploaded with id ${file.id}, name ${file.name}`);client.uploads.upload_file(
UploadFileAttributes(
name=new_file_name, parent=UploadFileAttributesParentField(id="0")
),
file_content_stream,
)await client.Uploads.UploadFileAsync(requestBody: new UploadFileRequestBody(attributes: new UploadFileRequestBodyAttributesField(name: newFileName, parent: new UploadFileRequestBodyAttributesParentField(id: "0")), file: fileContentStream));// Create InputStream for a file based on URL
guard let fileStream = InputStream(url: URL(string: "<URL_TO_YOUR_FILE>")!) else {
fatalError("Could not read a file")
}
// Create a request body with the required parameters
let requestBody = UploadFileRequestBodyArg(
attributes: UploadFileRequestBodyArgAttributesField(
name: "filename.txt",
parent: UploadFileRequestBodyArgAttributesFieldParentField(id: "0")
),
file: fileStream
)
// Call uploadFile method
let files = try await client.uploads.uploadFile(requestBody: requestBody)
// Print some data from the reponse
if let file = files.entries?[0] {
print("File uploaded with id \(file.id), name \(file.name!)")
}client.getUploads().uploadFile(new UploadFileRequestBody(new UploadFileRequestBodyAttributesField(newFileName, new UploadFileRequestBodyAttributesParentField("0")), fileContentStream))await client.Uploads.UploadFileAsync(requestBody: new UploadFileRequestBody(attributes: new UploadFileRequestBodyAttributesField(name: newFileName, parent: new UploadFileRequestBodyAttributesParentField(id: "0")), file: fileContentStream));const fs = require('fs');
const attrs = { name: 'filename.txt', parent: { id: '0' } };
const body = {
attributes: attrs,
file: fs.createReadStream('filename.txt'),
};
const files = await client.uploads.uploadFile(body);
const file = files.entries[0];
console.log(`File uploaded with id ${file.id}, name ${file.name}`);リクエスト形式
このAPIのリクエスト本文には、multipart/form-dataのコンテンツタイプが使用されます。これを使用して、ファイル属性とファイルの実際のコンテンツの2つの部分を送信します。
最初の部分はattributesと呼ばれ、ファイル名や親フォルダのidなど、ファイルに関する情報を含むJSONオブジェクトが含まれています。
以下の例では、ユーザーのルートフォルダにtest.txtをアップロードしています。
POST /api/2.0/files/content HTTP/1.1
Host: upload.box.com
Authorization: Bearer [ACCESS_TOKEN]
content-length: 343
content-type: multipart/form-data; boundary=------------------------9fd09388d840fef1
--------------------------9fd09388d840fef1
content-disposition: form-data; name="attributes"
{"name":"test.txt", "parent":{"id":"0"}}
--------------------------9fd09388d840fef1
content-disposition: form-data; name="file"; filename="test.txt"
content-type: text/plain
Test file text.
--------------------------9fd09388d840fef1--
オプション
ファイルのアップロード時に使用できるすべてのパラメータの詳細については、このAPIコールに関するリファレンスドキュメントを参照してください。パラメータには、設定することで転送中のファイルの破損を防ぐcontent-md5や、アップロード時間とは異なる時間をファイル作成時間として明示的に指定できる機能が含まれます。
制約事項
直接アップロードできるファイルサイズの上限は50 MBです。ファイルがこれより大きい場合は、分割アップロードAPIを使用してください。
アップロードの上限は、認証済みユーザーのアカウントの種類によって決まります。詳細については、このトピックに関するBoxコミュニティの記事を参照してください。