Skip to content

Instantly share code, notes, and snippets.

@Cdddo
Last active May 5, 2021 18:59
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 Cdddo/ed66c5d4f8e66c2a8d4611caec9a2805 to your computer and use it in GitHub Desktop.
Save Cdddo/ed66c5d4f8e66c2a8d4611caec9a2805 to your computer and use it in GitHub Desktop.
AWS Lambda function to fix metadata for Unity WebGL build files
# This function should be invoked from event triggers from objects being created, filtered by the .br or .gz suffix
# Note that for system-defined values like Content-Encoding and Content-Type, we have to assign them directly
# Reference https://stackoverflow.com/a/66009750
import boto3
s3 = boto3.client('s3')
def lambda_handler(event, context):
bucket_name = event['Records'][0]['s3']['bucket']['name']
object_name = event['Records'][0]['s3']['object']['key']
response = s3.head_object(Bucket=bucket_name, Key=object_name)
if object_name.endswith(".br"):
contentEncoding = "br"
elif object_name.endswith(".gz"):
contentEncoding = "gzip"
# else contentEncoding = response['ContentEncoding']
if object_name.endswith(".data.br") or object_name.endswith(".data.gz"):
contentType = "application/octet-stream"
elif object_name.endswith(".framework.js.br") or object_name.endswith(".framework.js.gz"):
contentType = "application/javascript"
elif object_name.endswith(".wasm.br") or object_name.endswith(".wasm.gz"):
contentType = "application/wasm"
elif object_name.endswith("symbols.json.br") or object_name.endswith("symbols.json.gz"):
contentType = "application/octet-stream"
# else contentType = response['ContentType']
# use this for user-defined metadata
# response['Metadata']['metadata-key'] = "value"
result = s3.copy_object(Bucket = bucket_name, Key = object_name,
CopySource = {'Bucket': bucket_name, 'Key': object_name},
# Metadata = response['Metadata'],
MetadataDirective = 'REPLACE', TaggingDirective = 'COPY',
ContentEncoding = contentEncoding,
ContentType = contentType)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment