本文介绍删除对象的常用场景代码。
以下代码用于删除指定目录下 exampledir/
的对象。
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 dir = 'exampledir/'; let continuationToken = ''; while (true) { const res = await client.listObjectsType2({ Bucket: bucketName, MaxKeys: 1000, ContinuationToken: continuationToken, Prefix: dir, }); for (const obj of res.Contents) { const res2 = await client.deleteObject({ Bucket: bucketName, Key: obj.Key, }); console.log('deleteObject %s Request ID:', obj.Key, res2.RequestId); } if (!res.IsTruncated) { break; } continuationToken = res.NextContinuationToken ?? ''; } } 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 { let continuationToken = ''; while (true) { const res = await client.listObjectsType2({ Bucket: bucketName, MaxKeys: 1000, ContinuationToken: continuationToken, }); for (const obj of res.Contents) { const res2 = await client.deleteObject({ Bucket: bucketName, Key: obj.Key, }); console.log('deleteObject %s Request ID:', obj.Key, res2.RequestId); } if (!res.IsTruncated) { break; } continuationToken = res.NextContinuationToken ?? ''; } 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; } keyMarker2 = res.NextKeyMarker ?? ''; uploadIdMarker = res.NextUploadIdMarker ?? ''; } } 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();