当对象不会再使用时,您可以删除对象,降低成本开销。目前 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
进行设置:
quiet
为false
时,是指在返回响应时,不管对象是否删除成功都将删除结果包含在响应里。quiet
为true
时,是指在返回响应时,只返回删除失败的对象结果,没有返回的认为删除成功。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。