对象大于 5GiB 时,需要使用 uploadPartCopy 来进行分片拷贝。
// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入 import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk'; // 创建客户端 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", // 填写域名地址 }); function handleError(error) { if (error instanceof TosClientError) { console.log('Client Err Name:', error.name); 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 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); } } async function main() { try { const srcBucket = 'node-sdk-test-bucket'; // 源桶 const srcObject = 'source-test'; // 源对象 const dstBucket = 'node-sdk-copy-bucket'; // 目标桶 const dstObject = 'copy-test'; // 目标对象 // 获取上传任务 ID const { data: { UploadId }, } = await client.createMultipartUpload({ bucket: dstBucket, key: dstObject, }); console.log('UploadId', UploadId); // 获取桶源桶中已存在对象的大小 const { data: { 'content-length': contentLength }, } = await client.headObject({ bucket: srcBucket, key: srcObject, }); console.log('srcObject content-length', contentLength); // 文件总大小 const totalSize = contentLength; const partSize = 5 * 1024 * 1024; let offset = 0; let partNumber = 1; const partsInfo = []; while (offset < totalSize) { // 上传分片拷贝 const uploadResult = await client.uploadPartCopy({ bucket: dstBucket, key: dstObject, partNumber, uploadId: UploadId, srcBucket, srcKey: srcObject, copySourceRangeStart: offset, copySourceRangeEnd: offset + partSize - 1, }); const eTag = uploadResult.data.ETag; console.log(`partNumber${partNumber} Etag:`, eTag); partsInfo.push({ partNumber, eTag, }); partNumber++; offset += partSize; } console.log('parts information', partsInfo); // 完成分片上传 const { data } = await client.completeMultipartUpload({ bucket: dstBucket, key: dstObject, uploadId: UploadId, parts: partsInfo, }); console.log('result data:', data); // 查询拷贝对象信息 const headResult = await client.headObject({ bucket: dstBucket, key: dstObject, }); console.log('headResult', headResult); } catch (error) { handleError(error); } } main();