You need to enable JavaScript to run this app.
导航
多版本场景(Harmony SDK)
最近更新时间:2024.12.23 17:29:49首次发布时间:2024.12.23 17:29:49

版本控制应用于桶中所有对象。通过开启桶的版本控制,您可以在错误删除或者覆盖对象后,将对象回恢复至任意的历史版本。
目前支持多版本的接口包含 getObject、copyObject、uploadPartCopy、deleteObject 等。调用相关接口时,您可通过可选参数 versionId 指定操作对象的具体版本。

设置桶版本控制状态

TOS 中桶的版本状态包含未开启、开启版本控制和暂停版本控制三种。以下代码展示如何设置桶状态为开启多版本(Enable)状态或暂停版本控制状态(Suspended)。

注意

  • 设置桶多版本状态前,您必须具备 tos:PutBucketVersioning 权限。
  • 查询桶多版本状态前,您必须具备 tos:GetBucketVersioning 权限。
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 res = await client.putBucketVersioning({
      Bucket: bucketName,
      Status: VersioningStatusType.VersioningStatusEnabled,
    });
    console.log('putBucketVersioning Request ID: ', res.RequestId);

    // 关闭多版本
    const res2 = await client.putBucketVersioning({
      Bucket: bucketName,
      Status: VersioningStatusType.VersioningStatusSuspended,
    });
    console.log('putBucketVersioning Request ID: ', res2.RequestId);
  } catch (error) {
    handleError(error);
  }
}

main();

下载多版本对象

以下代码展示如何下载 harmonyos-sdk-test-bucket 桶中的指定文件的指定版本,并在内存中直接读取打印字符串。

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 {
    // 指定的 objectKey
    const objectKey = '*** Provide your object key ***';
    // objectKey 对象的版本 id
    const versionId = '*** Provide object Version ID ***';
    // 下载数据到内存
    const res = await client.getObject({
      Bucket: bucketName,
      Key: objectKey,
      VersionId: versionId,
    });
    const content = buffer.from(res.Content.read(1000)).toString();
    res.Content.close();
    console.log('getObjectV2 Request ID: ', res.RequestId);
    console.log('buffer.length: ', content);
  } catch (error) {
    handleError(error);
  }
}

main();

删除多版本对象

以下代码展示如何删除存储桶中指定对象指定版本。

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 {
    // 指定的 objectKey
    const objectKey = '*** Provide your object key ***';
    // objectKey 对象的版本 id
    const versionId = '*** Provide object Version ID ***';
    // 删除存储桶中指定对象指定版本
    const res  = await client.deleteObject({
      Bucket: bucketName,
      Key: objectKey,
      VersionID: versionId,
    });
    console.log('deleteObject Request ID: ', res.RequestId);
  } catch (error) {
    handleError(error);
  }
}

main();

清空开启多版本的桶

以下代码展示如何清空整个开启多版本桶的示例代码,包括删除所有对象的多个版本、删除删除标记的多个版本、删除所有未合并的对象。

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);
  }
}

async function main() {
  try {
    const bucketName = 'harmonyos-sdk-test-bucket';

    // 删除桶中所有对象
    let keyMarker = '';
    let versionIdMarker = '';
    while (true) {
      const res = await client.listObjectVersions({
        MaxKeys: 1000,
        Bucket: bucketName,
        KeyMarker: keyMarker,
        VersionIDMarker: versionIdMarker,
      });

      // 删除多版本对象
      for (const object of res.Versions) {
        const res2 = await client.deleteObject({
          Bucket: bucketName,
          Key: object.Key,
          VersionID: object.VersionId,
        });
        console.log('deleteObject %s Request ID:', object.Key, res2.RequestId);
      }

      // 删除标记信息
      for (const markers of res.DeleteMarkers) {
        const res2 = await client.deleteObject({
          Bucket: bucketName,
          Key: markers.Key,
          VersionID: markers.VersionId,
        });
        console.log('deleteObject DeleteMarker %s Request ID:', markers.Key, res2.RequestId);
      }
      if (!res.IsTruncated) {
        break;
      }
      keyMarker = res.NextKeyMarker ?? '';
      versionIdMarker = res.NextVersionIdMarker ?? '';
    }

    let keyMarker2 = '';
    let uploadIdMarker = '';
    // 删除所有未合并的对象
    while (true) {
      const res = await client.listMultipartUploads({
        Bucket: bucketName,
        MaxUploads: 1000,
        KeyMarker: keyMarker2,
        UploadIdMarker: uploadIdMarker,
      });
      for (const upload of res.Uploads) {
        const res2 = await client.abortMultipartUpload({
          Bucket: bucketName,
          Key: upload.Key,
          UploadID: upload.UploadId,
        });
        console.log('abortMultipartUpload uploadId: %s, requestId: %s', upload.UploadId, res2.RequestId);
      }
      if (!res.IsTruncated) {
        break;
      }
      keyMarker = res.NextKeyMarker ?? '';
      uploadIdMarker = res.NextUploadIdMarker ?? '';
    }
  } catch (error) {
    handleError(error);
  }
}

main();