Skip to content

Instantly share code, notes, and snippets.

@ajorg
Created April 17, 2021 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajorg/d44fa14fc7a72f5c78878459e0cdb778 to your computer and use it in GitHub Desktop.
Save ajorg/d44fa14fc7a72f5c78878459e0cdb778 to your computer and use it in GitHub Desktop.
Copies index.html to / in S3
# 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