import boto3
import uuid
from PIL import Image
S3 = boto3.client("s3")
def resize_image(input_path, output_path, size):
with Image.open(input_path) as image:
image.thumbnail(size)
image.save(output_path, image.format)
return image.format
def get_ext(path):
with Image.open(path) as image:
return image.format
def handler(event, context):
print("Started ImageResize lambda")
SIZES = [(1600, 1600), (800, 800), (400, 400), (200, 200), (100, 100), (10, 10)]
for record in event["Records"]:
print("Record: {}".format(record))
bucket = record["s3"]["bucket"]["name"]
key = record["s3"]["object"]["key"]
key_without_upload = key.split('/')[2]
download_path = "/tmp/{}".format(key_without_upload)
S3.download_file(bucket, key, download_path)
print(download_path)
ext = get_ext(download_path)
mime = "image/" + ext
S3.upload_file(
download_path,
bucket,
"images/{0}/{0}-{1}.{2}".format(key_without_upload, "raw", ext.lower()),
ExtraArgs={"ContentType": mime},
)
for size in SIZES:
upload_path = "/tmp/resized-{0}-{1}".format(key_without_upload, size[0])
ext = resize_image(download_path, upload_path, size)
ext = ext.lower()
mime = "image/" + ext
# Upload
S3.upload_file(
upload_path,
bucket,
"images/{0}/{0}-{1}.{2}".format(key_without_upload, size[0], ext),
ExtraArgs={"ContentType": mime},
)
return key
import boto3
import uuid
import os
S3 = boto3.client("s3")
def handler(event, context):
print("Started ProcessPDF lambda")
for record in event["Records"]:
bucket = record["s3"]["bucket"]["name"]
key = record["s3"]["object"]["key"]
key_without_upload = key.split('/')[2]
download_path = "/tmp/{}".format(key_without_upload)
S3.download_file(bucket, key, download_path)
mime = "application/pdf"
# Upload raw
S3.upload_file(
download_path,
bucket,
"pdfs/{0}/{0}.{1}".format(key_without_upload, "pdf"),
ExtraArgs={"ContentType": mime},
)
return key