简体   繁体   English

如何使用 Google App Engine + Django 和 JQuery File Upload 将大文件上传到 Google Storage

[英]How to upload large files to Google Storage using Google App Engine + Django and JQuery File Upload

There is literally no example source code for Django and Large resumable file upload to Google Storage but only this https://cloud.google.com/storage/docs/performing-resumable-uploads#upload-resumable .实际上没有 Django 和大型可恢复文件上传到 Google Storage 的示例源代码,只有这个https://cloud.google.com/storage/docs/performing-resumable-uploads#upload-resumable Are there any examples on how I may achieve this ?有没有关于我如何实现这一目标的例子?

This provides a great answer, all credits go to 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这提供了一个很好的答案,所有的功劳都归于Kevin Hawkins: Django + Google Storage & Signed_URLS 的最广泛演练,其中同时提到了 cors、django 和 js 代码

Cors :科尔斯:

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

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

] ]

Signed URLS :签名网址:

# 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({})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM