AWS S3:使用Cognito中的id_token进行私有访问,比如ApiGateway:是否可以?

时间:2017-09-27 10:29:09

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

我使用Cognito中的token_id成功设置了对Lambda的访问权限 - 客户端添加了标头Authorization: <token_id>,Api Gateway验证了此令牌。我希望我可以从客户端浏览器设置对S3的类似访问。为此,我为S3存储桶编写了策略(每个用户都有自己的文件目录):

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Federated": "cognito-identity.amazonaws.com"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::buc/${cognito-identity.amazonaws.com:sub}/*"
        }
    ]
}

我试过这个电话:

curl -v --header 'Authorization: [id_token_from_cognito_after_signup]' https://s3.amazonaws.com/buc/<sub>/myfile.jpg

返回400 Authorization header is invalid -- one and only one ' ' (space) required。在网上我发现这个错误可能与空格无关 - 因为这个请求有正确的空格量(我试过请求没有空格)。

有趣的是,documentation关于S3并未提及Cognito。

我不想在客户端上使用js sdk来提高速度和简单性。

2 个答案:

答案 0 :(得分:0)

您可以使用cognito令牌访问您的API,因为在GUI网关中集成了cognito,如aws blog

中所述。
  

Cognito用户池与API网关的集成提供了一种新方法   保护您的API工作负载

然而,截至今天,S3没有这种可能性。您需要自己计算签名,因为您在引用的文档中对此进行了解释。 (各种示例均引用here

答案 1 :(得分:-1)

您是否尝试过提及here

的政策
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:ListBucket"],
      "Resource": ["arn:aws:s3:::<BUCKET-NAME>"],
      "Condition": {"StringLike": {"s3:prefix": ["cognito/<APPLICATION-NAME>/"]}}
    },
    {
      "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}/*"
      ]
    }
  ]
}