如何使用 Javascript 删除 AWS S3 上的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27753411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I delete an object on AWS S3 using Javascript?
提问by user3335960
I want to delete a file from Amazon S3 using Javascript. I have already uploaded the file using Javascript. Any ideas?
我想使用 Javascript 从 Amazon S3 中删除文件。我已经使用 Javascript 上传了文件。有任何想法吗?
回答by jlalcazar
You can use the JS method from S3:
var AWS = require('aws-sdk');
AWS.config.loadFromPath('./credentials-ehl.json');
var s3 = new AWS.S3();
var params = { Bucket: 'your bucket', Key: 'your object' };
s3.deleteObject(params, function(err, data) {
if (err) console.log(err, err.stack); // error
else console.log(); // deleted
});
Be aware that S3 never returns it the object has been deleted. You have to check it before or after with getobject, headobject, waitfor, etc
请注意,S3 永远不会返回对象已被删除。您必须在使用 getobject、headobject、waitfor 等之前或之后检查它
回答by Vitaliy Andrusishyn
You can use construction like this:
您可以使用这样的构造:
var params = {
Bucket: 'yourBucketName',
Key: 'fileName'
/*
where value for 'Key' equals 'pathName1/pathName2/.../pathNameN/fileName.ext'
- full path name to your file without '/' at the beginning
*/
};
s3.deleteObject(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
And don't forget to wrap it to the Promise.
并且不要忘记将它包装到Promise。
回答by Aniket Thakur
You can use deleteObjectsAPI to delete multiple objects at once instead of calling API for each key to delete. Helps save time and network bandwidth.
您可以使用deleteObjectsAPI 一次删除多个对象,而不是为每个要删除的键调用 API。有助于节省时间和网络带宽。
You can do following-
您可以执行以下操作-
var deleteParam = {
Bucket: 'bucket-name',
Delete: {
Objects: [
{Key: 'a.txt'},
{Key: 'b.txt'},
{Key: 'c.txt'}
]
}
};
s3.deleteObjects(deleteParam, function(err, data) {
if (err) console.log(err, err.stack);
else console.log('delete', data);
});
For reference see - https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteObjects-property
如需参考,请参阅 - https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteObjects-property
回答by banoth ravinder
Before deleting the file you have to check the 1) file whether it is in the bucket because If the file is not available in the bucket and using deleteObjectAPI this doesn't throw any error 2)CORS Configurationof the bucket. By using headObjectAPI gives the file status in the bucket.
在删除文件之前,您必须检查 1) 文件是否在存储桶中,因为如果文件在存储桶中不可用并且使用deleteObjectAPI,这不会引发CORS Configuration存储桶的任何错误 2) 。通过使用headObjectAPI 给出存储桶中的文件状态。
AWS.config.update({
accessKeyId: "*****",
secretAccessKey: "****",
region: region,
version: "****"
});
const s3 = new AWS.S3();
const params = {
Bucket: s3BucketName,
Key: "filename" //if any sub folder-> path/of/the/folder.ext
}
try {
await s3.headObject(params).promise()
console.log("File Found in S3")
try {
await s3.deleteObject(params).promise()
console.log("file deleted Successfully")
}
catch (err) {
console.log("ERROR in file Deleting : " + JSON.stringify(err))
}
} catch (err) {
console.log("File not Found ERROR : " + err.code)
}
As params are constant, the best way to use it with const. If the file is not found in the s3 it throws the error NotFound : null.
由于 params 是常量,因此最好将它与const. 如果在 s3 中找不到该文件,则会引发错误NotFound : null。
If you want to apply any operations in the bucket, you have to change the permissions of CORS Configurationin the respective bucket in the AWS. For changing permissions Bucket->permission->CORS Configurationand Add this code.
如果要在存储桶中应用任何操作,则必须更改CORS ConfigurationAWS 中相应存储桶中的权限。用于更改权限Bucket->permission->CORS Configuration并添加此代码。
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
for more information about CROS Configuration : https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html
有关 CROS 配置的更多信息:https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html
回答by iamsohel
You can follow this GitHub gist link https://gist.github.com/jeonghwan-kim/9597478.
你可以关注这个 GitHub 要点链接https://gist.github.com/jeonghwan-kim/9597478。
delete-aws-s3.js:
删除-aws-s3.js:
var aws = require('aws-sdk');
var BUCKET = 'node-sdk-sample-7271';
aws.config.loadFromPath(require('path').join(__dirname, './aws-config.json'));
var s3 = new aws.S3();
var params = {
Bucket: 'node-sdk-sample-7271',
Delete: { // required
Objects: [ // required
{
Key: 'foo.jpg' // required
},
{
Key: 'sample-image--10.jpg'
}
],
},
};
s3.deleteObjects(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});

