簡體   English   中英

如何在 web 上的 python flask 中從 AWS s3 讀取文件

[英]How to read file from AWS s3 in python flask on web

我想導入 s3 文件並立即在網絡上顯示文件('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._make_api_call(operation_name, kwargs) 文件“/home/ubuntu/.local/lib/python3.6/site-packages/botocore/client.py”,第 634 行,在 _make_api_call api_params, operation_model , context=request_context) 文件 "/home/ubuntu/.local/lib/python3.6/site-packages/botocore/client.py", line 682, in _convert_to_request_dict api_params, operation_model) 文件 "/home/ubuntu/. local/lib/python3.6/site-packages/botocore/validate.py”,第 297 行,在 serialize_to_request 中引發 ParamValidationError(report=report.generate_report())

botocore.exceptions.ParamValidationError:參數驗證失敗:參數鍵的長度無效,值:0,有效范圍:1-inf

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM