[英]Enable CORS for azure blob storage
我對Azure Blob Storage
完全Azure Blob Storage
,我在為CORS
客戶端設置CORS
遇到了問題。 我正在使用這個模塊。 我在我的nodejs
服務器上成功生成了一個sasToken
。
我還檢查了這些參考資料:設置 Blob 服務屬性、 Azure 存儲服務和Windows Azure 存儲的跨域資源共享 (CORS) 支持:引入 CORS,但我真的發現在何處執行/放置這些代碼感到困惑。
到目前為止,我所做的是:
在 AGULAR 控制器中:
// this is the service that generate the sasToken from the server
getService.getSasToken(filename)
.then(function (response) {
// response = {
// sasToken : "asa7sya....",
// token: "the shared key",
// account: "the storage account name"
// }
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
xhr.setRequestHeader("Content-Type","application/xml");
xhr.setRequestHeader("x-ms-version", "2013-08-15");
xhr.setRequestHeader("Authorization", response.token);
xhr.setRequestHeader("myaccount", response.account);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url, true);
xhr.setRequestHeader("Content-Type","application/xml");
xhr.setRequestHeader("x-ms-version", "2013-08-15");
xhr.setRequestHeader("Authorization", response.token);
xhr.setRequestHeader("myaccount", response.account);
} else {
xhr = null;
}
return xhr;
}
var xhr = createCORSRequest('PUT', 'https://foo.blob.core.windows.net?restype=service&comp=properties');
if (!xhr) {
throw new Error('CORS not supported');
}else{
// Response handlers.
xhr.onload = function() {
alert('Response from CORS request to ' + xhr.responseText);
azureBlob.upload({
baseUrl: "https://foo.blob.core.windows.net/bar/"+filename,
sasToken: "?"+response.sasToken,
file: blobFile,
progress: function (progress) {
console.log("progress", progress);
},
complete: function (complete) {
console.log("complete", complete);
},
error: function (error) {
console.log("error", error);
},
// blockSize: // What do I put here?
})
};
xhr.onerror = function() {
alert('Woops, there was an error making the request.');
};
$.ajax({
type: "GET",
url: "../scripts/cors.xml", // local xml file
dataType: "xml",
success: function(xml){
console.log("xml", xml);
xhr.send(xml);
}
});
}
},function (error) {
console.log(error);
})
文件格式
<?xml version="1.0" encoding="utf-8"?>
<StorageServiceProperties>
<Logging>
<Version>1.0</Version>
<Delete>true</Delete>
<Read>false</Read>
<Write>true</Write>
<RetentionPolicy>
<Enabled>true</Enabled>
<Days>7</Days>
</RetentionPolicy>
</Logging>
<HourMetrics>
<Version>1.0</Version>
<Enabled>true</Enabled>
<IncludeAPIs>false</IncludeAPIs>
<RetentionPolicy>
<Enabled>true</Enabled>
<Days>7</Days>
</RetentionPolicy>
</HourMetrics>
<MinuteMetrics>
<Version>1.0</Version>
<Enabled>true</Enabled>
<IncludeAPIs>true</IncludeAPIs>
<RetentionPolicy>
<Enabled>true</Enabled>
<Days>7</Days>
</RetentionPolicy>
</MinuteMetrics>
<Cors>
<CorsRule>
<AllowedOrigins>*</AllowedOrigins>
<AllowedMethods>GET,PUT,POST</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>
上面的代碼基於這個GUIDE
但是我仍然收到此錯誤:
以前有人這樣做過嗎? 請分享您的代碼。
提前致謝。
我為此花了兩天時間。 我必須在 blob 存儲 cors 設置中對原點進行硬編碼。 *
不起作用。 將您的客戶端 url 放在 cors 選項中而不是*
,我敢打賭情況會更好。 <AllowedOrigins>*</AllowedOrigins>
在開發時更改為您的本地主機。
HEAD
添加到所需的方法中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.