本SDK提供DeleteObjectsCommand
类实现文件删除。完整代码详见 Github 。
DeleteObjectsCommand
调用的 S3 API 为 DeleteObjects, 具体参见DeleteObjects API 文档。
参数说明
Bucket
: 文件所在的存储空间Key
: 文件在存储空间内的名称
示例
执行该示例前请确保配置文件的正确性
以下代码段需要在上下文中运行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const s3 = require('./s3client');
const { DeleteObjectsCommand } = require("@aws-sdk/client-s3");
async function deleteS3Objects(bucketName, objectKeys) {
try {
const deleteParams = {
Bucket: bucketName,
Delete: {
Objects: objectKeys.map(key => ({ Key: key }))
}
};
const command = new DeleteObjectsCommand(deleteParams);
const response = await s3.send(command);
console.log(`Successfully deleted ${response.Deleted.length} objects from S3 bucket. Deleted objects:`);
console.log(response.Deleted.map(d => ` • ${d.Key}`).join("\n"));
} catch (err) {
console.error("Error deleting objects:", err);
}
}
在
Example/
目录中运行以下命令执行该示例
1
$ node DeleteObject.js <bucketName> <objectKey1> <objectKey2> ...