Google cloud storage 谷歌云存储CORS设置

Google cloud storage 谷歌云存储CORS设置,google-cloud-storage,Google Cloud Storage,我已经将CORS设置为google云存储的bucket,没有访问控制Allow Origin头 如果我的设置错误,我希望你告诉我正确的方法 我的设置 $ cat cors-json-file.json [ { "origin": [ "*" ], "responseHeader": ["Origin", "Accept", "X-Requested-With", "Authorization", "Content-Type", "Content-L

我已经将CORS设置为google云存储的bucket,没有访问控制Allow Origin头

如果我的设置错误,我希望你告诉我正确的方法

我的设置

$ cat cors-json-file.json
[
  {
    "origin": [
      "*"
    ],
    "responseHeader": ["Origin", "Accept", "X-Requested-With", "Authorization", "Content-Type",     "Content-Length", "Accept-Encoding", "X-CSRF-Token"],
    "method": [
      "GET",
      "OPTIONS"
    ],
    "maxAgeSeconds": 1
  }
]

$ gsutil cors set cors-json-file.json gs://stone-swallow

$ gsutil cors get gs://stone-swallow
[{"origin": ["*"], "responseHeader": ["Origin", "Accept", "X-Requested-With", "Authorization", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token"], "method": ["GET", "OPTIONS"], "maxAgeSeconds": 1}]
尝试浏览器错误消息

var invocation = new XMLHttpRequest();
var url = 'http://storage.googleapis.com/stone-swallow/arcanine.png';

function callOtherDomain() {
  if(invocation) {
    invocation.open('GET', url, true);
    invocation.send();
  }
}
callOtherDomain();

无法加载XMLHttpRequest。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”。

在GET方法上指定一个“Origin”头。这将解决问题。

确保存储桶上有公共读取:


$gsutil-m acl set-R-a public read gs://stone swallow

我也遇到了同样的问题,解决方案是添加一个标题
Origin:“https://ourapp.appspot.com“
发送到初始可恢复请求

但是,由于以下变量,某些库(例如
sun.net.www.protocol.http.HttpURLConnection
)不允许您更改原始标头:

restrictedHeaders=新字符串[]{“访问控制请求头”、“访问控制请求方法”、“连接”、“内容长度”、“内容传输编码”、“主机”、“保持活动状态”、“源站”、“拖车”、“传输编码”、“升级”、“通过”}

我的解决方法是使用一个库创建一个新的HttpRequest,该库允许更新
Origin
标题。我在我的案例中使用了Okhttp(作为前Android开发人员)

OkHttpClient=new-OkHttpClient();
AppIdentityService AppIdentityService=credential.getAppIdentityService();
集合作用域=credential.getScopes();
字符串accessToken=appIdentityService.getAccessToken(作用域).getAccessToken();
Request Request=newrequest.Builder()
.url(“https://www.googleapis.com/upload/storage/v1/b/“+bucket+”/o?name=“+fileName+”&uploadType=可恢复”)
.post(RequestBody.create(MediaType.parse(mimeType),新字节[0]))
.addHeader(“X-Upload-Content-Type”,mimeType)
.addHeader(“X-Upload-Content-Length”,“长度+长度”)
.addHeader(“原点”http://localhost:8080")
.addHeader(“原点”、“*”)
.addHeader(“授权”、“承载人”+访问令牌)
.build();
Response=client.newCall(request.execute();
返回响应。标题(“位置”);

我觉得不错。你是如何在Javascript中加载图像的?添加Javascript代码。我想使用来自多个域的云存储桶的对象,如何加载就足够了?是的,这应该足够了。谢谢。我现在期待结果。设置延迟?这不是一个有用的答案。请提供相关代码以支持此操作。