如果您需要在 Bucket 中对文件进行重命名,您可以通过 CopyObject 接口将源对象拷贝至目标对象,然后通过 DeleteObject 接口删除源对象。
以下代码用于将存储桶中对象 src-object
重命名为 dest-object
。
// 导入 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 srcObjectKey = 'src-object'; const dstObjectKey = 'dest-object'; // 将源对象复制到目标对象 const { requestId } = await client.copyObject({ bucket: bucketName, key: dstObjectKey, srcBucket: bucketName, srcKey: srcObjectKey, }); console.log('copyObject Request ID:', requestId); // 删除源对象 const { requestId: requestId2 } = await client.deleteObject({ bucket: bucketName, key: srcObjectKey, }); console.log('deleteObject Request ID:', requestId2); } catch (error) { handleError(error); } } main();
以下代码用于将指定存储桶中目录 exampledir1/
重命名为 exampledir2/
。
// 导入 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 srcDir = 'exampledir1/'; const dstDir = 'exampledir2/'; for (let truncated = true, continuationToken = ''; truncated; ) { const { data } = await client.listObjectsType2({ bucket: bucketName, maxKeys: 1000, continuationToken: continuationToken, prefix: srcDir, }); for (const obj of data.Contents) { // 将源对象复制到目标对象 const { requestId } = await client.copyObject({ bucket: bucketName, key: obj.Key.replace(srcDir, dstDir), srcBucket: bucketName, srcKey: obj.Key, }); console.log('copyObject %s Request ID:', obj.Key, requestId); // 删除源对象 const { requestId: requestId2 } = await client.deleteObject({ bucket: bucketName, key: obj.Key, }); console.log('deleteObject %s Request ID:', obj.Key, requestId2); } truncated = data.IsTruncated; continuationToken = data.NextContinuationToken; } } catch (error) { handleError(error); } } main();