【问题标题】:How to partition amplify S3 storage by prefixing bucket如何通过为存储桶添加前缀来分区放大 S3 存储
【发布时间】:2020-08-01 09:38:59
【问题描述】:

首先,我想在我的放大项目中设置多个 S3 存储。
但这暂时是不允许的(amplify-cli 显示Amazon S3 storage was already added to your project.

我通过创建分区为我的用例找到了可能的解决方案。
这在下面的链接中有所提及。

https://github.com/aws-amplify/amplify-cli/issues/1923#issuecomment-516508923

这样说。

As a best practice, the Amplify Framwork allows you to have multiple prefixes in the bucket as a best practice instead of having multiple buckets.
You could partition your bucket by prefixes like the following:
`mybucket/partition1` and `mybucket/partition2` which can potentially have different auth policies and lambda triggers.

但它没有说明如何设置分区以及如何使用它。
那么,谁能解释一下怎么做?

【问题讨论】:

    标签: amazon-s3 aws-amplify


    【解决方案1】:

    在文件夹 amplify/backend/storage/s3-cloudformation-template.json 中,您可以为新前缀添加新策略,即 s3 存储桶中的文件夹名称

            "S3AuthStorage1Policy": {
                "DependsOn": [
                    "S3Bucket"
                ],
                "Condition": "CreateAuthStorage1",
                "Type": "AWS::IAM::Policy",
                "Properties": {
                    "PolicyName": {
                        "Ref": "s3Storage1Policy"
                    },
                    "Roles": [
                        {
                            "Ref": "authRoleName"
                        }
                    ],
                    "PolicyDocument": {
                        "Version": "2012-10-17",
                        "Statement": [
                            {
                                "Effect": "Allow",
                                "Action": {
                                    "Fn::Split" : [ "," , {
                                        "Ref": "s3PermissionsAuthenticatedStorage1"
                                    } ] 
                                },
                                "Resource": [
                                    {
                                        "Fn::Join": [
                                            "",
                                            [
                                                "arn:aws:s3:::",
                                                {
                                                    "Ref": "S3Bucket"
                                                },
                                                "/storage1/*"
                                            ]
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                }
            },
    

    https://docs.amplify.aws/lib/storage/getting-started/q/platform/js#using-amazon-s3 https://github.com/aws-amplify/amplify-js/issues/332#issuecomment-602606514

    现在您可以使用例如自定义前缀“storage1”将您的文件存储在 storage1 文件夹中。

    Storage.put("storageTest.png", file, {
      contentType: "image/png",
      level: 'public',
      customPrefix: {
        public: "storage1/"
      }
    })
      .then(result => console.log(result))
      .catch(err => console.log(err));
    

    };

    使用另一个前缀(在此示例中为存储 2)执行相同操作,而不是将来自另一个用例的文件存储在另一个文件夹中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 2021-06-08
      • 2020-10-17
      • 2020-02-05
      • 2023-03-31
      • 2020-11-05
      • 2019-03-15
      相关资源
      最近更新 更多