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

几乎没有Django和将大型可恢复文件上传到Google Storage的示例源代码,只有此https://cloud.google.com/storage/docs/performing-resumable-uploads#upload-resumable。有什么例子可以说明我如何实现这一目标?

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

这提供了一个很好的答案,所有功劳归于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

Cors:

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({})
本文链接:https://www.f2er.com/2528035.html

大家都在问