How do you set a default root object for subdirectories for a statically hosted website on Cloudfront?

前端 未结 9 1978
[愿得一人]
[愿得一人] 2020-12-22 17:33

How do you set a default root object for subdirectories on a statically hosted website on Cloudfront? Specifically, I\'d like www.example.com/subdir/index.html

9条回答
  •  别那么骄傲
    2020-12-22 18:26

    @johan-gorter indicated above that CloudFront serves file with keys ending by / After investigation, it appears that this option works, and that one can create this type of files in S3 programatically. Therefore, I wrote a small lambda that is triggered when a file is created on S3, with a suffix index.html or index.htm

    What it does is copying an object dir/subdir/index.html into an object dir/subdir/

    import json
    import boto3
    
    s3_client = boto3.client("s3")
    
    def lambda_handler(event, context):
    
        for f in event['Records']:
    
            bucket_name = f['s3']['bucket']['name']
            key_name = f['s3']['object']['key']
            source_object = {'Bucket': bucket_name, 'Key': key_name}
    
            file_key_name = False
    
            if key_name[-10:].lower() == "index.html" and key_name.lower() != "index.html":
                file_key_name = key_name[0:-10]
            elif key_name[-9:].lower() == "index.htm" and key_name.lower() != "index.htm":
                file_key_name = key_name[0:-9]
            
            if file_key_name:
                s3_client.copy_object(CopySource=source_object, Bucket=bucket_name, Key=file_key_name)
    

提交回复
热议问题