上传回调是指客户端在请求时携带回调(Callback)参数,服务端在上传完成后,发送同步的 POST 回调请求到 CallBack 中指定的第三方应用服务器,在服务器确认接受并返回结果后,才将所有结果返回给客户端。
关于上传回调的详细介绍,请参见上传回调。
// 导入 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 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); } } const bucketName = 'node-sdk-test-bucket'; async function main() { try { const callbackUrl = '*** Provide your callback url ***'; const objectKey = '*** Provide your object key ***'; // 设置上传回调参数 // 可以根据需求设置 callback 参数 const callback = ` { "callbackUrl" : "${callbackUrl}", "callbackBody" : ${'"{\\"bucket\\": ${bucket}, \\"object\\": ${object}, \\"key1\\": ${x:key1}}"'}, "callbackBodyType" : "application/json" }`; // callback 的值需要进行 base64 编码 // 配置相关变量参数 const callbackVar = ` { "x:key1" : "ceshi" }`;// callbackVar 的值需要进行 base64 编码 const { requestId: putObjectRequestId } = await client.putObject({ bucket: bucketName, key: objectKey, body: Buffer.from('object content'), callback: Buffer.from(callback).toString('base64'), callbackVar: Buffer.from(callbackVar).toString('base64'), }); console.log('putObject Request ID: ', putObjectRequestId); } catch (error) { handleError(error); } } main();
// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入 import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk'; import fsp from 'fs/promises'; // 创建客户端 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 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); } } async function main() { try { const bucketName = 'node-sdk-test-bucket'; // 本地 example_dir 文件夹下的 multipartExample 文件 const filePath = './example_dir/multipartExample'; const callbackUrl = '*** Provide your callback url ***'; const objectKey = '*** Provide your object key ***'; // 设置上传回调参数 // 可以根据需求设置 callback 参数 const callback = ` { "callbackUrl" : "${callbackUrl}", "callbackBody" : ${'"{\\"bucket\\": ${bucket}, \\"object\\": ${object}, \\"key1\\": ${x:key1}}"'}, "callbackBodyType" : "application/json" }`; // callback 的值需要进行 base64 编码 // 配置相关变量参数 const callbackVar = ` { "x:key1" : "ceshi" }`; // callbackVar 的值需要进行 base64 编码 // 获取上传任务 ID const { data: { UploadId }, } = await client.createMultipartUpload({ bucket: bucketName, key: objectKey, }); console.log('UploadId', UploadId); // 获取本地文件信息并准备进行分片 const stats = await fsp.stat(filePath); // 文件总大小 const totalSize = stats.size; let offset = 0; let partNumber = 1; const partsInfo = []; // 上传时指定分片大小 const partSize = 5 * 1024 * 1024; while (offset < totalSize) { const uploadResult = await client.uploadPartFromFile({ bucket: bucketName, key: objectKey, filePath, partNumber, uploadId: UploadId, offset, partSize, }); 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, requestId: completeMultipartUploadRequestId } = await client.completeMultipartUpload({ bucket: bucketName, key: objectKey, uploadId: UploadId, parts: partsInfo, callback: Buffer.from(callback).toString('base64'), callbackVar: Buffer.from(callbackVar).toString('base64'), }); console.log('completeMultipartUpload Request ID: ', completeMultipartUploadRequestId); console.log('result data:', data); } catch (error) { handleError(error); } } main();