【问题标题】:How to upload large files to Google Storage using Google App Engine + Django and JQuery File Upload如何使用 Google App Engine + Django 和 JQuery File Upload 将大文件上传到 Google Storage
【发布时间】:2023-12-22 00:51:01
【问题描述】:

实际上没有用于将 Django 和大型可恢复文件上传到 Google 存储的示例源代码,但只有这个 https://cloud.google.com/storage/docs/performing-resumable-uploads#upload-resumable。有没有关于我如何实现这一目标的示例?

【问题讨论】:

    标签: django google-app-engine google-cloud-storage jquery-file-upload


    【解决方案1】:

    这提供了一个很好的答案,所有的功劳归于 Kevin Hawkins:The most extensive walkthrough for Django + Google Storage & Signed_URLS where it mentions about cors, django and js code at the same time

    科斯:

    gsutil cors 设置 cors.json gs://yourbucket.appspot.com

    [
      {
        "origin": ["*"],
        "responseHeader": ["Content-Type", "Access-Control-Allow-Origin"],
        "method": ["GET", "PUT", "OPTIONS"],
        "maxAgeSeconds": 60
      }
    

    ]

    签名网址:

    # view (url: /tools/upload/url)
    def get_signed_url(request):
    if request.method == 'POST' and request.is_ajax():
        filename = request.POST.get('filename')
        filetype = request.POST.get('type')
        filesize = request.POST.get('size', 0)
    
        # build the URL path using whatever information
        fullpath = '/path/'+filename
    
        # create the blob - the blob has to be created in order to get a signed URL
        blob = default_storage.open(fullpath, 'wb')
    
        # generate the signed URL through the blob object - don't use django-storages (yet)
        signed_url = blob.blob.generate_signed_url(expiration=default_storage.expiration, method='PUT', content_type=filetype)
    
        # This is what you'd do with djagno-storages
        #signed_url = default_storage.url(fullpath)
    
        # Send the signed URL back. I also send the path back because I want to display the uploaded image (relative path)
        return JsonResponse({ 'signed_url': signed_url, 'url': settings.MEDIA_URL + fullpath })
    
    # Probably a terrible way to respond. You do you.
    return JsonResponse({})
    

    【讨论】:

      最近更新 更多