Dhanush
Dhanush

Reputation: 110

Make specific S3 bucket folder public with amplify override

make specific s3 folders publicly accessible. For example make public/ accessible to all. protected/ to be private.

Upvotes: 0

Views: 855

Answers (1)

Dhanush
Dhanush

Reputation: 110

  1. Do amplify override storage
  2. The command creates a new overrides.ts file under amplify/backend/storage/<resource-name>/
  3. Add a bucket policy as below in override.ts
import {AmplifyS3ResourceTemplate} from '@aws-amplify/cli-extensibility-helper'

export function override(resources: AmplifyS3ResourceTemplate) {
  resources.addCfnResource(
    {
      type: 'AWS::S3::BucketPolicy',
      properties: {
        Bucket: {
          Ref: 'S3Bucket',
        },
        PolicyDocument: {
          Statement: [
            {
              Action: ['s3:GetObject'],
              Effect: 'Allow',
              Resource: [
                {
                  'Fn::Sub': 'arn:aws:s3:::${S3Bucket}/public/*',
                },
              ],
              Principal: {
                AWS: ['*'],
              },
            },
          ],
        },
      },
    },
    'MyS3BucketPolicy'
  )
}

Upvotes: 1

Related Questions