【问题标题】:How to use ECS credentials with AWS JS SDK如何将 ECS 凭证与 AWS JS 开发工具包一起使用
【发布时间】:2018-01-19 22:42:40
【问题描述】:

我正在尝试使用 AWS JS SDK 访问 S3 存储桶,但没有成功。

我有一个任务定义,它使用一个名为Foo任务角色。此任务角色作为附加的策略访问 S3 存储桶:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Action": [
        "s3:PutObject",
        "s3:GetObject"
      ],
      "Resource": "arn:aws:s3:::foo-bucket"
    }
  ]
}

它在AWS Documentation about loading credentials from IAM roles for EC2 中说我应该将我的实例配置为使用 IAM 角色。但我在 AWS 文档中找不到任何相关内容。

我尝试使用AWS.ECSCredentials class 定义credentials

const options = {
  apiVersion: '2006-03-01',
  region: bucketSettings.region,
  credentials: new AWS.ECSCredentials({
    httpOptions: { timeout: 5000 }, // 5 second timeout
    maxRetries: 10, // retry 10 times
    retryDelayOptions: { base: 200 }, // see AWS.Config for information
  })
};

this.s3Instance = new AWS.S3(options);

当我尝试访问 S3 存储桶中的文件时:

const document = await this.s3Instance
  .getObject({ Bucket: bucketSettings.name, Key: key })
  .promise();

return document;

我还有一个

访问被拒绝

知道我在那里缺少什么吗?

【问题讨论】:

    标签: javascript amazon-s3 aws-sdk amazon-ecs


    【解决方案1】:

    访问 S3 存储桶的策略出错(注意资源末尾的 /*):

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "",
          "Effect": "Allow",
          "Action": [
            "s3:PutObject",
            "s3:GetObject"
          ],
          "Resource": "arn:aws:s3:::foo-bucket/*"
        }
      ]
    }
    

    另外,不需要提供给 AWS 开发工具包的 credentials 选项:

    const options = {
      apiVersion: '2006-03-01',
      region: bucketSettings.region,
    };
    
    this.s3Instance = new AWS.S3(options);
    

    【讨论】:

    • 我正在尝试同样的事情,但是,当我留下凭据时,我收到一条错误消息“缺少凭据”,如果我添加凭据,我会收到错误消息“ECSCredentials 不是构造函数”。有什么建议吗?
    猜你喜欢
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 2017-05-14
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    相关资源
    最近更新 更多