【问题标题】:How to allow downloading files from S3 only to AWS Cognito users?如何只允许将文件从 S3 下载到 AWS Cognito 用户?
【发布时间】:2020-04-01 04:38:16
【问题描述】:

在我的 AWS 项目中,我使用 Cognito 来允许经过身份验证和未经身份验证的用户使用我的 Android 应用程序。我还使用 S3 存储用户可以从应用下载的文件。

这是我的 S3 存储桶的内容:

myS3Bucket:
   - folderAll
   - folderUserGroup

在该存储桶中,folderAll 包含我的应用程序的每个用户都可以下载的文件,folderUserGroup 包含只能由特定 Cognito 用户组中经过身份验证的用户下载的文件。

folderAllfolderUserGroup 的文件均不得在应用程序外部下载,例如使用网络浏览器。

我已经配置了一个 Cognito 用户池和身份池,其中包含经过身份验证和未经身份验证的用户的角色,例如经过身份验证的用户角色:

  MyIdentityPoolRoleAuthenticated:
    Type: AWS::IAM::Role
    Properties:
      RoleName: my-identity-pool-authenticated
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: 'Allow'
            Principal:
              Federated: 'cognito-identity.amazonaws.com'
            Action:
              - 'sts:AssumeRoleWithWebIdentity'
            Condition:
              StringEquals:
                'cognito-identity.amazonaws.com:aud':
                  Ref: MyIdentityPool
              'ForAnyValue:StringLike':
                'cognito-identity.amazonaws.com:amr': authenticated
      Policies:
        - PolicyName: 'MyIdentityPoolRoleAuthenticatedPolicy'
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: 'Allow'
                Action:
                  - 'mobileanalytics:PutEvents'
                  - 'cognito-sync:*'
                  - 'cognito-identity:*'
                Resource: '*'
              - Effect: "Allow"
                Action:
                  - "s3:GetObject"
                Resource: "arn:aws:s3:::myS3Bucket/*"

所以我尝试关注thisthis,但我不明白如何配置 S3 和 Cognito 角色和策略来满足我的需求。

如何配置我的角色和策略以将文件下载仅限于我的 Cognito 用户?

感谢您的帮助。

【问题讨论】:

  • 到目前为止你做了多少?
  • @ArunmainthanKamalanathan 我已经设置了一个完整的环境,其中包含一个完全正常工作的 Cognito 用户池和身份池,以及一个 S3 存储桶。我唯一不明白的是如何设置 IAM 角色和策略来满足我的需求。
  • @ArunmainthanKamalanathan 我刚刚更新了我的问题,但仍然出现 403 错误。
  • 你还没有发布你的代码。您只发布了政策。试图访问文件的代码在哪里

标签: amazon-web-services amazon-s3 amazon-cognito amazon-iam


【解决方案1】:

为了只允许您的 AWS Cognito 池用户访问您的存储桶,您可以创建如下所示的策略。您只需替换自己的 bucket-nameapplication-name

{
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "ListYourObjects",
        "Effect": "Allow",
        "Action": "s3:ListBucket",
        "Resource": ["arn:aws:s3:::bucket-name"],
        "Condition": {
            "StringLike": {
                "s3:prefix": ["cognito/application-name/${cognito-identity.amazonaws.com:sub}"]
            }
        }
    },
    {
        "Sid": "ReadWriteDeleteYourObjects",
        "Effect": "Allow",
        "Action": [
            "s3:GetObject",
            "s3:PutObject",
            "s3:DeleteObject"
        ],
        "Resource": [
            "arn:aws:s3:::bucket-name/cognito/application-name/${cognito-identity.amazonaws.com:sub}",
            "arn:aws:s3:::bucket-name/cognito/application-name/${cognito-identity.amazonaws.com:sub}/*"
        ]
    }
]

}

参考

  1. https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_s3_cognito-bucket.html

【讨论】:

  • application-name 是什么?我不记得使用术语application name 或在创建我的aws 应用程序时使用它。我有一个公司名称、api 名称和我的应用程序标识的一堆其他东西。你在哪里找到的?
  • application-name 这里指的是 cognito 的用户池应用客户端。你可以在这里找到它:cognito -> manage cognito pools -> your cognito pool -> App clients
【解决方案2】:

我不太记得您是如何直接使用 S3 策略/IAM 进行此操作的,但我喜欢通过 AWS API Gateway 路由所有经过身份验证的流量,因为它与 Cognito 有很好的集成。

然后,您可以通过 API Gateway 设置 S3 代理(在此加载大量 google 资源)并告诉 API Gateway 仅允许经过身份验证的用户查看 s3 内容。您在 api 网关中使用“授权人”来执行此操作。无需代码。直接与 cognito 集成。

我一直认为这是让用户访问受限内容的最佳实践方式。我认为有一些方法可以通过 Congnito 赋予实际的 IAM 角色,但这总是让我感到不舒服。对我来说,Cognito 的用例是将你的“用户”集中在一个地方。我喜欢让 API Gateway 负责与 Cognito 集成并控制对受限区域的访问——这对我来说是最明显的方式。

解决方案过于复杂,无法在此处提供完整的解决方案。 AWS 在Expose API Methods to Access an Amazon S3 Object in a Bucket 上有以下资源以及如何Control Access to a REST API Using Amazon Cognito User Pools as Authorizer

【讨论】:

  • 谢谢,但我的目标是下载文件,而不是调用 Web 服务(不过,我已经为此目的使用 API Gateway)...
  • 如果您已经使用 api 网关,添加另一个资源作为 s3 代理,使用 cognito 身份验证非常简单。让自己的生活更轻松,使用你所拥有的。
  • 谢谢,我稍后再看看。我其实不知道我可以通过 API Gateway 使用 S3。
  • 没有问题。只需 google s3 代理和 api 网关。然后,您需要设置和 api gateway authorizo​​r 以与 cognito 集成。然后它将为您完成所有身份验证。无需代码!
  • @matteoh 查看带有 aws 指南链接的更新答案,如果您需要任何帮助,请大喊
猜你喜欢
  • 2011-11-19
  • 2012-10-08
  • 2021-05-08
  • 2018-05-08
  • 2017-12-19
  • 2014-02-26
  • 2018-07-05
  • 2016-02-02
  • 2020-12-18
相关资源
最近更新 更多