【问题标题】:Why am I getting "AccessDenied" from S3 DeleteObjects?为什么我从 S3 DeleteObjects 收到“AccessDenied”?
【发布时间】:2020-02-21 07:30:27
【问题描述】:

我正在尝试像这样(Lambda / Node.js)删除 S3 存储桶中的多个对象:

exports.handler =  async function(event, context) {
  const s3Params = {
    Bucket: 'my-bucket',
    Delete: {
      Objects: [ 'my-bucket/dir/file1.json', 'my-bucket/dir/file2.json' ],
      Quiet: False
    }
  }
  const result = await s3.deleteObjects(s3Params).promise()
  return result
}

但我明白了:

{ 
  result: { 
    Deleted: [],
    Errors: [ 
        {Key: "my-bucket/dir/file1.json", Code: "AccessDenied", Message: "Access Denied"},
        {Key: "my-bucket/dir/file1.json", Code: "AccessDenied", Message: "Access Denied"}
    ]
  }
}  

这是我的角色政策:

{
    "RoleName": "S3CleanupRole", 
    "PolicyDocument": {
        "Version": "2012-10-17", 
        "Statement": [
            {
                "Action": [
                    "s3:ListBucket", 
                    "s3:DeleteObject"
                ], 
                "Resource": [
                    "arn:aws:s3:::my-bucket"
                ], 
                "Effect": "Allow"
            }, 
            {
                "Action": [
                    "DynamoDB:Query"
                ], 
                "Resource": [
                    "arn:aws:dynamodb:us-east-1:514141358776:table/buckets-to-clean-out"
                ], 
                "Effect": "Allow"
            }
        ]
    }, 
    "PolicyName": "S3CleanupPolicy"
}

有什么问题?

【问题讨论】:

    标签: amazon-s3


    【解决方案1】:

    (为此感谢Ravi Ramanujam - 他对AWS S3 Access Denied on delete 的回答帮助我解决了这个问题。)

    我的政策是错误的。 s3:DeleteObject 的权限仅适用于存储桶 arn:aws:dynamodb:us-east-1:514141358776:table/buckets-to-clean-out 其任何内容。正确的策略允许 s3:DeleteObject 存储桶内容,即:

    {
        "RoleName": "S3CleanupRole", 
        "PolicyDocument": {
            "Version": "2012-10-17", 
            "Statement": [
                {
                    "Action": [
                        "s3:ListBucket"
                    ], 
                    "Resource": [
                        "arn:aws:s3:::my-bucket"
                    ], 
                    "Effect": "Allow"
                }, 
                {
                    "Action": [
                        "s3:DeleteObject"
                    ], 
                    "Resource": [
                        "arn:aws:s3:::my-bucket/*"
                    ], 
                    "Effect": "Allow"
                }, 
                {
                    "Action": [
                        "DynamoDB:Query"
                    ], 
                    "Resource": [
                         "arn:aws:dynamodb:us-east-1:514141358776:table/buckets-to-clean-out"
                    ], 
                    "Effect": "Allow"
                }
            ]
        }, 
        "PolicyName": "S3CleanupPolicy"
    }
    

    关键部分如下。请注意,“资源”部分包含 arn:aws:s3:::my-bucket/* 而不仅仅是 arn:aws:s3:::my-bucket

     {
         "Action": [
             "s3:DeleteObject"
         ], 
         "Resource": [
              "arn:aws:s3:::my-bucket/*"
         ], 
         "Effect": "Allow"
     } 
    

    【讨论】:

    猜你喜欢
    • 2019-08-14
    • 2010-12-31
    • 2014-12-06
    • 2016-02-10
    • 2017-08-17
    • 2020-09-18
    • 2012-05-27
    • 2020-03-09
    • 2020-04-08
    相关资源
    最近更新 更多