Created
April 17, 2021 17:21
-
-
Save ajorg/d44fa14fc7a72f5c78878459e0cdb778 to your computer and use it in GitHub Desktop.
Copies index.html to / in S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright Andrew Jorgensen | |
# SPDX-License-Identifier: MIT | |
import json | |
from os import environ | |
import boto3 | |
MAP = json.loads(environ.get("MAP", '{"index.html":"/"}')) | |
CLIENT = boto3.client("s3") | |
def copy_index(record): | |
print(json.dumps(record)) | |
key = record["s3"]["object"]["key"] | |
basename = ("/" + key).rsplit("/", 1)[1] | |
if basename not in MAP: | |
print(f"Skipping {key}, not in {json.dumps(MAP)}") | |
return | |
region = record["awsRegion"] | |
bucket = record["s3"]["bucket"]["name"] | |
index_key = key[: -(len(basename) + 1)] + MAP[basename] | |
print(f"Copying {key} to {index_key}") | |
CLIENT.copy( | |
CopySource={"Bucket": bucket, "Key": key}, | |
Bucket=bucket, | |
Key=index_key, | |
ExtraArgs={"ACL": "public-read"}, | |
) | |
def lambda_handler(event, context): | |
print(json.dumps(MAP)) | |
for record in event["Records"]: | |
copy_index(record) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment