Skip to content

Instantly share code, notes, and snippets.

@sykire
Forked from cxasper/utils.py
Last active September 17, 2019 22:07
Show Gist options
  • Save sykire/b180ff8e56f934c4bca8eb69b4f74b0e to your computer and use it in GitHub Desktop.
Save sykire/b180ff8e56f934c4bca8eb69b4f74b0e to your computer and use it in GitHub Desktop.
from os.path import basename
class EditImgUploadS3:
def __init__(self):
self.s3 = boto3.resource('s3')
self.region = os.getenv('AWS_STORAGE_REGION')
self.bucket = os.getenv('AWS_STORAGE_BUCKET_NAME')
def upload_object(self, image):
new_image = self.draw_rectangle(image)
image.seek(0)
client = boto3.client('s3', region_name=self.region)
client.upload_file(new_image.filename, 'trident-collection', basename(new_image.filename))
self.assign_acl(new_image)
key = f'{new_image}'.replace(' ', '+')
return f"https://{self.bucket}.s3.{self.region}.amazonaws.com/{key}"
def draw_rectangle(self, image):
source_img = Image.open(image).convert("RGBA")
draw = ImageDraw.Draw(source_img)
draw.rectangle(((1000, 1000), (1250, 1250)), outline="red", width=8)
source_img.save(f'../trident-api/media/{image}')
return Image.open(f'../trident-api/media/{image}', mode='r')
def assign_acl(self, image):
obj = self.s3.Bucket(self.bucket).Object(f'{image}')
return obj.Acl().put(ACL='public-read')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment