当对象不会再使用时,您可以删除对象,降低成本开销。目前 TOS Node.JS SDK 支持删除桶中的单个对象、删除指定的多个对象,对于开启多版本的桶,你可指定删除对象的任意版本数据。
tos:DeleteObject
权限,具体操作,请参见权限配置指南。tos:DeleteObjectVersion
权限,具体操作,请参见权限配置指南。以下代码展示从存储桶中删除指定对象。
// 导入 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); } } async function main() { try { const bucketName = 'node-sdk-test-bucket'; // 指定的 objectKey const objectKey = '*** Provide your object key ***'; // 删除对象 const { requestId } = await client.deleteObject({ bucket: bucketName, key: objectKey, }); console.log('deleteObject Request ID:', requestId); } catch (error) { handleError(error); } } main();
// 导入 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); } } async function main() { try { const bucketName = 'node-sdk-test-bucket'; // 指定的 objectKey const objectKey = '*** Provide your object key ***'; // objectKey 对应的对象版本号 const versionId = `*** Provide objectKey's version id ***`; // 删除对象 const { requestId } = await client.deleteObject({ bucket: bucketName, key: objectKey, versionId, }); console.log('deleteObject Request ID:', requestId); } catch (error) { handleError(error); } } main();
注意
versionId
,将插入删除标记。如果指定了versionId
,将永久删除该对象的指定版本。quiet
进行设置:
quiet
为false
时,是指在返回响应时,不管对象是否删除成功都将删除结果包含在响应里。quiet
为true
时,是指在返回响应时,只返回删除失败的对象结果,没有返回的认为删除成功。// 导入 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); } } async function main() { try { const bucketName = 'node-sdk-test-bucket'; // 指定的 objectKey const objectKey1 = '*** Provide your object key ***'; const objectKey2 = '*** Provide your object key ***'; // 批量删除对象 const { data, requestId } = await client.deleteMultiObjects({ bucket: bucketName, objects: [{ key: objectKey1 }, { key: objectKey2 }], }); console.log('deleteMultiObjects Request ID:', requestId); console.log('deleteMultiObjects Deleted.length:', data.Deleted.length); for (const item of data.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();
以下代码用于删除指定存储桶中指定对象名与版本号的多个对象数据。
// 导入 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); } } 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 { data, requestId } = await client.deleteMultiObjects({ bucket: bucketName, objects: [ { key: objectKey1, versionId: versionId1 }, { key: objectKey2, versionId: versionId2 }, ], }); console.log('deleteMultiObjects Request ID:', requestId); console.log('deleteMultiObjects Deleted.length:', data.Deleted.length); for (const item of data.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();
以下代码展示删除指定 Bucket 中所有对象。
// 导入 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); } } async function main() { try { const bucketName = 'node-sdk-test-bucket'; for (let isTruncated = true, keyMarker = '', versionIdMarker = ''; isTruncated; ) { const { data } = await client.listObjectVersions({ maxKeys: 1000, bucket: bucketName, keyMarker, versionIdMarker, }); // 删除多版本对象 for (const object of data.Versions) { const { requestId } = await client.deleteObject({ bucket: bucketName, key: object.Key, versionId: object.VersionId, }); console.log('deleteObject %s Request ID:', object.Key, requestId); } // 删除标记信息 for (const markers of data.DeleteMarkers) { const { requestId } = await client.deleteObject({ bucket: bucketName, key: markers.Key, versionId: markers.VersionId, }); console.log('deleteObject DeleteMarker %s Request ID:', markers.Key, requestId); } isTruncated = data.IsTruncated; keyMarker = data.NextKeyMarker; versionIdMarker = data.NextVersionIdMarker; } } catch (error) { handleError(error); } } main();
关于删除对象的 API 文档,请参见 DeleteObject。