gpt4 book ai didi

谷歌云存储 CORS 设置

转载 作者:行者123 更新时间:2023-12-04 14:33:11 24 4
gpt4 key购买 nike

我已经将 CORS 设置为谷歌云存储桶,没有 Access-Control-Allow-Origin header

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

我的设置

$ 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 无法加载 http://storage.googleapis.com/stone-swallow/arcanine.png .请求的资源上不存在“Access-Control-Allow-Origin” header 。产地' http://localhost:8080 ' 因此不允许访问。

最佳答案

我遇到了同样的问题,解决方案是在初始可恢复请求中添加一个 Header Origin: "https://ourapp.appspot.com"

但是,一些库,例如 sun.net.www.protocol.http.HttpURLConnection 不允许您更改 Origin header ,因为以下变量:

restrictedHeaders = new String[]{"Access-Control-Request-Headers", "Access-Control-Request-Method", "Connection", "Content-Length", "Content-Transfer-Encoding", "Host", "Keep-Alive", "Origin", "Trailer", "Transfer-Encoding", "Upgrade", "Via"};

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

OkHttpClient client = new OkHttpClient();
AppIdentityService appIdentityService = credential.getAppIdentityService();
Collection<String> scopes = credential.getScopes();
String accessToken = appIdentityService.getAccessToken(scopes).getAccessToken();
Request request = new Request.Builder()
.url("https://www.googleapis.com/upload/storage/v1/b/" + bucket + "/o?name=" + fileName + "&uploadType=resumable")
.post(RequestBody.create(MediaType.parse(mimeType), new byte[0]))
.addHeader("X-Upload-Content-Type", mimeType)
.addHeader("X-Upload-Content-Length", "" + length)
.addHeader("Origin", "http://localhost:8080")
.addHeader("Origin", "*")
.addHeader("authorization", "Bearer "+accessToken)
.build();
Response response = client.newCall(request).execute();
return response.header("location");

关于谷歌云存储 CORS 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28923394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com