【问题标题】:Setting up AWS S3 bucket permissions设置 AWS S3 存储桶权限
【发布时间】:2020-01-24 22:21:53
【问题描述】:

我有一个 s3 存储桶(例如 mybucket),目前其权限设置如下:

Block all public access
On
 |
 |_Block public access to buckets and objects granted through new access control lists (ACLs)
 | On
 |
 |_Block public access to buckets and objects granted through any access control lists (ACLs)
 | On
 |
 |_Block public access to buckets and objects granted through new public bucket or access point policies
 | On
 |
 |_Block public and cross-account access to buckets and objects through any public bucket or access point policies
   On

在这个桶里面我有两个文件夹:

 images  (e.g. https://mybucket.s3.us-west-2.amazonaws.com/images/puppy.jpg)
 private (e.g. https://mybucket.s3.us-west-2.amazonaws.com/private/mydoc.doc)

我希望图像文件夹可以公开访问,以便我可以在我的网站上显示图像

我希望私有文件夹受到限制,并且只能使用 IAM 帐户以编程方式访问

如何设置这些权限?我试过关闭上述权限,我也点击了图片 并在操作下,点击“公开”。然后我尝试上传以下内容:

   $image = Image::make($file->getRealPath())->resize(360, 180);
   Storage::disk('s3')->put($filePath, $image->stream());

文件已上传,但当我尝试按如下方式显示图像时出现 403 错误:

   <img src="{{ Storage::disk('s3')->url($file->path) }}" /> 

要下载私人文件,我有以下内容:

    $response =  [
        'Content-Type' => $file->mime_type,
        'Content-Length' => $file->size,
        'Content-Description' => 'File Transfer',
        'Content-Disposition' => "attachment; filename={$file->name}",
        'Content-Transfer-Encoding' => 'binary',
    ];


    return Response::make(Storage::disk('s3')->get($file->path), 200, $response);

设置这些权限的正确方法是什么?

在任何人开始大喊我是 S3 存储新手之前,请耐心等待。

任何帮助和指导将不胜感激。

【问题讨论】:

    标签: amazon-web-services amazon-s3 permissions laravel-6


    【解决方案1】:

    Amazon S3 阻止公共访问是一种存储桶级配置,可防止您将该存储桶中的任何对象设为公开。因此,如果您想公开一个或多个对象,例如images/*,那么你需要为这个bucket禁用S3 Block Public Access

    当然,这本身并不会公开您的任何对象。默认情况下,S3 存储桶是私有的。要将images/ 中的对象公开,您需要配置 S3 存储桶策略,例如:

    {
      "Id": "id101",
      "Statement": [
        {
          "Sid": "publicimages",
          "Action": "s3:GetObject",
          "Effect": "Allow",
          "Resource": "arn:aws:s3:::mybucket/images/*",
          "Principal": "*"
        }
      ]
    }
    

    【讨论】:

    • 感谢您。这似乎有效。如何使用只有 IAM 用户和账户用户才能访问的策略来保护私有文件夹。我是否需要一个,因为您提到公开存储桶并不会公开任何对象?
    • 该存储桶策略允许未经身份验证(即公开)访问images/*,除此之外别无其他。独立地,如果您的 IAM 用户具有包含此存储桶的权限(例如,所有存储桶的 s3:*),那么他们可以独立于此存储桶策略访问该存储桶。
    猜你喜欢
    • 2022-01-03
    • 1970-01-01
    • 2014-12-06
    • 2012-02-22
    • 1970-01-01
    • 2022-12-09
    • 2021-09-06
    • 2017-04-25
    • 2017-01-30
    相关资源
    最近更新 更多