def write(name, image, format): key = bucket.new_key(name) buf = StringIO.StringIO() image.save(buf, format=format, quality=8) key.set_metadata('Content-Type', 'image/'+format.lower()) key.set_contents_from_string(buf.getvalue()) buf.close()
def write(name, image, format): key = bucket.new_key(name) buf = StringIO.StringIO() image.save(buf, format=format, quality=8) key.set_metadata('Content-Type', 'image/'+format.lower()) key.set_contents_from_string(buf.getvalue()) # Make the image publicly available key.set_canned_acl('public-read') buf.close()
def upload(): # Get the uploaded image from the user upload = request.files.get('image') # If the user didn't actually send anything then error if not upload: return 'no image' # Set some variables for convenience; upload.file is a # file-descriptor like object representing the data the # user has uploaded. file = upload.file image = None # Attempt to open the file the user uploaded as an image # and if it fails then tell the user they've uploaded an # invalid image. try: image = Image.open(file) except: return 'invalid image' # Since Image.open moves the file pointer as it checks the # validity of the image, we need to rewind it for when we # place all the data into the bucket. file.seek(0) # Generate a new id and key to place the image into. id = generate_id() # Store the original image into the bucket. key = bucket.new_key(id+'-original') # Set the Content-Type metadata to the appropriate mime-type so # that when your data is served over S3 the browser can display # the image properly (the default is application/octet-stream). key.set_metadata('Content-Type', 'image/'+image.format.lower()) # Finally load the image data into S3. key.set_contents_from_file(file) # Make the image publicly available key.set_canned_acl('public-read') # Send a message to a worker to begin processing the resizing # of the freshly minted image. notify_worker(id, sizes) # Return the URLs to the images. return { key: url(id+'-'+key) for key in ['original'] + sizes.keys() }