Skip to content

Instantly share code, notes, and snippets.

@koorukuroo
Created April 21, 2026 01:48
Show Gist options
  • Select an option

  • Save koorukuroo/33e506157b4d4cfa9e5c79e86280f547 to your computer and use it in GitHub Desktop.

Select an option

Save koorukuroo/33e506157b4d4cfa9e5c79e86280f547 to your computer and use it in GitHub Desktop.
import json
import boto3
from PIL import Image
import io
import urllib.parse
s3 = boto3.client('s3')
# 원하는 사이즈 설정
MAX_WIDTH = 300
def lambda_handler(event, context):
# S3 이벤트에서 정보 추출
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'])
try:
# 원본 이미지 다운로드
response = s3.get_object(Bucket=bucket, Key=key)
image_content = response['Body'].read()
# Pillow로 이미지 열기
image = Image.open(io.BytesIO(image_content))
# 비율 유지하면서 리사이징
width_percent = (MAX_WIDTH / float(image.size[0]))
height_size = int((float(image.size[1]) * float(width_percent)))
resized_image = image.resize((MAX_WIDTH, height_size))
# 메모리에 저장
buffer = io.BytesIO()
resized_image.save(buffer, format=image.format)
buffer.seek(0)
# 새로운 경로 (예: resized/폴더)
new_key = f"resized/{key}"
# S3 업로드
s3.put_object(
Bucket=bucket,
Key=new_key,
Body=buffer,
ContentType=response['ContentType']
)
return {
'statusCode': 200,
'body': json.dumps('Image resized successfully!')
}
except Exception as e:
print(e)
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment