【问题标题】:How to stop aws-amplify from appending level and identityPoolId in the url while performing S3 operations如何在执行 S3 操作时停止 aws-amplify 在 url 中附加 level 和 identityPoolId
【发布时间】:2020-01-13 04:45:04
【问题描述】:

按照aws-amplify docs 和中等post 中的文章,我已经设置了托管在S3 上的React js 网站,并使用Cognito 进行身份验证。一切都按照记录进行,但我一直在自定义 URL 以在 S3 存储桶(不是用于托管我的网站的存储桶)上执行操作

我的 S3 存储桶中的对象不在级别(公共、私有、受保护)之下,也不在 identityPoolId 密钥之下。 例如,我的存储桶中的典型对象键如下所示

project/run/ad33dff21f3g53/result.txt

但是,下面是我的应用程序中的示例代码

componentDidMount() {
    Amplify.configure({
      Auth: {
        identityPoolId: awsconfig.aws_cognito_identity_pool_id,
        region: awsconfig.aws_cognito_region,
        userPoolId: awsconfig.aws_user_pools_id,
        userPoolWebClientId: awsconfig.aws_user_pools_web_client_id
      },
      Storage: {
        bucket: awsconfig.my_results_bucket,
        region: awsconfig.aws_cognito_region,
        identityPoolId: awsconfig.aws_cognito_identity_pool_id
      }
    });
    SetS3Config(awsconfig.my_results_bucket, "private");
    Storage.get("project/run/ad33dff21f3g53/result.txt", {
      download: true,
      level: "private"
    })
      .then(result => {
        console.log(result.Body.toString());
        this.setState({ content: result.Body.toString() });
      })
      .catch(err => console.log(err));
  }

export function SetS3Config(bucket, level) {
  Storage.configure({
    bucket: bucket,
    level: level,
    region: awsconfig.aws_cognito_region,
    identityPoolId: awsconfig.aws_cognito_identity_pool_id
  });
}

不起作用,正如我在浏览器的网络选项卡中看到的那样,对 s3 存储桶 API 的请求按以下模式命中

https://my_results_bucket.s3.eu-west-2.amazonaws.com/private/eu-west-2%3A0e197abd3-004e-4af8-ba56-32ff0d534f67/project/run/ad33dff21f3g53/result.txt

这会引发 404 错误,因为我在指定的键下没有我的对象。

有什么方法可以阻止 aws amplify 将 levelidentityPoolId 添加到 URL 中?

PS: 如果我将对象放置在上述 URL 中所述的确切位置,它就会起作用。但是,我不能这样做,因为我的 S3 存储桶中的键有很多依赖项。

【问题讨论】:

    标签: reactjs amazon-web-services amazon-s3 amazon-cognito aws-amplify


    【解决方案1】:

    据我了解,恐怕这不适用于 aws-amplify。 目前,您最多可以为每个级别设置 customPrefixes:

    const customPrefix = {
        public: 'myPublicPrefix/',
        protected: 'myProtectedPrefix/',
        private: 'myPrivatePrefix/'
    };
    
    Storage.put('test.txt', 'Hello', {
        customPrefix: customPrefix,
        level: \\ one of private, protected or public
    })
    .then (result => console.log(result))
    .catch(err => console.log(err));
    

    我恢复的是使用 aws-sdk 来执行复杂的操作:

    const credentials = await Auth.currentCredentials();
    
    const essentials = await  Auth.essentialCredentials(credentials)
    console.log(essentials)
    var s3 = new AWS.S3({
        apiVersion: '2006-03-01', 
        region: YOUR_REGION, 
        credentials:Auth.essentialCredentials(credentials)
    });
    
    var params = {Bucket: YOUR_BUCKET , Key: YOUR_OBJECT_PATH};
    var url = s3.getSignedUrl('getObject', params);
    console.log('The URL is', url);
    

    这需要将策略附加到您的用户角色,以授予他执行所需操作的适当权限,因此请前往 IAM 并创建一个策略:

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Action": [
                    "s3:PutObject",
                    "s3:GetObject",
                    "s3:DeleteObject"
                ],
                "Resource": [
                    "arn:aws:s3:::YOUR_BUCKET/YOUR_OBJECT_PATH"
                ],
                "Effect": "Allow"
            }
        ]
    }
    

    将此策略附加到需要访问权限的 auth/unauth 角色,您应该一切顺利。希望我有所帮助

    【讨论】:

      猜你喜欢
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多