You need to enable JavaScript to run this app.
导航
移动及重命名场景(Harmony SDK)
最近更新时间:2024.12.23 17:29:49首次发布时间:2024.12.23 17:29:49

如果您需要在 Bucket 中对文件进行重命名,您可以通过 CopyObject 接口将源对象拷贝至目标对象,然后通过 DeleteObject 接口删除源对象。

重命名单个对象

以下代码用于将存储桶中对象 src-object 重命名为 dest-object

import { TosClient, TosClientError, TosServerError } from '@volcengine/tos_harmonyos_sdk';

// 创建客户端
const client = new TosClient({
  accessKeyId: "Provide your ak", 
  accessKeySecret: "Provide your sk", 
  securityToken: 'Provide your securityToken',
  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 = 'harmonyos-sdk-test-bucket';
async function main() {
  try {
    const srcObjectKey = 'src-object';
    const dstObjectKey = 'dest-object';

    // 将源对象复制到目标对象
    const res = await client.copyObject({
      Bucket: bucketName,
      Key: dstObjectKey,
      SrcBucket: bucketName,
      SrcKey: srcObjectKey,
    });
    console.log('copyObject Request ID:', res.RequestId);

    // 删除源对象
    const res2 = await client.deleteObject({
      Bucket: bucketName,
      Key: srcObjectKey,
    });
    console.log('deleteObject Request ID:', res2.RequestId);
  } catch (error) {
    handleError(error);
  }
}

main();

重命名目录

以下代码用于将指定存储桶中目录 exampledir1/ 重命名为 exampledir2/

import { TosClient, TosClientError, TosServerError } from '@volcengine/tos_harmonyos_sdk';

// 创建客户端
const client = new TosClient({
  accessKeyId: "Provide your ak", 
  accessKeySecret: "Provide your sk", 
  securityToken: 'Provide your securityToken',
  region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,"Provide your region" 填写为 cn-beijing。
  endpoint: "Provide your endpoint", // 填写域名地址
});

async function main() {
  try {
    const srcDir = 'exampledir1/';
    const dstDir = 'exampledir2/';
    let continuationToken = '';

    while (true) {
      const res = await client.listObjectsType2({
        Bucket: bucketName,
        MaxKeys: 1000,
        ContinuationToken: continuationToken,
        Prefix: srcDir,
      });

      for (const obj of res.Contents) {
        // 将源对象复制到目标对象
        const res2 = await client.copyObject({
          Bucket: bucketName,
          Key: obj.Key.replace(srcDir, dstDir),
          SrcBucket: bucketName,
          SrcKey: obj.Key,
        });
        console.log('copyObject %s Request ID:', obj.Key, res2.RequestId);
        // 删除源对象
        const res3 = await client.deleteObject({
          Bucket: bucketName,
          Key: obj.Key,
        });
        console.log('deleteObject %s Request ID:', obj.Key, res3.RequestId);
      }
      if (!res.IsTruncated) {
        break;
      }
      continuationToken = res.NextContinuationToken ?? '';
    }
  } catch (error) {
    handleError(error);
  }
}

main();