Python 2.7 使用flask API从我的s3 bucket和boto3库在浏览器上下载文件
我想用我的FlaskAPI从s3 bucket下载浏览器上的文件。以前我将数据存储在服务器上,所以flask send文件对我来说非常有效,我可以下载该文件。现在,我已经切换到s3存储桶来存储我的docs send_文件,但它不起作用。 我正在使用boto3库连接s3服务器 这是代码供参考 下载文件的函数Python 2.7 使用flask API从我的s3 bucket和boto3库在浏览器上下载文件,python-2.7,Python 2.7,我想用我的FlaskAPI从s3 bucket下载浏览器上的文件。以前我将数据存储在服务器上,所以flask send文件对我来说非常有效,我可以下载该文件。现在,我已经切换到s3存储桶来存储我的docs send_文件,但它不起作用。 我正在使用boto3库连接s3服务器 这是代码供参考 下载文件的函数 def download_file_from_s3_bucket(KEY): s3_client = s3_bucket_connection() try: f
def download_file_from_s3_bucket(KEY):
s3_client = s3_bucket_connection()
try:
file_obj = open("test.pdf", "r+")
s3_client.download_fileobj(settings.BUCKET_NAME, KEY, file_obj)
return file_obj
except Exception as e:
return {"status": False, "message": e.message}
烧瓶API:
@api.route("/return_file", methods=["GET","POST"])
@jwt_required
def return_file():
try:
json_data = request.get_json(force=True)
upload_id = json_data.get('upload_id')
upload_path,file_name = v2_business.get_upload_path(upload_id)
file_link = upload_path
# old way to get file from local server.
# return send_file(file_link,file_name)
download_file_obj = root_business.download_file_from_s3_bucket(upload_path)
return send_file(download_file_obj, as_attachment=True, attachment_filename=file_name)
except IOError as e:
return jsonify({"status": False,
"message": (e.__class__.__name__ + ': ' + (str(e)))})
except Exception as e:
return jsonify({
"status": False,
"message": e.message
})