CORS与SignedURL

时间:2017-12-27 10:21:55

标签: google-app-engine google-cloud-platform google-cloud-storage

如何使用带有CORS的Go API返回的SignedURL? 当前设置: Bucket配置 gsutil cors获取gs:// example-bucket 收益: [{   " maxAgeSeconds":1,   "方法":[" GET"," HEAD"," PUT"," DELETE"," POST"," OPTIONS"],   " origin":[" http://example.com" ;," https://example.com" ;," http:// localhost: 4000&#34],   " responseHeader":[" Origin"," Accept"," X-Requested-With"," Authorization", " Content-Type"," Content-Length"," Accept-Encoding"," X-CSRF-Token" ]}] 去API调用 acc,_:= appengine.ServiceAccount(ctx) signedUrl,err:= storage.SignedURL(         "例如桶&#34 ;,         文件名,         &安培; storage.SignedURLOptions {             GoogleAccessID:acc,             SignBytes:func(b [] byte)([] byte,error){                 _,signedBytes,signingError:= appengine.SignBytes(ctx,b)                 返回signedBytes,signingError             },             方法:" PUT",             ContentType:contentType,             过期:time.Now()。添加(1 * time.Hour),         }) JS XHR 使用Go的SignedURL,即xhr.open(" PUT",signedUrl); 附注 SignURL由Go API生成,始终以https://storage.googleapis.com/example-bucket/fileName ...其他地方开头我已经读过它应该是www.googleapis.com/storage但是我不知道#39;认为这是问题,肯定从官方API返回的网址是正确的。 我可以看到请求标头包括: access-control-request-headers:content-type,x-upload-content-type access-control-request-method:PUT 来源:http:// localhost:4000 所以它似乎勾掉了所需标题的所有方框......

1 个答案:

答案 0 :(得分:3)

在JS方面,我通过X-Upload-Content-Type标题为文件上传设置了其他元数据。

由于此标头不在CORS配置中,因此请求失败。

通过将该标头添加到配置中(例如,提供给responseHeader配置文件的gsutil cors set列表),所有内容都完全按照问题中的说明进行操作:)