Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

307
Views
Python flask ajax save decoded base64 image to server temporarily

I am resizing images client side before sending them to my flask app.

The resized image, which is drawn into a canvas to be resized, is sent via a POST request.

In my app the image is decoded via base64:

def resize_image(item):
    content = item.split(';')[1]
    image_encoded = content.split(',')[1]
    body = base64.decodestring(image_encoded.encode('utf-8'))
    return body

The imagedata is stored as type String in the body variable. I can save the data to my local machine and it works:

filename = 'some_image.jpg' 
with open(filename, 'wb') as f:
    print "written"
    f.write(body)

What I need is to upload the resized image to AWS3. On one point I need to read() the image contents, but until the image is saved somewhere as a file it is still a String, so it fails:

file_data = request.values['a']
imagedata = resize_image(file_data)              

s3 = boto.connect_s3(app.config['MY_AWS_ID'], app.config['MY_AWS_SECRET'], host='s3.eu-central-1.amazonaws.com')

bucket_name = 'my_bucket'
bucket = s3.get_bucket(bucket_name)
k = Key(bucket)  

# fails here         
file_contents = imagedata.read()

k.key = "my_images/" + "test.png"

k.set_contents_from_string(file_contents)

Unless there is an other solution, I thought I save the image temporarily to my server (Heroku) and upload it and then delete it, how would this work? Deleting afterwards is important here!

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

set_contents_from_string takes a string as a parameter, you could probably just pass your image string data directly to it for upload to S3

Solution:

Delete this part:

file_contents = imagedata.read()

Use imagedata directly here:

k.set_contents_from_string(imagedata)
about 4 years ago · Santiago Trujillo Report

0

If you need to call .read() on your data, but don't need save file on disk use StringIO:

import StringIO
output = StringIO.StringIO()
output.write('decoded image')
output.seek(0)


output.read()
Out[1]: 'decoded image'
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!