0

我正在尝试使用签名 URL 从浏览器将文件上传到 GCS。

我已经在存储桶上设置了 CORS 策略:

$ 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"]}]

然后我生成一个签名的 URL:

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

但是当我OPTIONS对存储桶进行调用时,我看不到 CORS 标头:

$ 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"

我不知道我在做什么错。任何帮助表示赞赏。

4

1 回答 1

0

发现问题在于我如何从浏览器发送数据。以下 jQuery 代码对我有用:

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);
});

于 2021-12-19T11:12:52.327 回答