为什么我在s3上的访问被拒绝(使用aws-sdk for Node.js)?

ocebsuys  于 2023-03-08  发布在  Node.js
关注(0)|答案(9)|浏览(114)

我尝试从我的s3存储桶读取现有文件,但我一直得到“拒绝访问”,没有解释或说明如何处理它。Here是我正在使用的代码:

'use strict'

var AWS = require('aws-sdk')
const options = {
  apiVersion: '2006-03-01',
  params: {
    Bucket: process.env['IMAGINATOR_BUCKET']
  },
  accessKeyId: process.env['IMAGINATOR_AWS_ACCESS_KEY_ID'],
  secretAccessKey: process.env['IMAGINATOR_AWS_SECRET_ACCESS_KEY'],
  signatureVersion: 'v4'
}
console.log('options', options)
var s3 = new AWS.S3(options)

module.exports = exports = {
  get (name, cb) {
    const params = {
      Key: name + '.json'
    }
    console.log('get params', params)
    return s3.getObject(params, cb)
  },
  set (name, body, cb) {
    const params = {
      Key: name + '.json',
      Body: body
    }
    console.log('set params', params)
    return s3.putObject(params, cb)
  }
}

这就是我在使用get方法并记录回调中提供的错误时得到的输出(敏感信息被删除):

options { apiVersion: '2006-03-01',
  params: { Bucket: CENSORED_BUT_CORRECT },
  accessKeyId: CENSORED_BUT_CORRECT,
  secretAccessKey: CENSORED_BUT_CORRECT,
  signatureVersion: 'v4' }
get params { Key: 'whitelist.json' }
err { [AccessDenied: Access Denied]
  message: 'Access Denied',
  code: 'AccessDenied',
  region: null,
  time: Wed Sep 21 2016 11:17:50 GMT-0400 (EDT),
  requestId: CENSORED,
  extendedRequestId: CENSORED,
  cfId: undefined,
  statusCode: 403,
  retryable: false,
  retryDelay: 20.084538962692022 }
/Users/shawn/git/vigour-io/imaginate/node_modules/aws-sdk/lib/request.js:31
            throw err;
            ^

AccessDenied: Access Denied
    at Request.extractError (/Users/shawn/git/vigour-io/imaginate/node_modules/aws-sdk/lib/services/s3.js:538:35)
    at Request.callListeners (/Users/shawn/git/vigour-io/imaginate/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
    at Request.emit (/Users/shawn/git/vigour-io/imaginate/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
    at Request.emit (/Users/shawn/git/vigour-io/imaginate/node_modules/aws-sdk/lib/request.js:668:14)
    at Request.transition (/Users/shawn/git/vigour-io/imaginate/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/Users/shawn/git/vigour-io/imaginate/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /Users/shawn/git/vigour-io/imaginate/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/Users/shawn/git/vigour-io/imaginate/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/Users/shawn/git/vigour-io/imaginate/node_modules/aws-sdk/lib/request.js:670:12)
    at Request.callListeners (/Users/shawn/git/vigour-io/imaginate/node_modules/aws-sdk/lib/sequential_executor.js:115:18)

现在我不知道该怎么办,因为我认为我按照文档正确地做了事情,但它不工作,错误消息没有说明为什么我的访问被拒绝...你知道下一步应该做什么来让它工作吗?

pgpifvop

pgpifvop1#

问题是我的新IAM用户没有附加策略,我给它分配了AmazonS3FullAccess策略,现在它可以工作了。
正如评论中所指出的,限制性更强的政策会安全得多

kognpnkq

kognpnkq2#

如果您尝试将ACL设置为“公共读取”,但存储桶阻止公共访问,也可能发生这种情况。例如,如果您打算将静态资产上载到配置错误的S3存储桶。您可以在存储桶设置中更改它。

cgyqldqp

cgyqldqp3#

您的策略中的FullAccess不是必需的。您可以尝试以下操作:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:Put*",
                "s3:Get*",
                "s3:List*",
                "s3:Delete*"
            ],
            "Resource": [
                "arn:aws:s3:::bucket/*",
                "arn:aws:s3:::bucket"
            ]
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": "s3:ListAllMyBuckets",
            "Resource": "*"
        }
    ]
}
fnvucqvd

fnvucqvd4#

这些错误可能发生在您试图读取的对象不存在的情况下。据我所知,AWS错误在这些情况下并不那么清楚。
验证您的键/桶是否正确,以及您是否在API方法上发送了正确的参数。
这个问题我已经遇到两次了:

  • 当我用bucket参数替换key参数时,反之亦然,我试图使用getObject()方法读取s3对象。
  • 当我试图使用copyObject()方法将文件复制到不存在的位置时。
6yt4nkrj

6yt4nkrj5#

Steps

1: click on Users in IAM (in AWS)
2: click on permission tab
3: click on add permission then click on add group
4: search s3fullaccess in searchbar
5: select AmazonS3FullAccess and type any group name then click on create
6: perform action through your API again
7: done
jaql4c8m

jaql4c8m6#

"code":"AccessDenied","region":null,"time":"2020-05-24T05:20:56.219Z","requestId": ...

在“权限”选项卡的s3 aws控制台〉Bucket policy editor 中应用以下策略,以消除上述错误,

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::<IAM-user-ID>:user/testuser"
            },
            "Action": [
                "s3:ListBucket",
                "s3:ListBucketVersions",
                "s3:GetBucketLocation",
                "s3:Get*",
                "s3:Put*"
            ],
            "Resource": "arn:aws:s3:::srcbucket"
        }
    ]
}
aij0ehis

aij0ehis7#

在我的例子中,我更新了.env文件中保存s3存储桶名称的变量,但没有更新程序中的变量,因此程序接收到存储桶名称变量的undefined值,这导致我的程序抛出access denied错误。

因此,如果要在变量中存储存储存储区名称,请确保使用正确的存储区名称或正确的变量名称

x9ybnkn6

x9ybnkn68#

我遇到了同样的错误,原因是我尝试访问的文件不在存储桶中。因此,请确保使用正确的存储桶名称,并且要查找的文件名与该存储桶中存在的文件名完全相同。https://www.diffchecker.com/diff是查找字符串差异的好工具

tvz2xvvm

tvz2xvvm9#

在IAM用户设置中,如果您启用了AmazonS3FullAccess,请确保您不应启用任何其他S3策略。它将覆盖您的AmazonS3FullAccess。并检查您是否收到来自亚马逊的任何电子邮件。

相关问题