Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from PIL import Image
from django.conf import settings
import S3, mimetypes
THUMBNAIL_SIZE = 70, 70
MEDIUM_SIZE = 150, 150
profile_image_path = settings.MEDIA_ROOT + '/images/roles/'
AWS_ACCESS_KEY_ID = 'YOUR_AWS_KEY'
AWS_SECRET_ACCESS_KEY = 'YOUR_AWS_SECRET'
BUCKET_NAME = settings.AMAZON_MEDIA_BUCKET
conn = S3.AWSAuthConnection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
def handle_uploaded_picture(user_id, picture_file):
imo = Image.open(picture_file)
## First make the image a square. Crop it.
width, height = imo.size
if width > height:
delta = width - height
left = int(delta/2)
upper = 0
right = height + left
lower = height
else:
delta = height - width
left = int(delta)/2
upper = 0
right = width
lower = width + upper
im = imo.crop((left, upper, right, lower))
create_medium_size(im, user_id)
create_thumbnail_size(im, user_id)
def create_thumbnail_size(image_object, user_id):
image_object.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
image_object.save(profile_image_path + str(user_id) + "_70.jpg", "JPEG")
filedata = open(profile_image_path + str(user_id) + "_70.jpg", 'rb').read()
conn.put(BUCKET_NAME, 'images/provider/' + str(user_id)+'_70.jpg', S3.S3Object(filedata), {'x-amz-acl': 'public-read', 'Content-Type': 'image/jpeg'})
def create_medium_size(image_object, user_id):
image_object.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS)
image_object.save(profile_image_path + str(user_id) + "_150.jpg", "JPEG")
filedata = open(profile_image_path + str(user_id) + "_150.jpg", 'rb').read()
conn.put(BUCKET_NAME, 'images/provider/' + str(user_id)+'_150.jpg', S3.S3Object(filedata), {'x-amz-acl': 'public-read', 'Content-Type': 'image/jpeg'})
def delete_profile_picture_from_s3(user_id):
conn.delete(BUCKET_NAME, 'images/provider/' + str(user_id)+'_70.jpg')
conn.delete(BUCKET_NAME, 'images/provider/' + str(user_id)+'_150.jpg')
@believeohiozua

This comment has been minimized.

Copy link

believeohiozua commented Sep 13, 2019

import S3 is giving import error on python 3, django 2.0. do you have any fix for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.