您如何允许授予 public 对上传到 AWS S3 的对象的读取权限?

How do you allow granting public read access to objects uploaded to AWS S3?

我创建了允许访问我帐户中的单个 S3 存储桶的策略。然后我创建了一个只有这个策略的组和一个属于该组的用户。

用户可以按预期查看、删除文件并将文件上传到存储桶。但是,用户似乎无法授予 public 对上传文件的读取权限

授予public对此对象的读取权限选项被select编辑时,上传失败。

存储桶正在托管一个静态网站,我想让前端开发人员上传文件并制作它们 public。

用户角色的策略如下:

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

这是当 IAM 用户尝试授予 public 对上传文件的访问权限时发生的情况:

代理错误看似无关,但本质上是上传卡住了,没有任何反应。如果他们不 select 授予 public 访问权限 选项,上传会立即完成(尽管也会出现代理错误)。

为了重现您的情况,我执行了以下操作:

  • 使用默认设置创建了一个新的 Amazon S3 存储桶(阻止 Public 访问 = 开启)
  • 创建了一个 IAM 用户(没有附加策略)
  • 创建了一个 IAM 组(没有附加策略)
  • 已将 IAM 用户添加到 IAM 组
  • 将您的策略​​(来自问题)作为 内联策略
  • 附加到 IAM 组(更新存储桶名称)
  • 以新的 IAM 用户身份登录到 Amazon S3 管理控制台

此时,用户收到 Access Denied 错误,因为不允许他们列出所有 Amazon S3 存储桶。因此,控制台无法使用。

相反,我 运行 这个 AWS CLI 命令:

aws s3 cp foo.txt s3://new-bucket/ --acl public-read

结果是:

An error occurred (AccessDenied) when calling the PutObject operation: Access Denied

但是,操作成功了:

aws s3 cp foo.txt s3://new-bucket/

这意味着 --acl 是被拒绝的组件。

然后我转到 阻止 Public 访问 并关闭名为 "Block public access to buckets and objects granted through new access control lists (ACLs)" 的选项。我的设置是:

然后我再次运行这个命令:

aws s3 cp foo.txt s3://new-bucket/ --acl public-read

成功了!

为了验证这一点,我返回到 阻止 Public 访问 并打开所有选项(通过顶部的复选框)。我重新运行命令,它又是拒绝访问确认原因是阻止Public访问设置.

底线:关闭第一个块Public访问设置。

您可以通过 AWS CLI 更新对象的 ACL 来完成

选项 1: 已存储在 Amazon S3 上的对象,您可以 运行 此命令更新 public 读取访问权限的 ACL:

aws s3api put-object-acl --bucket <<S3 Bucket Name>> --key <<object>> --acl public-read

选项 2: 运行 此命令向 AWS 帐户所有者 授予对象的完全控制权,并向其他所有人授予 读取权限:

aws s3api put-object-acl --bucket <<S3 Bucket Name>> --key <<object>> --grant-full-control emailaddress=<<Accountowneremail@emaildomain.com>> --grant-read uri=http://acs.amazonaws.com/groups/global/AllUsers

我发现当 ListAllMyBuckets 未被授予 所有 s3 资源。将以下内容添加到 IAM 策略解决了该问题:

 {
      "Sid": "AccessS3Console",
      "Action": [
        "s3:ListAllMyBuckets"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::*"
    }

我测试的一些操作从控制台失败但从 CLI 成功:

  • 正在重命名对象。控制台显示“错误 - 无法将文件重命名为”。 解决方法:删除并re-uploading 具有新名称的对象。
  • 正在上传对象“授予 public 对该对象的读取权限”。控制台状态栏显示操作卡在“进行中”。 解决方法:在未授予 public 读取权限的情况下上传对象,然后右键单击它并选择“制作 public”。

按照此处的说明操作后,我遇到了这些问题 https://aws.amazon.com/premiumsupport/knowledge-center/s3-console-access-certain-bucket/ 描述了如何限制对单个存储桶的访问(并防止看到帐户中的完整存储桶列表)。 post 没有提到注意事项。

To limit a user's Amazon S3 console access to only a certain bucket or folder (prefix), change the following in the user's AWS Identity and Access Management (IAM) permissions:

  1. Remove permission to the s3:ListAllMyBuckets action.
  2. Add permission to s3:ListBucket only for the bucket or folder that you want the user to access. Note: To allow the user to upload and download objects from the bucket or folder, you must also include s3:PutObject and s3:GetObject.

Warning: After you change these permissions, the user gets an Access Denied error when they access the main Amazon S3 console. The user must access the bucket using a direct console link to the bucket or folder.