使用 Flask 将文件上传到 Google Cloud Storage [英] Uploading files to Google Cloud Storage with Flask

查看:34
本文介绍了使用 Flask 将文件上传到 Google Cloud Storage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Flask 通过 App Engine 实例将一些图像上传到 GCS,但是每次上传文件时,当我下载它时,我都会得到一个损坏的文件.我做错了什么?

I'm trying to upload some images to GCS via an App Engine instance using Flask, but every time the file it's uploaded, when I download it I get a corrupt file. What am I doing wrong ?

我已经按照文档中的方式下载并使用了 Google Cloud Storage 客户端.

I've downloaded and used the Google Cloud Storage client the way is in the docs.

@app.route('/upload', methods=['POST'])
def upload():
    if request.method == 'POST':
        file = request.files['file']
        extension = secure_filename(file.filename).rsplit('.', 1)[1]
        options = {}
        options['retry_params'] = gcs.RetryParams(backoff_factor=1.1)
        options['content_type'] = 'image/' + extension
        bucket_name = "gcs-tester-app"
        path = '/' + bucket_name + '/' + str(secure_filename(file.filename))
        if file and allowed_file(file.filename):
            try:
                with gcs.open(path, 'w', **options) as f:
                    f.write(str(file))
                    print jsonify({"success": True})
                return jsonify({"success": True})
            except Exception as e:
                logging.exception(e)
                return jsonify({"success": False})

感谢您的帮助!!

推荐答案

您正在上传(写入 gcs 流)文件对象的 str 表示,而不是文件内容.

You're uploading (writing to the gcs stream) the str representation of the file object, not the file contents.

试试这个:

@app.route('/upload', methods=['POST'])
def upload():
    if request.method == 'POST':
        file = request.files['file']
        extension = secure_filename(file.filename).rsplit('.', 1)[1]
        options = {}
        options['retry_params'] = gcs.RetryParams(backoff_factor=1.1)
        options['content_type'] = 'image/' + extension
        bucket_name = "gcs-tester-app"
        path = '/' + bucket_name + '/' + str(secure_filename(file.filename))
        if file and allowed_file(file.filename):
            try:
                with gcs.open(path, 'w', **options) as f:
                    f.write(file.stream.read())# instead of f.write(str(file))
                    print jsonify({"success": True})
                return jsonify({"success": True})
            except Exception as e:
                logging.exception(e)
                return jsonify({"success": False})

但这不是最有效的方法,而且,直接从应用程序引擎上传文件有 32mb 的上限,避免这种情况的方法是使用 GCS 签署上传 url 并直接从GCS 的前端,或者您可以使用 blobstore 和处理程序创建文件上传 url 来执行上传后处理,如下所示:https://cloud.google.com/appengine/docs/python/blobstore/

But this is not the most efficient way to do this, also, there is a 32mb cap on file uploads straight from app engine, the way to avoid this is by signing an upload url with GCS and upload the file directly from the front-end to GCS, or you can create a file upload url with blobstore and a handler to do the post-upload processing, as specified here: https://cloud.google.com/appengine/docs/python/blobstore/

这篇关于使用 Flask 将文件上传到 Google Cloud Storage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆