Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

313
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda