1

In my flask application, I am using a function to upload file to Amazon s3, using Boto.

Its working fine most of the cases, but some times its uploading files as zero byte file with no extension.

Why its failing sometimes,

I am validating user image file in form.

FileField('Your photo',validators=[FileAllowed(['jpg', 'png'], 'Images only!')])

My image upload function.

def upload_image_to_s3(image_from_form):


    #upload  pic to amazon
    source_file_name_photo = secure_filename(image_from_form.filename)
    source_extension = os.path.splitext(source_file_name_photo)[1]
    destination_file_name_photo = uuid4().hex + source_extension


    s3_file_name = destination_file_name_photo

    # Connect to S3 and upload file.

    conn = boto.connect_s3('ASJHjgjkhSDJJHKJKLSDH','GKLJHASDJGFAKSJDGJHASDKJKJHbbvhjcKJHSD')
    b = conn.get_bucket('mybucket')

    # Connect to S3 and upload file.

    sml = b.new_key("/".join(["myfolder",destination_file_name_photo]))
    sml.set_contents_from_string(image_from_form.read())
    acl='public-read'
    sml.set_acl(acl)

    return s3_file_name
2
  • Code looks fine to me. add mimetype sml.set_metadata('Content-Type', 'image/png') it may helps. Commented Mar 30, 2015 at 12:54
  • Do you get any error message back from S3? Commented Mar 30, 2015 at 16:27

1 Answer 1

1

How large are your assets? If there is too large of an upload, you may have to multipart/chunk it otherwise it will timeout.

bucketObject.initiate_multipart_upload('/local/object/as/file.ext')

it means you will not be using set_contents_from_string but rather store and upload. You may have to use something to chuck the file, like FileChuckIO.

An example is here if this applies to you : http://www.bogotobogo.com/DevOps/AWS/aws_S3_uploading_large_file.php

Also, you may want to edit your post above and alter your AWS keys.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm just uploading images., 1Kb -2 MB, I am not getting any erro respoce from S3
I'd check into the FileAllowed v/s FileRequired behavior. FileAllowed seems like it would enforce FileRequired, but it is not explicit in the documentation (flask-wtf.readthedocs.org/en/latest/…). Is it possible submissions are blank and it's returning no extension (because the .split fails ) and zero bytes because the empty file is ....zero.
Thanks for answer , I did not add the Required gladiator for that input filed , I added that for HTML file and my from :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.