tatsuya kanemoto
tatsuya kanemoto

Reputation: 2028

How to partition amplify S3 storage by prefixing bucket

At first, I want to setup multiple S3 storage in my amplify project.
But this is not allowed for now (amplify-cli shows me Amazon S3 storage was already added to your project.)

And I found the possible solution for my use-case by creating partitions.
This is mentioned in the link below.

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

This says like follows.

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.

But it didn't explain how to setup partitions and how to use it.
So, could anyone explain how to do it?

Upvotes: 1

Views: 1696

Answers (1)

Johannes Konings
Johannes Konings

Reputation: 188

In the folder amplify/backend/storage/s3-cloudformation-template.json you can add a new policy for your new prefix, which will be the foldername in the s3 bucket

        "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

Now you can use e.g. the custom prefix "storage1" to store your files in a storage1 folder.

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

};

Do the same with another prefix (in this example storage 2) than you can store files from another use case in another folder.

s3 bucket with two custom prefixes

Upvotes: 1

Related Questions