Skip to content

Setting up Amazon S3

William Silversmith edited this page Apr 30, 2019 · 5 revisions

Public Bucket Policy

Fill in $BUCKETNAME below.

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "AllowPublicRead",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::$BUCKETNAME/*"
        }
    ]
}

CORS

cors-s3.json

{
    "CORSRules": [
        {
            "AllowedHeaders": [
                "*"
            ], 
            "MaxAgeSeconds": 600, 
            "AllowedMethods": [
                "GET"
            ], 
            "AllowedOrigins": [
                "*"
            ]
        }
    ]
}
aws s3api put-bucket-cors --bucket $BUCKET --cors-configuration file://$HOME/Desktop/cors-s3.json

Using boto3

import boto3

client = boto3.client( 
  's3', 
  aws_access_key_id='ACCESS_KEY_ID', 
  aws_secret_access_key='SECRET_KEY',
  endpoint_url='https://subdomain.domain.tld/', # if using an s3 emulation service
)

client.get_bucket_cors(Bucket='BUCKET_NAME')

client.put_bucket_cors(Bucket='BUCKET_NAME',     
  CORSConfiguration={ 
    'CORSRules': [ 
      { 
        'AllowedHeaders': ['*'], 
        'AllowedMethods': [ 'GET' ], 
        'AllowedOrigins': [ '*' ], 
        'MaxAgeSeconds': 600, 
      }, 
    ] 
  }, 
)