Automation of static website hosting in S3 using boto3

0

Can configuring a bucket to host a static website be automated, from bucket creation to uploading of files, using just boto3?

I managed to create scripts for these steps:

  1. Create a bucket
client = boto3.client('s3')
client.create_bucket(
            Bucket=bucket_name,
            CreateBucketConfiguration={
                'LocationConstraint': self.client.meta.region_name
            },
        )
  1. Enable static website hosting
client.put_bucket_website(
            Bucket=bucket_name,
            WebsiteConfiguration={
                'ErrorDocument': {'Key': 'error.html'},
                'IndexDocument': {'Suffix': 'index.html'},
            },
        )
  1. Change permissions and bucket policy
bucket_policy = {
        'Version': '2012-10-17',
        'Statement': [
            {
                'Sid': 'PublicReadGetObject',
                'Effect': 'Allow',
                'Principal': '*',
                'Action': 's3:GetObject',
                'Resource': BUCKET_ARN,
            }
        ],
    }
bucket_policy = json.dumps(bucket_policy)
client.put_bucket_policy(Bucket=bucket_name, Policy=bucket_policy)
  1. Upload file
    for subdir, dirs, files in os.walk(base_dir):
            for file in files:
                key = file
                if subdir != base_dir:
                    rel_path = os.path.relpath(subdir, base_dir)
                    key = os.path.join(rel_path, file)

                full_path = os.path.join(subdir, file)
                mime_type = mimetypes.guess_type(full_path)[0]

                if mime_type:
                    with open(full_path, 'rb') as data:
                        self.client.put_object(
                            Bucket=bucket_name,
                            Key=key,
                            Body=data,
                            ContentType=mime_type,
                        )

However the website is not hosted properly. For one, when typing the endpoint in the address bar, instead of displaying the page it downloads the content of index.html file. Did I miss any steps here? Are there other things to consider?

Vince
demandé il y a 9 mois323 vues
2 réponses
1

You're almost there! What you're not doing is setting the content type when uploading the file to S3. The AWS CLI does this for you automatically (and silently) but you need to set it when using boto3. Luckily that's really easy:

s3 = boto3.client('s3')

s3.put_object(Bucket='bucketNameHere', Key='index.html', Body=bodyOfFileGoesHere, ContentType='text/html')
profile pictureAWS
EXPERT
répondu il y a 9 mois
profile pictureAWS
EXPERT
vérifié il y a 9 mois
profile picture
EXPERT
Steve_M
vérifié il y a 9 mois
  • Already did this but still not working. I updated my question to include the source code.

  • If you do some debugging - what is the mime_type variable set to? Is it actually text/html? You can also check the content type in the AWS Console by looking at the object metadata.

1

Hi,

You have got an issue with your bucket policy,

'Action': 's3:GetObject',
'Resource': f'{BUCKET_ARN}/*'

With Action with *Object, you must add /* meaning all objects.

profile picture
Donov
répondu il y a 9 mois

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions