Miki Tebeka
Miki Tebeka

Reputation: 13910

GCS, signed URL and CORS

I'm trying to upload a file to GCS from the browser using a signed URL.

I've set the CORS policy on the bucket:

$ gsutil cors get gs://some-bucket/
[{"maxAgeSeconds": 3600, "method": ["HEAD", "GET", "OPTIONS", "PUT"], "origin": ["*"], "responseHeader": ["Access-Control-Allow-Origin", "Content-Length", "Content-Type", "User-Agent", "x-goog-resumable"]}]

Then I generate a signed URL:

$ gsutil signurl -m PUT -c application/octet-stream -d 1d credentials.json gs://some-bucket/data.txt
https://storage.googleapis.com/...

But when I do an OPTIONS call on the bucket, I don't see the CORS headers:

$ curl -i -X OPTIONS -H 'Content-Type: application/octet-stream' https://storage.googleapis.com/...

HTTP/2 200 
x-guploader-uploadid: ADPycdsFHRQM8pa4O7veziiugor92S82cN7Ov2S5XhbycQ2x_PwAPhWnhssvdYeoyW01SMOSwMeZIAtxQsxKLLokBlgo4kCNRQ
date: Sun, 19 Dec 2021 10:52:39 GMT
expires: Sun, 19 Dec 2021 10:52:39 GMT
cache-control: private, max-age=0
content-length: 0
server: UploadServer
content-type: text/html; charset=UTF-8
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"

I can't figure out what am I doing wrong. Any help is appreciated.

Upvotes: 0

Views: 89

Answers (1)

Miki Tebeka
Miki Tebeka

Reputation: 13910

Found out the problem was with how I send the data from the browser. The following jQuery code worked for me:

var uploadURL = "https://storage.googleapis.com/...";

function upload() {
    console.log('UPLOADING');
    $.ajax({
    url: uploadURL,
    crossDomain: true,
    headers: {
        'Content-Type': 'application/octet-stream'
    },
    data: 'bazooka',
    error: function(xhr, stat, error) {
        console.log('ERROR: ' + error);
    },
    method: 'PUT',
    success: function(data, stat, xhr) {
        console.log('SUCCESS: ' + data);
    }
    });
}

$(function() {
    $('#upload').click(upload);
});

Upvotes: 0

Related Questions