使用Flask将文件上传到Google Cloud Storage

Merqurio

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

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

@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})

谢谢你的帮助 !!

拉斐尔·巴罗斯(Rafael Barros)

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

尝试这个:

@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})

但这不是执行此操作的最有效方法,而且,直接从App Engine上传文件的上限为32mb,避免这种情况的方法是通过使用GCS签名上传网址并直接从前端上传文件到GCS,或者您可以使用blobstore和处理程序创建文件上传网址,以执行上传后处理,如下所示:https : //cloud.google.com/appengine/docs/python/blobstore/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 Codeigniter 将文件上传到 Google Cloud Storage

使用 For 循环将多个文件上传到 Google Cloud Storage

将 xml 文件上传到 Google Cloud Storage 时出错

Alamofire无法将文件上传到Google Cloud Storage

将文件上传到Google Cloud Storage:500后端错误

将.raw文件上传到Google Cloud Storage

使用Google App Engine(Python)将文件上传到Google Cloud Storage

使用Google App Engine将大文件上传到Google Cloud Storage

使用JSON API将文件上传到Google Cloud Storage,错误401未经授权

使用PHP和Ajax将文件上传到Google Cloud Storage Bucket

如何使用JSON API和CloseableHttpClient将文件上传到Google Cloud Storage存储桶?

使用 Node.js 将文件上传到 Google Cloud Storage(已声明“bucketName”)

使用Python将文件上传到Google Cloud Storage签名的URL

使用Python将文件上传到Google Cloud Storage Bucket子目录

使用C#中的HMAC密钥将文件上传到Google Cloud Storage

如何通过Cloud Functions将文件上传到Cloud Storage并使用Firestore控制对Cloud Storage的访问?

将文件从Firebase Cloud Functions上传到Cloud Storage

使用Java通过Google App Engine将文件上传到Google Cloud Storage :(无此类文件或目录)

将图像从Google Cloud Function上传到Cloud Storage

将文件上传到Google Cloud Storage而不在本地下载文件?

自动将数据上传到 Google Cloud Storage 和 BigQuery

将 Excel 上传到 Google Cloud Storage 时出错

将文件直接上传到Google Cloud Storage for GAE应用

将Python文件上传到Google Cloud Storage Bucket返回管道中断错误

将python对象上传到Google Cloud Storage,而不保存到文件中

无法从Django将文件上传到Google Cloud Storage(本地Windows ENV

从Localhost或外部服务器将文件上传到Google Cloud Storage

如何将文件上传到 Google Cloud Storage Bucket 子目录

使用 GAE 和 Google Cloud Storage 从 csv 文件上传到存储桶图片