【问题标题】:S3 Access Denied when specify the principal指定主体时 S3 访问被拒绝
【发布时间】:2018-06-11 08:14:18
【问题描述】:

存储桶策略

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::123456784337:root",
                    "arn:aws:iam::123456784337:user/lambda-user"
                ]
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::mybucket/*"
        }
    ]
   }

初始化

 AWS.config.update({
      region: 'ap-southeast-1',
      accessKey: 'abcxxxx',
      secretAccessKey:'abcdxxx'

    });
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
     IdentityPoolId: 'ap-southeast-1:12340000-5587-4d40-91fe-9fab5668c708'
    });

S3 获取对象

function (bucketName, key) {
  const params = {
    Bucket: bucketName,
    Key: key,
  };
  return s3.getObject(params).promise()
    .then((data) => {
      console.log('Successfully read from S3!');
      return data;
    });
};

Connito 用户未认证

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "lambda:InvokeFunction",
                "mobileanalytics:PutEvents",
                "dynamodb:Scan",
                "lambda:InvokeAsync",
                "cognito-sync:*"
            ],
            "Resource": "*"
        }
    ]
}

未能读取到 S3。 AccessDenied:拒绝访问 (节点:73168)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:1):AccessDenied:访问被拒绝

仅当 Principal 为通配符时才有效,但不建议使用该配置。

【问题讨论】:

    标签: amazon-web-services amazon-s3 access-denied principal


    【解决方案1】:

    这是从 S3 读取文件的策略。

    {
        "Id": "Policy1528709447655",
        "Version": "2012-10-17",
        "Statement": [{
            "Sid": "Stmt1528709412334",
            "Action": [
                "s3:GetBucketPolicy",
                "s3:GetObject",
                "s3:GetObjectTagging",
                "s3:GetObjectAcl"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:s3:::bucket_name",
                "arn:aws:s3:::bucket_name/*"
            ],
            "Principal": {
                "AWS": [
                    "arn:aws:iam::123456784337:root",
                    "arn:aws:iam::487686674337:user/lambda-user"
                ]
            }
        }]
    }
    

    【讨论】:

    • 谢谢,但出现错误:缺少必需的主体
    • 在刚刚将策略赋予角色时,我们需要在其中添加主体。 :) Anwer 已更新。
    • 仍然错误无法读取到 S3。 AccessDenied: Access Denied (node:73208) UnhandledPromiseRejectionWarning: UnhandledPromiseRejectionWarning: Unhandled Promise Rejection (rejection id: 1): AccessDenied: Access Denied 有什么问题吗?我需要在 Cognito 中创建经过身份验证的用户吗?
    • 我知道未经身份验证的策略。我更新了上面的问题用例。谢谢
    • 我刚刚更新了策略,它对我有用(我没有使用 Cognito 进行测试,而是使用 IAM 用户)。所以试试这个。或参考这里:blog.powerupcloud.com/…
    猜你喜欢
    • 2012-09-11
    • 2021-07-27
    • 2017-07-28
    • 2020-09-30
    • 2020-08-07
    • 1970-01-01
    • 2013-08-15
    • 2020-06-30
    • 2018-02-07
    相关资源
    最近更新 更多