Это отличный ответ, все кредиты go Кевину Хокинсу: Самое обширное прохождение для Django + Google Storage & Signed_URLS, где упоминается о cors, django и js код на в то же время
Cors:
gsutil cors set cors. json gs: //yourbucket.appspot.com
[
{
"origin": ["*"],
"responseHeader": ["Content-Type", "Access-Control-Allow-Origin"],
"method": ["GET", "PUT", "OPTIONS"],
"maxAgeSeconds": 60
}
]
Подписанные URL:
# 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({})