我试图通过PUT
方法将文件上传到我自己的存储桶中,但无法让它工作:
function upload(url, data) {
var pd = new XMLHttpRequest();
pd['open']('PUT', "http://storage.googleapis.com/" + url, true);
pd['setRequestHeader']('Content-type', 'text/html');
pd['setRequestHeader']('x-goog-acl', 'public-read');
pd['setRequestHeader']('Authorization', 'Bearer *********F0aIu4NbTd6A');
pd['setRequestHeader']('Content-length', data['length']);
pd['setRequestHeader']('Connection', 'close');
pd['send'](data);
}
upload('bucked/index.html','<b>hello</b>');
答案 0 :(得分:2)
该错误似乎表明您的存储桶没有设置CORS。
答案 1 :(得分:1)
我有类似的问题:
request.setRequestHeader('x-goog-acl', 'public-read');
request.setRequestHeader('Authorization', 'Bearer ' + access_token);
request.setRequestHeader("X-File-Name", file.name);
request.setRequestHeader("X-File-Size", file.size);.
示例cors.json
[
{
"origin": ["*", "http://localhost:8080", "https://localhost:8080"],
"responseHeader": ["authorization", "content-type", "x-file-name", "x-file-size", "x-goog-acl"],
"method": ["GET", "HEAD", "DELETE", "PUT", "OPTIONS"],
"maxAgeSeconds": 3600
}
]
要设置此配置,请使用gsutil cors set cors.json gs:// your_bucket
PS。 连接和内容长度不是必需的,JS会向控制台抛出一些错误。
答案 2 :(得分:0)
如果你能告诉我们PUT操作失败会更有帮助。您是否尝试过上传到commondatastorage.googleapis.com而不是storage.googleapis.com?