Created
September 17, 2019 21:19
-
-
Save cxasper/860070fa288f31bbf0939c171689fe27 to your computer and use it in GitHub Desktop.
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
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment