0
votes

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. Are there any examples on how I may achieve this ?

1

1 Answers

0
votes

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

Cors :

gsutil cors set 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({})