简体   繁体   English

在我的烧瓶应用程序中使用“ BOTO”的Amazon S3文件上传问题-Python

[英]Amazon S3 File Uploading issue using “BOTO” in my flask application -Python

In my flask application, I am using a function to upload file to Amazon s3, using Boto. 在我的flask应用程序中,我正在使用通过Boto将文件上传到Amazon s3的功能。

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

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. 这意味着您将不会使用set_contents_from_string,而是会存储和上传。 You may have to use something to chuck the file, like FileChuckIO. 您可能必须使用某些东西来固定文件,例如FileChuckIO。

An example is here if this applies to you : http://www.bogotobogo.com/DevOps/AWS/aws_S3_uploading_large_file.php 如果适合您,此处是一个示例: 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. 另外,您可能希望在上方编辑您的帖子并更改您的AWS密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM