Skip to content

Instantly share code, notes, and snippets.

@bbsmithy
Last active October 23, 2018 20:10
Show Gist options
  • Save bbsmithy/e7a8cb8a71b2119aeafaf4f940b5c405 to your computer and use it in GitHub Desktop.
Save bbsmithy/e7a8cb8a71b2119aeafaf4f940b5c405 to your computer and use it in GitHub Desktop.
Python image compression for AWS lamda, with S3 triggers
from __future__ import print_function
import boto3
from botocore.client import Config
import os
import sys
import uuid
from PIL import Image
import PIL.Image
ACCESS_KEY_ID = //
ACCESS_SECRET_KEY = //
# S3 Connect
s3 = boto3.resource(
's3',
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=ACCESS_SECRET_KEY
)
def resize_image(image_path, resized_path):
with Image.open(image_path) as image:
image.thumbnail(tuple(x / 2 for x in image.size))
image.save(resized_path)
def lamba_handler(event, context):
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
upload_path = '/tmp/{}'.format(key)
resized_path = '/tmp/resized-{}'.format(key)
s3.Bucket(bucket).download_file(key, upload_path)
resize_image(upload_path, resized_path)
s3.meta.client.upload_file(resized_path, BUCKET_NAME, key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment