def _thumbnail_s3(self, original_filename, thumb_filename, thumb_size, thumb_url, bucket_name, crop=None, bg=None, quality=85): """Finds or creates a thumbnail for the specified image on Amazon S3.""" scheme = self.app.config.get('THUMBNAIL_S3_USE_HTTPS') and 'https' or 'http' thumb_url_full = url_for_s3( 'static', bucket_name=self.app.config.get('THUMBNAIL_S3_BUCKET_NAME'), filename=thumb_url, scheme=scheme) original_url_full = url_for_s3( 'static', bucket_name=bucket_name, filename=self._get_s3_path(original_filename).replace('static/', ''), scheme=scheme) # Return the thumbnail URL now if it already exists on S3. # HTTP HEAD request saves us actually downloading the image # for this check. # Thanks to: # http://stackoverflow.com/a/16778749/2066849 try: resp = httplib2.Http().request(thumb_url_full, 'HEAD') resp_status = int(resp[0]['status']) assert(resp_status < 400) return thumb_url_full except Exception: pass # Thanks to: # http://stackoverflow.com/a/12020860/2066849 try: fd = urllib.urlopen(original_url_full) temp_file = BytesIO(fd.read()) image = Image.open(temp_file) except Exception: return '' img = self._thumbnail_resize(image, thumb_size, crop=crop, bg=bg) temp_file = BytesIO() img.save(temp_file, image.format, quality=quality) conn = S3Connection(self.app.config.get('THUMBNAIL_S3_ACCESS_KEY_ID'), self.app.config.get('THUMBNAIL_S3_ACCESS_KEY_SECRET')) bucket = conn.get_bucket(self.app.config.get('THUMBNAIL_S3_BUCKET_NAME')) path = self._get_s3_path(thumb_filename) k = bucket.new_key(path) try: k.set_contents_from_string(temp_file.getvalue()) k.set_acl(self.app.config.get('THUMBNAIL_S3_ACL', 'public-read')) except S3ResponseError: return '' return thumb_url_full
def _thumbnail_s3(self, original_filename, thumb_filename, thumb_size, thumb_url, crop=None, bg=None, quality=85): """Finds or creates a thumbnail for the specified image on Amazon S3.""" scheme = self.app.config.get('THUMBNAIL_S3_USE_HTTPS') and 'https' or 'http' bucket_name = self.app.config.get('THUMBNAIL_S3_BUCKET_NAME') cdn_domain = self.app.config.get('THUMBNAIL_S3_CDN_DOMAIN') thumb_url_full = url_for_s3( 'static', bucket_name=bucket_name, cdn_domain=cdn_domain, filename=thumb_url, scheme=scheme) original_url_full = url_for_s3( 'static', bucket_name=bucket_name, cdn_domain=cdn_domain, filename=self._get_s3_path(original_filename).replace('static/', ''), scheme=scheme) conn = S3Connection(self.app.config.get('THUMBNAIL_S3_ACCESS_KEY_ID'), self.app.config.get('THUMBNAIL_S3_ACCESS_KEY_SECRET')) bucket = conn.get_bucket(self.app.config.get('THUMBNAIL_S3_BUCKET_NAME')) # Return the thumbnail URL now if it already exists on S3. key_exists = bucket.get_key(thumb_filename) if key_exists: return thumb_url_full # Thanks to: # http://stackoverflow.com/a/12020860/2066849 try: fd = urllib.urlopen(original_url_full) temp_file = BytesIO(fd.read()) image = Image.open(temp_file) except Exception: return '' img = self._thumbnail_resize(image, thumb_size, crop=crop, bg=bg) temp_file = BytesIO() img.save(temp_file, image.format, quality=quality) path = self._get_s3_path(thumb_filename) k = bucket.new_key(path) try: k.set_contents_from_string(temp_file.getvalue()) k.set_acl(self.app.config.get('THUMBNAIL_S3_ACL', 'public-read')) except S3ResponseError: return '' return thumb_url_full
def thumbnail_s3(original_url, bucket_name, thumb_size=(400, 400), crop=None, quality=85): """Finds or creates a thumbnail for the specified image on Amazon S3.""" scheme = 'http' img_folder = 'imgs' thumb_name = original_url.split('/')[::-1][0].split('?')[0] thumb_url_full = url_for_s3(img_folder, bucket_name=bucket_name, filename=thumb_name, scheme=scheme) path = '{}/{}'.format(img_folder, _get_s3_path(thumb_name)) # Return the thumbnail URL now if it already exists on S3. # HTTP HEAD request saves us actually downloading the image # for this check. # Thanks to: # http://stackoverflow.com/a/16778749/2066849 try: resp = requests.head(thumb_url_full) assert (resp.status_code == requests.codes.ok) return path except Exception: pass try: image = Image.open(requests.get(original_url, stream=True).raw) except Exception: print(Exception) return '' img = _thumbnail_resize(image, thumb_size) temp_file = BytesIO() img.save(temp_file, image.format, quality=quality) conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) bucket = conn.get_bucket(AWS_STORAGE_BUCKET_NAME) k = bucket.new_key(path) try: k.set_contents_from_string(temp_file.getvalue()) k.set_metadata('Cache-Control', 'max-age={}'.format(60 * 60 * 24 * 100)) k.set_acl('public-read') except S3ResponseError: return '' return path
def thumbnail_s3(original_url, bucket_name, thumb_size=(400, 400), crop=None, quality=85): """Finds or creates a thumbnail for the specified image on Amazon S3.""" scheme = 'http' img_folder = 'imgs' thumb_name = original_url.split('/')[::-1][0] thumb_url_full = url_for_s3( img_folder, bucket_name=bucket_name, filename=thumb_name, scheme=scheme) path = '{}/{}'.format(img_folder, _get_s3_path(thumb_name)) # Return the thumbnail URL now if it already exists on S3. # HTTP HEAD request saves us actually downloading the image # for this check. # Thanks to: # http://stackoverflow.com/a/16778749/2066849 try: resp = requests.head(thumb_url_full) assert(resp.status_code == requests.codes.ok) return path except Exception: pass try: image = Image.open(requests.get(original_url, stream=True).raw) except Exception: print(Exception) return '' img = _thumbnail_resize(image, thumb_size) temp_file = BytesIO() img.save(temp_file, image.format, quality=quality) conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) bucket = conn.get_bucket(AWS_STORAGE_BUCKET_NAME) k = bucket.new_key(path) try: k.set_contents_from_string(temp_file.getvalue()) k.set_metadata( 'Cache-Control', 'max-age={}'.format(60 * 60 * 24 * 100)) k.set_acl('public-read') except S3ResponseError: return '' return path
def get_url(self, field): if op.isfile(op.join(field.base_path, field.data)): return super(S3ImageUploadInput, self).get_url(field) if field.thumbnail_size: filename = field.thumbnail_fn(field.data) else: filename = field.data if field.url_relative_path: filename = urljoin(field.url_relative_path, filename) return url_for_s3(field.endpoint, bucket_name=field.bucket_name, filename=filename)
def get_url(self, field): if op.isfile(op.join(field.base_path, field.data)): return super(S3ImageUploadInput, self).get_url(field) if field.thumbnail_size: filename = field.thumbnail_fn(field.data) else: filename = field.data if field.url_relative_path: filename = urljoin(field.url_relative_path, filename) return url_for_s3('static', bucket_name=field.bucket_name, filename=filename)
def image_url_storageaware(self): if not self.image: return None if not (self.image_storage_type and self.image_storage_bucket_name): return url_for('static', filename=self.image_url, _external=True) if self.image_storage_type != 's3': raise ValueError( ('Storage type "%s" is invalid, the only supported ' + 'storage type (apart from default local storage) ' + 'is s3.') % self.image_storage_type) return url_for_s3('static', bucket_name=self.image_storage_bucket_name, filename=self.image_url)
def image_url_storageaware(self): if not self.image: return None if not ( self.image_storage_type and self.image_storage_bucket_name): return url_for( 'static', filename=self.image_url, _external=True) if self.image_storage_type != 's3': raise ValueError(( 'Storage type "%s" is invalid, the only supported ' + 'storage type (apart from default local storage) ' + 'is s3.') % self.image_storage_type) return url_for_s3( 'static', bucket_name=self.image_storage_bucket_name, filename=self.image_url)
def _thumbnail_s3(self, original_filename, thumb_filename, thumb_size, thumb_url, crop=None, bg=None, quality=85): """Finds or creates a thumbnail for the specified image on Amazon S3.""" scheme = self.app.config.get( 'THUMBNAIL_S3_USE_HTTPS') and 'https' or 'http' bucket_name = self.app.config.get('THUMBNAIL_S3_BUCKET_NAME') cdn_domain = self.app.config.get('THUMBNAIL_S3_CDN_DOMAIN') thumb_url_full = url_for_s3('static', bucket_name=bucket_name, cdn_domain=cdn_domain, filename=thumb_url, scheme=scheme) original_url_full = url_for_s3( 'static', bucket_name=bucket_name, cdn_domain=cdn_domain, filename=self._get_s3_path(original_filename).replace( 'static/', ''), scheme=scheme) conn = S3Connection( self.app.config.get('THUMBNAIL_S3_ACCESS_KEY_ID'), self.app.config.get('THUMBNAIL_S3_ACCESS_KEY_SECRET')) bucket = conn.get_bucket( self.app.config.get('THUMBNAIL_S3_BUCKET_NAME')) # Return the thumbnail URL now if it already exists on S3. key_exists = bucket.get_key(thumb_filename) if key_exists: return thumb_url_full # Thanks to: # http://stackoverflow.com/a/12020860/2066849 try: fd = urlopen(original_url_full) temp_file = BytesIO(fd.read()) image = Image.open(temp_file) except Exception: return '' img = self._thumbnail_resize(image, thumb_size, crop=crop, bg=bg) temp_file = BytesIO() img.save(temp_file, image.format, quality=quality) path = self._get_s3_path(thumb_filename) k = bucket.new_key(path) try: k.set_contents_from_string(temp_file.getvalue()) k.set_acl(self.app.config.get('THUMBNAIL_S3_ACL', 'public-read')) except S3ResponseError: return '' return thumb_url_full
def _thumbnail_s3(self, original_filename, thumb_filename, thumb_size, thumb_url, bucket_name, crop=None, bg=None, quality=85): """Finds or creates a thumbnail for the specified image on Amazon S3.""" scheme = self.app.config.get( 'THUMBNAIL_S3_USE_HTTPS') and 'https' or 'http' thumb_url_full = url_for_s3( 'static', bucket_name=self.app.config.get('THUMBNAIL_S3_BUCKET_NAME'), filename=thumb_url, scheme=scheme) original_url_full = url_for_s3( 'static', bucket_name=bucket_name, filename=self._get_s3_path(original_filename).replace( 'static/', ''), scheme=scheme) # Return the thumbnail URL now if it already exists on S3. # HTTP HEAD request saves us actually downloading the image # for this check. # Thanks to: # http://stackoverflow.com/a/16778749/2066849 try: resp = httplib2.Http().request(thumb_url_full, 'HEAD') resp_status = int(resp[0]['status']) assert (resp_status < 400) return thumb_url_full except Exception: pass # Thanks to: # http://stackoverflow.com/a/12020860/2066849 try: fd = urllib.urlopen(original_url_full) temp_file = BytesIO(fd.read()) image = Image.open(temp_file) except Exception: return '' img = self._thumbnail_resize(image, thumb_size, crop=crop, bg=bg) temp_file = BytesIO() img.save(temp_file, image.format, quality=quality) conn = S3Connection( self.app.config.get('THUMBNAIL_S3_ACCESS_KEY_ID'), self.app.config.get('THUMBNAIL_S3_ACCESS_KEY_SECRET')) bucket = conn.get_bucket( self.app.config.get('THUMBNAIL_S3_BUCKET_NAME')) path = self._get_s3_path(thumb_filename) k = bucket.new_key(path) try: k.set_contents_from_string(temp_file.getvalue()) k.set_acl(self.app.config.get('THUMBNAIL_S3_ACL', 'public-read')) except S3ResponseError: return '' return thumb_url_full
def s3_url_for_employee_photo(self): if app.config['USE_S3']: return url_for_s3('', bucket_name=app.config['S3_BUCKET_NAME'], scheme='https', filename=self.image)