2012-10-02 112 views
0

我试图通过PUT方法将文件上传到自己的桶,但不能得到它的工作:上传文件到谷歌云存储由JavaScript

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

您能否提供错误详情?此外,你所要求的存储桶是“反驳” - 是一个错字?使用命令'gsutil ls gs:// bucked'来检查该桶是否已经存在可能是值得的。 –

+0

你应该尝试S3。 GS只是副本(虽然不是很好)。 – themihai

回答

0

这将是更有帮助,如果你能告诉我们如何PUT操作失败。您是否尝试上传到commondatastorage.googleapis.com而不是storage.googleapis.com?

+0

他们的服务器没有根据需要响应第一个OPTIONS请求 – user1031143

+0

XMLHttpRequest无法加载http://commondatastorage.googleapis.com/**。 Access-Control-Allow-Origin不允许使用原始http://www.**.net。 – user1031143

+1

这应该是评论,而不是答案。 –

2

错误似乎表明CORS未设置为您的存储桶。

+0

为能够设置CORS设置,我需要使用PUT方法,以便能够使用PUT请求我需要使用PUT请求,我不能使用:) – user1031143

+0

您可以使用gsutil(gsutil setcors命令)设置CORS, – Navneet

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);. 
  1. 设置适当的原点
  2. 添加响应标头(同您的要求)[ “授权”, “内容类型”,“X-文件级名称”, “X-文件大小”, “X-goog-ACL”]
  3. PUT和OPTIONS方法 我找到解决这个doc

样品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设置cors.json GS:// your_bucket

PS。连接和Content-lenght不是必需的,JS会向控制台抛出一些错误。

相关问题