appendObject 接口用于追加写对象。追加写是指在已上传的对象末尾追加内容。只能对类型为 Appendable 的对象使用此接口。通过直接上传和分片上传创建的对象类型为 Normal。
tos:PutObject
权限,具体操作请参见权限配置指南。使用 appendObject 接口时,对象的大小限制说明如下:
以下代码用于将字符流追加上传到目标桶。
// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入 import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk'; import https from 'https'; // 创建客户端 const client = new TosClient({ accessKeyId: process.env['TOS_ACCESS_KEY'], accessKeySecret: process.env['TOS_SECRET_KEY'], region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,则 "Provide your region" 填写为 cn-beijing。 endpoint: "Provide your endpoint", // 填写域名地址 }); async function main() { try { const bucketName = 'node-sdk-test-bucket'; const objectName = 'append-example.txt'; // 第一次追加 const appendResult = await client.appendObject({ bucket: bucketName, key: objectName, offset: 0, body: Buffer.from('your append object value.'), }); console.log('appendResult', appendResult); // 追加上传网络流 const req = https.get('https://www.volcengine.com/'); const res = await new Promise((resolve) => req.on('response', resolve)); await client.appendObject({ bucket: bucketName, key: objectName, offset: appendResult.data.nextAppendOffset, body: res, contentLength: +res.headers['content-length'], }); // 查询对象的大小 const { data } = await client.headObject({ bucket: bucketName, key: objectName, }); // object size: console.log('object size:', data['content-length']); } catch (error) { handleError(error); } } function handleError(error) { if (error instanceof TosClientError) { console.log('Client Err Msg:', error.message); console.log('Client Err Stack:', error.stack); } else if (error instanceof TosServerError) { console.log('Request ID:', error.requestId); console.log('Response Status Code:', error.statusCode); console.log('Response Header:', error.headers); console.log('Response Err Code:', error.code); console.log('Response Err Msg:', error.message); } else { console.log('unexpected exception, message: ', error); } } main();
追加上传时可通过 dataTransferStatusChange
参数接受上传进度,代码示例如下。
// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入 import { DataTransferType, TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk'; import https from 'https'; // 创建客户端 const client = new TosClient({ accessKeyId: process.env['TOS_ACCESS_KEY'], accessKeySecret: process.env['TOS_SECRET_KEY'], region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,则 "Provide your region" 填写为 cn-beijing。 endpoint: "Provide your endpoint", // 填写域名地址 }); async function main() { try { const bucketName = 'node-sdk-test-bucket'; const objectName = 'append-example.txt'; const rateLimit1M = 1024 * 1024; // 第一次追加 const appendResult = await client.appendObject({ bucket: bucketName, key: objectName, offset: 0, body: Buffer.from('your append object value.'), }); console.log('appendResult', appendResult); // 追加上传网络流 const req = https.get('https://www.volcengine.com/'); const res = await new Promise((resolve) => req.on('response', resolve)); await client.appendObject({ bucket: bucketName, key: objectName, offset: appendResult.data.nextAppendOffset, body: res, contentLength: +res.headers['content-length'], // 通过自定义方式设置回调函数查看上传进度 dataTransferStatusChange: (event) => { if (event.type === DataTransferType.Started) { console.log('Data Transfer Started'); } else if (event.type === DataTransferType.Rw) { const percent = ((event.consumedBytes / event.totalBytes) * 100).toFixed(2); console.log(`Once Read:${event.rwOnceBytes},ConsumerBytes/TotalBytes: ${event.consumedBytes}/${event.totalBytes},${percent}%`); } else if (event.type === DataTransferType.Succeed) { const percent = ((event.consumedBytes / event.totalBytes) * 100).toFixed(2); console.log(`Data Transfer Succeed, ConsumerBytes/TotalBytes:${event.consumedBytes}/${event.totalBytes},${percent}%`); } else if (event.type === DataTransferType.Failed) { console.log('Data Transfer Failed'); } }, }); // 查询对象的大小 const { data } = await client.headObject({ bucket: bucketName, key: objectName, }); // object size: console.log('object size:', data['content-length']); } catch (error) { handleError(error); } } function handleError(error) { if (error instanceof TosClientError) { console.log('Client Err Msg:', error.message); console.log('Client Err Stack:', error.stack); } else if (error instanceof TosServerError) { console.log('Request ID:', error.requestId); console.log('Response Status Code:', error.statusCode); console.log('Response Header:', error.headers); console.log('Response Err Code:', error.code); console.log('Response Err Msg:', error.message); } else { console.log('unexpected exception, message: ', error); } } main();
追加上传时可以通过客户端使用 rateLimiter
参数对上传数据所占用的带宽进行限制,代码如下所示。
// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入 import { DataTransferType, createDefaultRateLimiter, TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk'; import https from 'https'; // 创建客户端 const client = new TosClient({ accessKeyId: process.env['TOS_ACCESS_KEY'], accessKeySecret: process.env['TOS_SECRET_KEY'], region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,则 "Provide your region" 填写为 cn-beijing。 endpoint: "Provide your endpoint", // 填写域名地址 }); async function main() { try { const bucketName = 'node-sdk-test-bucket'; const objectName = 'append-example.txt'; const rateLimit1M = 1024 * 1024; // 第一次追加 const appendResult = await client.appendObject({ bucket: bucketName, key: objectName, offset: 0, body: Buffer.from('your append object value.'), }); console.log('appendResult', appendResult); // 追加上传网络流 const req = https.get('https://www.volcengine.com/'); const res = await new Promise((resolve) => req.on('response', resolve)); await client.appendObject({ bucket: bucketName, key: objectName, offset: appendResult.data.nextAppendOffset, body: res, contentLength: +res.headers['content-length'], // 通过自定义方式设置回调函数查看上传进度 dataTransferStatusChange: (event) => { if (event.type === DataTransferType.Started) { console.log('Data Transfer Started'); } else if (event.type === DataTransferType.Rw) { const percent = ((event.consumedBytes / event.totalBytes) * 100).toFixed(2); console.log(`Once Read:${event.rwOnceBytes},ConsumerBytes/TotalBytes: ${event.consumedBytes}/${event.totalBytes},${percent}%`); } else if (event.type === DataTransferType.Succeed) { const percent = ((event.consumedBytes / event.totalBytes) * 100).toFixed(2); console.log(`Data Transfer Succeed, ConsumerBytes/TotalBytes:${event.consumedBytes}/${event.totalBytes},${percent}%`); } else if (event.type === DataTransferType.Failed) { console.log('Data Transfer Failed'); } }, // 上传对象并在客户端限制上传速度为 1M/s rateLimiter: createDefaultRateLimiter(rateLimit1M, rateLimit1M), }); // 查询对象的大小 const { data } = await client.headObject({ bucket: bucketName, key: objectName, }); // object size: console.log('object size:', data['content-length']); } catch (error) { handleError(error); } } function handleError(error) { if (error instanceof TosClientError) { console.log('Client Err Msg:', error.message); console.log('Client Err Stack:', error.stack); } else if (error instanceof TosServerError) { console.log('Request ID:', error.requestId); console.log('Response Status Code:', error.statusCode); console.log('Response Header:', error.headers); console.log('Response Err Code:', error.code); console.log('Response Err Msg:', error.message); } else { console.log('unexpected exception, message: ', error); } } main();
关于追加上传的 API 文档,请参见 AppendObject。