Skip to content
This repository was archived by the owner on Apr 28, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Created by https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode

### Linux ###
Expand Down Expand Up @@ -242,3 +241,10 @@ $RECYCLE.BIN/
*/build/*

# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode


## serverless/emailImageResizer
serverless/emailImageResizer/.chalice/config.json
serverless/emailImageResizer/.chalice/deployed
serverless/emailImageResizer/.chalice/deployments
serverless/emailImageResizer/requirements.txt
17 changes: 17 additions & 0 deletions serverless/emailImageResizer/.chalice/config.json.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"version": "2.0",
"app_name": "emailImageResizer",
"autogen_policy": false,
"iam_policy_file": "policy.json",
"stages": {
"dev": {
"api_gateway_stage": "api",
"environment_variables": {
"PIXEL_RESIZE_TO": "",
"S3_BUCKET_NAME": "",
"S3_SOURCE_PREFIX": "",
"S3_DESTINATION_PREFIX": ""
}
}
}
}
24 changes: 24 additions & 0 deletions serverless/emailImageResizer/.chalice/policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::*"
]
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
}
17 changes: 17 additions & 0 deletions serverless/emailImageResizer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# emailImageResizer

TODO: Add more detailed information

## Configuration

```sh
$ cp .chalice/config.json.tmpl .chalice/config.json
$ vim .chalice/config.json
```

## Deployment

```sh
$ poetry export -f requirements.txt -o requirements.txt --without-hashes
$ chalice deploy
```
45 changes: 45 additions & 0 deletions serverless/emailImageResizer/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import email
import io
import os

from PIL import Image, ImageFile

import boto3
from chalice import Chalice
from chalice.app import S3Event

S3_BUCKET_NAME = os.environ['S3_BUCKET_NAME']
S3_SOURCE_PREFIX = os.environ['S3_SOURCE_PREFIX']
S3_DESTINATION_PREFIX = os.environ['S3_DESTINATION_PREFIX']
PIXEL_RESIZE_TO = int(os.environ['PIXEL_RESIZE_TO'])
RESIZE_SCALE = PIXEL_RESIZE_TO, PIXEL_RESIZE_TO

ImageFile.LOAD_TRUNCATED_IMAGES = True


app = Chalice(app_name='emailImageResizer')
s3 = boto3.resource('s3')
s3_bucket = s3.Bucket(S3_BUCKET_NAME)


@app.on_s3_event(bucket=S3_BUCKET_NAME, prefix=S3_SOURCE_PREFIX)
def resize_image(event: S3Event) -> None:
content = s3_bucket.Object(event.key).get()
message = email.message_from_bytes(content['Body'].read())

for m in message.get_payload():
if not m.get_content_maintype() == 'image':
continue

src_image = io.BytesIO(m.get_payload(decode=True))
dst_image = io.BytesIO()

with Image.open(src_image) as img:
img.thumbnail(RESIZE_SCALE)
img.save(dst_image, format=img.format)

s3_bucket.put_object(
Key=f'{S3_DESTINATION_PREFIX}{m.get_filename()}',
Body=dst_image.getvalue(),
ContentType=m.get_content_type(),
)
Loading