【问题标题】:s3 bucket delete missing credentials errors3 存储桶删除丢失的凭据错误
【发布时间】: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


【解决方案1】:

当您使用 AWS API 时,不涉及 CORS。

您的问题是您在创建 S3 客户端后分配凭据。

将您的代码更改为如下所示:

var aws = require('aws-sdk');

aws.config.update({
    accessKeyId: "xxxxxxxxxxxxxxxxxxxxxx",
    secretAccessKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    bucket : "xxxxxxxxxxxx",
    region: "us-east-1"
});

var s33 = new aws.S3();

【讨论】:

  • 是的,你是对的....但现在错误已更改为 { Deleted: [], Errors: [ { Key: 'xyzzz', Code: 'AccessDenied', Message: 'Access Denied ' } ] }
  • 这意味着凭证(Access Key)没有删除对象(Key:xyzzz)的权限。使用 Amazon 控制台 IAM,查看 IAM 用户策略(或角色,如果将角色分配给实例)并验证您所需的权限。您将需要 s3:DeleteObject。
  • 不客气。请务必阅读我的 cmets 更改 CORS 配置。
猜你喜欢
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
  • 2017-06-08
  • 2010-09-06
  • 2018-09-11
  • 2020-07-14
相关资源
最近更新 更多