各位好:

近日在开发一款React应用时,遇到一个奇怪的问题。我在 Leacloud 有开两个应用,一个是测试的,另一个是正式的应用,两个设定都一样。我在测试的环境下用下面的代码可以成功上传图档,但同样的代码,在正式的环境却无法运行。

上传图片的代码:

let uploadFile = new AV.File(file.name, file);
uploadFile.save().then((result) => {
  return callback(null, result);
}, (error) => {
  console.error(error);
  return callback(error);
});

Chrome Debugger 显示的错误讯息:

Failed to load https://avos-cloud-fmnjeeu8un2b.s3.amazonaws.com/b37......: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 403.

我的环境是:
Chrome: v65
JS SDK : leancloud-storage 3.6.3
React: 16.2.0
使用美国节点

请稍候,我确认一下问题。

感谢帮忙,请问问题有确认了吗? 稍早有再测试一次,问题依旧存在

我们检查了这个应用的 CORS 配置,没有问题。

The response had HTTP status code 403

能在 network 中找到这个 403 的 OPTIONS 请求然后右键 copy as cURL 吗?

好的,我 copy了所有的 HTTP 请求,第三个 curl 发生了 403 error

curl 'https://us-api.leancloud.cn/1.1/fileTokens' -X OPTIONS -H 'pragma: no-cache' -H 'access-control-request-headers: content-type,x-lc-id,x-lc-session,x-lc-sign,x-lc-ua' -H 'access-control-request-method: POST' -H 'origin: http://localhost:8080' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7' -H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' -H 'accept: */*' -H 'cache-control: no-cache' -H 'authority: us-api.leancloud.cn' -H 'dnt: 1' --compressed ;

curl 'https://us-api.leancloud.cn/1.1/fileTokens' -H 'x-lc-ua: LeanCloud-JS-SDK/3.6.4 (Browser)' -H 'origin: http://localhost:8080' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7' -H 'x-lc-sign: ...' -H 'pragma: no-cache' -H 'x-lc-session: ...' -H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' -H 'content-type: application/json;charset=UTF-8' -H 'accept: */*' -H 'cache-control: no-cache' -H 'authority: us-api.leancloud.cn' -H 'referer: http://localhost:8080/' -H 'x-lc-id: ...' -H 'dnt: 1' --data-binary '{"key":"3164b80ac96c94cad6a3.png","name":"97_test.png","metaData":{"size":22367,"owner":"5abda909f84da5fa7623268f"}}' --compressed ;

下面这个发生403问题

curl 'https://avos-cloud-fmnjeeu8un2b.s3.amazonaws.com/3164b80ac96c94cad6a3.png?x-amz-acl=public-read&Signature=3q0zOEPjVcmFf%2BSUA5TaHO9uc4Q%3D&AWSAccessKeyId=AKIAJ7Z2RFWPKWEGGHEQ&Expires=1522644300' -X OPTIONS -H 'Pragma: no-cache' -H 'Access-Control-Request-Method: PUT' -H 'Origin: http://localhost:8080' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' -H 'Accept: */*' -H 'Cache-Control: no-cache' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Access-Control-Request-Headers: cache-control,content-type' --compressed ;

curl 'https://us-api.leancloud.cn/1.1/fileCallback' -X OPTIONS -H 'pragma: no-cache' -H 'access-control-request-headers: content-type,x-lc-id,x-lc-session,x-lc-sign,x-lc-ua' -H 'access-control-request-method: POST' -H 'origin: http://localhost:8080' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7' -H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' -H 'accept: */*' -H 'cache-control: no-cache' -H 'authority: us-api.leancloud.cn' -H 'dnt: 1' --compressed ;

curl 'https://us-api.leancloud.cn/1.1/fileCallback' -H 'x-lc-ua: LeanCloud-JS-SDK/3.6.4 (Browser)' -H 'origin: http://localhost:8080' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7' -H 'x-lc-sign: ...' -H 'pragma: no-cache' -H 'x-lc-session: ...' -H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' -H 'content-type: application/json;charset=UTF-8' -H 'accept: */*' -H 'cache-control: no-cache' -H 'authority: us-api.leancloud.cn' -H 'referer: http://localhost:8080/' -H 'x-lc-id: ...' -H 'dnt: 1' --data-binary '{"token":"vW8xPco9zXE8Hxkq","result":false}' --compressed

已复现正在调查。

你好,已经修复了,很抱歉给您带来的不便。这个问题是因为您的正式应用的创建时间比较早,当时创建的 bucket 有一个 bug。

可以上传了~ 非常感谢!!