def add_thumbnail(file_name):
''' add a thumbnail for the specific file'''
src_uri = boto.storage_uri('{}/{}'.format(conf.photos_bucket_name,
file_name),
'gs')
dest_uri = boto.storage_uri('{}/{}'.format(conf.thumbnails_bucket_name,
file_name),
'gs')
try:
new_key = dest_uri.new_key()
except boto.exception.NoAuthHandlerFound as e:
logging.error(e)
return None
# Create a file-like object for holding the photo contents.
photo = StringIO.StringIO()
src_uri.get_key().get_file(photo)
thumbnail = StringIO.StringIO()
im = Image.open(photo)
im.thumbnail((260, 260))
im.save(thumbnail, 'JPEG')
thumbnail.seek(0)
# save the thumbnail
try:
new_key.set_contents_from_file(thumbnail)
new_key.make_public()
except boto.exception.GSResponseError as e:
logging.error(e)
# Do we have the credentials file set up?
boto_cred_file = os.path.expanduser('~') + '/.boto'
if not os.path.exists(boto_cred_file):
logging.error('Credentials file {} was not found.'.format(boto_cred_file))
评论列表
文章目录