【发布时间】:2017-11-17 07:33:50
【问题描述】:
大家好,我正在尝试使用节点 js 从我的 s3 存储桶中删除对象,这是我将执行的服务器端脚本
var aws = require('aws-sdk');
var s33 = new aws.S3();
aws.config.update({
accessKeyId: "xxxxxxxxxxxxxxxxxxxxxx",
secretAccessKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
bucket : "xxxxxxxxxxxx",
region: "us-east-1"
});
app.post('/deleteObj',function(req, res) {
var params = {
Bucket: 'xxxxxxxxxxxxx',
Delete: { // required
Objects: [ // required
{
Key: 'some-key' // required
},
/*{
Key: 'sample-image--10.jpg'
}*/
],
},
};
s33.deleteObjects(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
});
它显示了一个不应该发生的缺少凭据的错误,因为我只能使用这些凭据进行基于浏览器的 POST 上传。
这是错误日志:
CredentialsError: Missing credentials in config
at IncomingMessage.<anonymous> (/home/ubuntu/xxxxxx/node_modules/aws-sdk/lib/util.js:864:34)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
message: 'Missing credentials in config',
retryable: false,
time: 2017-11-17T06:43:51.657Z,
code: 'CredentialsError',
originalError:
{ message: 'Could not load credentials from any providers',
retryable: false,
time: 2017-11-17T06:43:51.657Z,
这里是存储桶的 cors 配置,如果有帮助,请查看:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>http://xxxxxxxxxxxxxxxxxxxxxxxxxxxx</AllowedOrigin>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
让我知道你对此的看法。
-谢谢
【问题讨论】:
-
现在我仔细看,我认为我的答案不正确。为什么要尝试使用 POST 请求删除项目?
-
这只是为了检查它们是否被删除......当我完成写这篇文章时,看起来如果这个人已经上传了图像,那么他将能够在发布请求的关键帮助下删除它....
-
浏览器使用CORS。浏览器应该调用的唯一方法是 GET。删除 POST、PUT 和 DELETE(如果存在),除非有非常具体的原因允许这些方法。注意:在 AllowedHeader 中,从“Authorization”更改为“*” - 如果您实际使用授权并包括“Authorization:”HTTP 标头,则例外。
标签: node.js amazon-web-services amazon-s3 aws-sdk