使用匿名Cognito身份将文件上传到S3

问题描述 投票:0回答:1

我有一个 iOS 客户端,可以使用匿名 Cognito Identity 和 Amplify SDK 成功将文件上传到 S3。 现在我尝试使用 CLI 模拟相同的上传,但失败得很惨。 这就是我正在做的事情:

  1. 获取给定池 ID 的身份:
        > aws cognito-identity get-id --identity-pool-id us-east-1:XXX-XXX

        {
            "IdentityId": "us-east-1:YYY-YYY"
        }
  1. 获取此身份的凭证:
        > aws cognito-identity get-credentials-for-identity --identity-id  "us-east-1:YYY-YYY" --region "us-east-1"

        {
            "IdentityId": "us-east-1:YYY-YYY",
            "Credentials": {
                "AccessKeyId": "KEY-KEY-KEY",
                "SecretKey": "SECRET-SECRET-SECRET",
                "SessionToken": "long session token here",
                "Expiration": "2030-01-09T01:28:11+02:00"
            }
        }
  1. 尝试使用返回的凭据上传文件:
        > AWS_ACCESS_KEY_ID="KEY-KEY-KEY" AWS_SECRET_ACCESS_KEY="SECRET-SECRET-SECRET" aws s3 --region us-east-1 cp /tmp/txt.txt s3://my-public-bucket/txt.txt

        upload failed: /tmp/txt.txt to s3://my-public-bucket/txt.txt An error occurred (InvalidAccessKeyId) when calling the PutObject operation: The AWS Access Key Id you provided does not exist in our records.

我得到的结果是

InvalidAccessKeyId
The AWS Access Key Id you provided does not exist in our records.
密钥应该在使用前注册吗?我错过了什么?

amazon-web-services amazon-s3 amazon-cognito
1个回答
0
投票

找到了! 它还需要使用会话令牌,而不仅仅是密钥和秘密。所以,最后一条命令改成这样:

> AWS_ACCESS_KEY_ID="KEY-KEY-KEY" \
AWS_SECRET_ACCESS_KEY="SECRET-SECRET-SECRET"\
AWS_SESSION_TOKEN="long session token here"\
aws s3 --region us-east-1 cp /tmp/txt.txt s3://my-public-bucket/txt.txt
© www.soinside.com 2019 - 2024. All rights reserved.