You need to enable JavaScript to run this app.
导航
删除对象(Harmony SDK)
最近更新时间:2024.12.23 17:29:49首次发布时间:2024.12.23 17:29:49

当对象不会再使用时,您可以删除对象,降低成本开销。目前 TOS Harmony SDK 支持删除桶中的单个对象、删除指定的多个对象,对于开启多版本的桶,你可指定删除对象的任意版本数据。

注意事项

  • 删除对象前,您必须具有 tos:DeleteObject 权限,具体操作,请参见权限配置指南
  • 对于开启多版本的桶,删除指定版本对象前,您必须具有 tos:DeleteObjectVersion 权限,具体操作,请参见权限配置指南

示例代码

删除指定对象

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 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 bucketName = 'harmony-sdk-test-bucket';
    // 指定的 objectKey
    const objectKey = '*** Provide your object key ***';
    // 删除对象
    const res = await client.deleteObject({
      Bucket: bucketName,
      Key: objectKey,
    });
    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 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 bucketName = 'harmony-sdk-test-bucket';
    // 指定的 objectKey
    const objectKey = '*** Provide your object key ***';
    // objectKey 对应的对象版本号
    const versionId = `*** Provide objectKey's 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();

批量删除指定对象

注意

在开启版本控制的桶中,在调用 DeleteMultiObjects 接口来批量删除对象时,如果在Delete请求中未指定versionId,将插入删除标记。如果指定了versionId,将永久删除该对象的指定版本。
批量删除对象支持的响应方式可以通过quiet进行设置:

  • quietfalse时,是指在返回响应时,不管对象是否删除成功都将删除结果包含在响应里。
  • quiettrue时,是指在返回响应时,只返回删除失败的对象结果,没有返回的认为删除成功。
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 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 bucketName = 'node-sdk-test-bucket';
    // 指定的 objectKey
    const objectKey1 = '*** Provide your object key ***';
    const objectKey2 = '*** Provide your object key ***';

    // 批量删除对象
    const res = await client.deleteMultiObjects({
      Bucket: bucketName,
      Objects: [new ObjectTobeDeleted(objectKey1), new ObjectTobeDeleted(objectKey2)],
    });
    console.log('deleteMultiObjects Request ID:', res.RequestId);
    console.log('deleteMultiObjects Deleted.length:', res.Deleted.length);
    for (const item of res.Deleted) {
      console.log('deleted item Key:', item.Key);
      console.log('deleted item VersionId:', item.VersionId);
      console.log('deleted item DeleteMarker:', item.DeleteMarker);
      console.log('deleted item DeleteMarkerVersionId:', item.DeleteMarkerVersionId);
    }
  } 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 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 bucketName = 'node-sdk-test-bucket';
    // 指定的 objectKey
    const objectKey1 = '*** Provide your object key ***';
    const objectKey2 = '*** Provide your object key ***';
    // 指定的版本号
    const versionId1 = `*** Provide objectKey1's version id ***`;
    const versionId2 = `*** Provide objectKey2's version id ***`;

    // 批量删除对象
    const res = await client.deleteMultiObjects({
      Bucket: bucketName,
      Objects: [new ObjectTobeDeleted(objectKey1, versionId1), new ObjectTobeDeleted(objectKey2, versionId2)],
    });
    console.log('deleteMultiObjects Request ID:', res.RequestId);
    console.log('deleteMultiObjects Deleted.length:', res.Deleted.length);
    for (const item of res.Deleted) {
      console.log('deleted item Key:', item.Key);
      console.log('deleted item VersionId:', item.VersionId);
      console.log('deleted item DeleteMarker:', item.DeleteMarker);
      console.log('deleted item DeleteMarkerVersionId:', item.DeleteMarkerVersionId);
    }
  } catch (error) {
    handleError(error);
  }
}

main();

相关文档

关于删除对象的 API 文档,请参见 DeleteObject