如何在没有BlobStore的情况下从Flask表单直接将视频文件上传到Cloud Storage?

时间:2020-02-13 07:56:17

标签: python google-app-engine flask google-cloud-storage flask-wtforms

我正在使用Flask,并且我的Web应用程序的索引页上有一个表单,该表单要求用户上传MP4视频。我希望我的用户上传30分钟长的视频,因此视频大小可能会达到数百兆字节。现在的问题是,我打算将此Flask应用程序部署到Google App Engine,显然我不能使用32MB以上的任何静态文件。不知何故,当我尝试上传已部署版本中大于32MB的任何视频时,都会出现Request Too Large错误。

我看到BlobStore Python API过去曾经是推荐的解决方案,过去可以在服务器上处理非常大的文件。但这是针对Python 2.7的:https://cloud.google.com/appengine/docs/standard/python/blobstore/

我正在使用Python 3.7,Google现在建议将文件直接上传到Cloud Storage,但我不确定如何做到这一点。

以下是显示我当前如何通过表单将用户上传的视频存储到Cloud Storage中的代码段。不幸的是,由于收到错误消息,我仍然无法上传大文件。再说一遍,我的问题是:如何使我的用户将文件直接上传到Cloud Storage,而不会导致服务器超时或给我一个Request Too Large错误?

form = SessionForm()
    blob_url = ""
    if form.validate_on_submit():

        f = form.video.data
        video_string = f.read()
        filename = secure_filename(f.filename)

        try:
            # The custom function upload_session_video() uploads the file to a Cloud Storage bucket
            # It uses the Storage API's upload_from_string() method.
            blob_url = upload_session_video(video_string, filename)

        except FileNotFoundError as error:
            flash(error, 'alert')

        # Create the Cloud Storage bucket (same name as the video file)
        user_bucket = create_bucket(form.patient_name.data.lower())

1 个答案:

答案 0 :(得分:0)

由于request limitation,您无法使用Google App Engine将超过32MB的文件上传到Cloud Storage。但是,您可以通过使用"google-resumable-media"在python情况下使用可恢复的上传上传到Cloud Storage来绕过该问题。

  • 资源的大小未知(即它是在 飞)
  • 请求必须是短暂的
  • 客户端有请求大小限制
  • 资源太大,无法容纳到内存中

示例代码included here