in aws/services/CloudFormation/MacrosExamples/S3Objects/lambda/resource.py [0:0]
def handler(event, context):
print("Received request:", json.dumps(event, indent=4))
request = event["RequestType"]
properties = event["ResourceProperties"]
if "Target" not in properties or all(
prop not in properties for prop in ["Body", "Base64Body", "Source"]
):
return sendResponse(event, context, "FAILED", "Missing required parameters")
target = properties["Target"]
if request in ("Create", "Update"):
if "Body" in properties:
target.update(
{
"Body": properties["Body"],
}
)
s3_client.put_object(**target)
elif "Base64Body" in properties:
try:
body = base64.b64decode(properties["Base64Body"])
except:
return sendResponse(event, context, "FAILED", "Malformed Base64Body")
target.update({"Body": body})
s3_client.put_object(**target)
elif "Source" in properties:
source = properties["Source"]
s3_client.copy_object(
CopySource=source,
Bucket=target["Bucket"],
Key=target["Key"],
MetadataDirective="COPY",
TaggingDirective="COPY",
ACL=target["ACL"],
)
else:
return sendResponse(event, context, "FAILED", "Malformed body")
return sendResponse(event, context, "SUCCESS", "Created")
if request == "Delete":
s3_client.delete_object(
Bucket=target["Bucket"],
Key=target["Key"],
)
return sendResponse(event, context, "SUCCESS", "Deleted")
return sendResponse(event, context, "FAILED", f"Unexpected: {request}")