Skip to content

Instantly share code, notes, and snippets.

@cxasper
Created September 17, 2019 21:19
Show Gist options
  • Select an option

  • Save cxasper/860070fa288f31bbf0939c171689fe27 to your computer and use it in GitHub Desktop.

Select an option

Save cxasper/860070fa288f31bbf0939c171689fe27 to your computer and use it in GitHub Desktop.

Revisions

  1. cxasper created this gist Sep 17, 2019.
    25 changes: 25 additions & 0 deletions utils.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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, 'trident-collection', f'{new_image}')
    self.assign_acl(new_image)t
    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')