我正在尝试使用适用于Windows Azure的PHP SDK,它不支持CORS,也不支持共享访问密钥。
在重写SDK以允许我生成SAS和必要的查询字符串,并为感恩节更新重写它(允许CORS通过REST API更新到Storage)之后,我遇到了一个困境。
我成功地创建了SAS,并且可以通过它们读取文件。同样,我的CORS请求如下:
<StorageServiceProperties>
<Cors>
<CorsRule>
<AllowedOrigins>
*
</AllowedOrigins>
<AllowedMethods>
GET, PUT, POST, DELETE, HEAD, OPTIONS
</AllowedMethods>
<MaxAgeInSeconds>
500
</MaxAgeInSeconds>
<ExposedHeaders>
x-ms-meta-data*,x-ms-meta-customheader
</ExposedHeaders>
<AllowedHeaders>
x-ms-meta-target*,x-ms-meta-customheader
</AllowedHeaders>
</CorsRule>
</Cors>
<DefaultServiceVersion>
2013-08-15
</DefaultServiceVersion>
</StorageServiceProperties>
我收到响应HTTP_Request2_Response Object ( [version:protected] => 1.1 [code:protected] => 202 [reasonPhrase:protected] => Accepted [effectiveUrl:protected] => <mysite>?restype=service&comp=properties [headers:protected] => Array ( [transfer-encoding] => chunked [server] => Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 [x-ms-request-id] => 59bc0e05-4767-4953-9df1-2d2a6a9054bc [x-ms-version] => 2013-08-15 [date] => Fri, 13 Dec 2013 11:23:49 GMT ) [cookies:protected] => Array ( ) [lastHeader:protected] => date [body:protected] => [bodyEncoded:protected] => 1 )
我还检查了输入的任何部分,它确实失败了,并发送了400个失败响应。
所以现在我在我的网页上写了一个应用程序,它应该允许用户直接将文件上传到存储帐户,但我仍然收到错误
OPTIONS <mystorage> 403 (Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.) jquery.js:8706
OPTIONS <mystorage> No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<mysite>' is therefore not allowed access. jquery.js:8706
XMLHttpRequest cannot load <mystorage>. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin <mysite> is therefore not allowed access. azure_blob.html:1
error
我不知道从这里到哪里继续。。。我觉得我已经启用了CORS,我觉得SAS正在工作,但它告诉我的存储帐户CORS支持已关闭,我甚至不知道如何通过REST API/Azure SDK 检查这是否属实
编辑:谢谢Guarav。。。问题确实是将content-type
添加到我的头中
对于任何很快看到这一点的人:我会修复代码,让它变得漂亮,并实现一些其他事情,然后向PHP SDK提交一个pull请求,这样你就不必再经历这些了。
我只针对新的更新实现了CORS。在提交请求之前,我将进入并实现其他serviceProperty更新,也许还有其他事情。
尝试在CORS请求的<AllowedHeaders>
部分中添加以下标题:
- x-ms-*(将处理头类型x-ms-blob-type等)
- 内容长度(我在使用CORS的实验中注意到,这是浏览器自动添加的,如果我不将其包含在CORS标题中,我会得到403错误)
- Accept(我在使用CORS的实验中注意到,这是浏览器自动添加的,如果我不将其包含在CORS头中,我会收到403错误)
- 内容类型
或者,您可以通过Fiddler
等工具跟踪请求,查看请求中发送的所有请求头,并将它们与CORS规则进行匹配。