Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

save PIL image to s3

if self.image:
    m = storage.open(self.image.name)
    obj_location = 'media/%s' % self.image.name

    # img = Image.open(self.image)
    session = boto3.Session()

    s3_session = session.client(
        "s3", aws_access_key_id=settings.AWS_S3_ACCESS_KEY_ID,
        aws_secret_access_key=settings.AWS_S3_SECRET_ACCESS_KEY
    )

    img = Image.open(m)

    if img.height > 200 or img.width > 200:
        output_size = (200, 200)
        img.resize(output_size)
        img.thumbnail(output_size)

        img.convert('RGB')
        sfile = io.BytesIO()
        img.save(sfile, format='JPEG')
        # im.seek(0)
        
        value = sfile.getvalue()
        # buffer = sfile.getbuffer()
        
        md = hashlib.md5(value).digest()
        img_md5 = base64.b64encode(md).decode('utf-8')

        s3_session.put_object(
            ContentMD5=img_md5,
            Body=sfile,
            Bucket=settings.AWS_STORAGE_BUCKET_NAME,
            Key=self.image.name
        )
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #save #PIL #image
ADD COMMENT
Topic
Name
6+8 =