简体   繁体   English

使用boto3和python flask将CSV文件上传到S3存储桶时出现错误500

[英]Error 500 while Uploading CSV file to S3 bucket using boto3 and python flask

kind of looked at all possible options. 看了所有可能的选择。 I am using boto3 and python3.6 to upload file to s3 bucket, Funny thing is while json and even .py file is getting uploaded, it is throwing Error 500 while uploading CSV. 我正在使用boto3和python3.6将文件上传到s3存储桶,有趣的是json甚至.py文件正在上传时,在上传CSV时抛出Error 500。 On successful uplaod i am returning an json to check all the values. 成功升级后,我将返回一个json以检查所有值。

    import boto3
    from botocore.client import Config

    @app.route("/upload",methods = ['POST','GET'])
    def upload(): 
    if request.method == 'POST':
        file = request.files['file']
        filename = secure_filename(file.filename)
        s3 = boto3.resource('s3', aws_access_key_id= os.environ.get('AWS_ACCESS_KEY_ID'), aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY'),config=Config(signature_version='s3v4'))
        s3.Bucket(os.environ.get('S3_BUCKET')).put_object(Key=filename, Body=open(filename, 'rb'), ContentEncoding='text/csv')
        return jsonify({'successful upload':filename, 'S3_BUCKET':os.environ.get('S3_BUCKET'), 'ke':os.environ.get('AWS_ACCESS_KEY_ID'), 'sec':os.environ.get('AWS_SECRET_ACCESS_KEY'),'filepath': "https://s3.us-east-2.amazonaws.com/"+os.environ.get('S3_BUCKET')+"/" +filename})

Please help!! 请帮忙!!

You are getting a FileNotFoundError for file xyz.csv because the file does not exist. 因为文件xyz.csv不存在,所以您收到FileNotFoundError

This could be because the code in upload() does not actually save the uploaded file, it merely obtains a safe name for it and immediately tries to open it - which fails. 这可能是因为upload()中的代码实际上并未保存上传的文件,它只是为其获取一个安全名称,然后立即尝试将其打开-失败。

That it works for other files is probably due to the fact that those files already exist, perhaps left over from testing, so there is no problem. 它适用于其他文件可能是由于这些文件已经存在,可能是测试遗留的事实,所以没有问题。

Try saving the file to the file system using save() after obtaining the safe filename: 获取安全文件名后,尝试使用save()将文件save()到文件系统中:

upload_file = request.files['file']
filename = secure_filename(upload_file.filename)
upload_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

and then uploading it (assuming that you've configured an UPLOAD_FOLDER ): 然后上传(假设您已配置UPLOAD_FOLDER ):

with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'rb') as f:
    s3.Bucket(os.environ.get('S3_BUCKET')).put_object(Key=filename, Body=f, ContentEncoding='text/csv')
    return jsonify({...})

There is no need to actually save the file to the file system; 无需将文件实际保存到文件系统; it can be streamed directly to your S3 bucket using the stream attribute of the upload_file object: 可以使用upload_file对象的stream属性将其直接流式传输到S3存储桶:

upload_file = request.files['file']
filename = secure_filename(upload_file.filename)

s3 = boto3.resource('s3', aws_access_key_id='key', aws_secret_access_key='secret')
s3.Bucket('bucket').put_object(Key=filename, Body=upload_file.stream, ContentType=upload_file.content_type)

To make this more generic you should use the content_type attribute of the uploaded file as shown above. 要使其更通用,您应该使用上载文件的content_type属性,如上所示。

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

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