-
-
Save jasson-33/92af65ea74a8cb8bd16a9e2eb58bf363 to your computer and use it in GitHub Desktop.
Load image from S3 directly into memory as PIL image and write to S3 directly from memory from PIL image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
from PIL import Image | |
from io import BytesIO | |
import os | |
class S3ImagesInvalidExtension(Exception): | |
pass | |
class S3ImagesUploadFailed(Exception): | |
pass | |
class S3Images(object): | |
"""Useage: | |
images = S3Images(aws_access_key_id='fjrn4uun-my-access-key-589gnmrn90', | |
aws_secret_access_key='4f4nvu5tvnd-my-secret-access-key-rjfjnubu34un4tu4', | |
region_name='eu-west-1') | |
im = images.from_s3('my-example-bucket-9933668', 'pythonlogo.png') | |
im | |
images.to_s3(im, 'my-example-bucket-9933668', 'pythonlogo2.png') | |
""" | |
def __init__(self, aws_access_key_id, aws_secret_access_key, region_name): | |
self.s3 = boto3.client('s3', aws_access_key_id=aws_access_key_id, | |
aws_secret_access_key=aws_secret_access_key, | |
region_name=region_name) | |
def from_s3(self, bucket, key): | |
file_byte_string = self.s3.get_object(Bucket=bucket, Key=key)['Body'].read() | |
return Image.open(BytesIO(file_byte_string)) | |
def to_s3(self, img, bucket, key): | |
buffer = BytesIO() | |
img.save(buffer, self.__get_safe_ext(key)) | |
buffer.seek(0) | |
sent_data = self.s3.put_object(Bucket=bucket, Key=key, Body=buffer) | |
if sent_data['ResponseMetadata']['HTTPStatusCode'] != 200: | |
raise S3ImagesUploadFailed('Failed to upload image {} to bucket {}'.format(key, bucket)) | |
def __get_safe_ext(self, key): | |
ext = os.path.splitext(key)[-1].strip('.').upper() | |
if ext in ['JPG', 'JPEG']: | |
return 'JPEG' | |
elif ext in ['PNG']: | |
return 'PNG' | |
else: | |
raise S3ImagesInvalidExtension('Extension is invalid') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment