如何在web上的python flask中从AWS s3读取文件

如何在web上的python flask中从AWS s3读取文件,python,amazon-web-services,amazon-s3,flask,Python,Amazon Web Services,Amazon S3,Flask,我想导入s3文件并立即在web上显示文件('parameter.txt')的内容。 我不确定我在文件名中输入了代码的哪一部分 发生此错误。 请给我一些建议。多谢各位 @app.route('/information', methods=['POST']) def information(): key = request.form['key'] my_bucket = get_bucket() file_obj = my_bucket.Object(key).get()

我想导入s3文件并立即在web上显示文件('parameter.txt')的内容。 我不确定我在文件名中输入了代码的哪一部分

发生此错误。 请给我一些建议。多谢各位

@app.route('/information', methods=['POST'])
def information():
    key = request.form['key']
    my_bucket = get_bucket()
    file_obj = my_bucket.Object(key).get()

    return Response(
        file_obj['Body'].read(),
        mimetype='text/plain',
        headers={"Content-Disposition": "attachment:filename= 
        {}".format(key)}
      )
错误:。。。 返回self.\u make\u api\u call(operation\u name,kwargs)文件“/home/ubuntu/.local/lib/python3.6/site packages/botocore/client.py”, LINE634,在调用 api_参数,操作_模型,context=request_context)文件“/home/ubuntu/.local/lib/python3.6/site packages/botocore/client.py”, LINE 682,在转换为请求指令中 api参数,操作模型文件“/home/ubuntu/.local/lib/python3.6/site packages/botocore/validate.py”, 第297行,在序列化请求中 raise ParamValidationError(report=report.generate_report())

botocore.exceptions.ParamValidationError:参数验证失败: 参数键的长度无效,值:0,有效范围:1-inf


我想你的问题在这里
key=request.form['key']
。验证
request.form
是否具有键
key
。发布一个
键的值示例。同时退出您的问题以显示如何创建boto3客户端。
import boto
from boto.s3.key import Key

keyId = "your key"
sKeyId="your_aws_secret_key_id"

srcFileName="package.json"
destFileName="package1.json"
bucketName="bucketname"
conn = boto.connect_s3(keyId,sKeyId)
bucket = conn.get_bucket(bucketName)

#Get the Key object of the given key, in the bucket

k = Key(bucket,srcFileName)

#Get the contents of the key into a file 

k.get_contents_to_filename(destFileName)