Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

give public read and view access to s3 bucket objects using cloudformation template

I am writing a AWS cloudformation template to receive a file inside a s3 bucket from Kinesis Firehose. I have gave public read access to the bucket (bucket is public) but when i access the file inside the bucket using object URL, i get "The XML file does not appear to have any style associated with it" error and it says access denied. However the object (JSON file) is downloadable.

I have given full access to the s3 bucket

Resources:

# Create s3 bucket
MyS3Bucket:
 Type: AWS::S3::Bucket
 Properties:
    BucketName: health-app-buckett
    AccessControl: PublicRead

# Create Role
S3BucketRole:
 Type: 'AWS::IAM::Role'
 Properties:
  AssumeRolePolicyDocument:
    Statement:
      - Effect: Allow
        Principal:
          Service:
            - s3.amazonaws.com
        Action:
          - 'sts:AssumeRole'

#Create policy for bucket
S3BucketPolicies:
 Type: 'AWS::IAM::Policy'
 Properties:
  PolicyName: S3BucketPolicy
  PolicyDocument:
    Statement:
      - Sid: PublicReadForGetBucketObjects
        Effect: Allow
        Action: 's3:GetObject'
        Resource: !Join
          - ''
          - - 'arn:aws:s3:::'
            - !Ref MyS3Bucket
            - /*
  Roles:
    - !Ref S3BucketRole

I want to be able to view the file using Object URL

like image 820
Utsab Avatar asked Dec 05 '25 19:12

Utsab


1 Answers

You need to add PublicAccessBlockConfiguration to your template

MyS3Bucket:
  Type: AWS::S3::Bucket
  Properties:
    BucketName: health-app-bucket
    AccessControl: PublicRead
    PublicAccessBlockConfiguration:
      BlockPublicAcls: false
      BlockPublicPolicy: false
      IgnorePublicAcls: false
      RestrictPublicBuckets: false

When pushing your objects to S3, you'll still need to put them with ACL: public-read.

Note: the AccessControl: PublicRead will grant list permission on your bucket allowing all objects to be found publically.

like image 143
Sonali Jain Avatar answered Dec 08 '25 11:12

Sonali Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!