您好,我在 Microsoft Azure 云存储上遇到 Cors 错误。
我正在按如下方式生成我的 SAS:
public SAS(string containername, string id, SharedAccessBlobPermissions permissions)
{
this.id = id;
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobContainer containerReference = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference("teeessssttt");
if (containerReference.CreateIfNotExists(null, null))
{
BlobConfig.configureCORS();
}
CloudBlockBlob blockBlobReference = containerReference.GetBlockBlobReference(id);
SharedAccessBlobPolicy sharedAccessBlobPolicy = new SharedAccessBlobPolicy()
{
Permissions = permissions
};
DateTime utcNow = DateTime.UtcNow;
sharedAccessBlobPolicy.SharedAccessStartTime = new DateTimeOffset?(utcNow.AddMinutes(-5));
DateTime dateTime = DateTime.UtcNow;
sharedAccessBlobPolicy.SharedAccessExpiryTime = new DateTimeOffset?(dateTime.AddMinutes(15));
string sharedAccessSignature = blockBlobReference.GetSharedAccessSignature(sharedAccessBlobPolicy);
this.HostingSite = blockBlobReference.Uri.AbsoluteUri;
this.Token = sharedAccessSignature;
this.fullurl = string.Concat(this.HostingSite, this.Token);
}
这会产生类似的东西:
{"fullurl":"https://ingenovatebeta.blob.core.windows.net/teeessssttt/7554eb72-f361-4715-a255-a5d6922706b0?sv=2013-08-15&sr=b&sig=iVL4zeytgC1H7W09lV2YZspOKlzF433oqsx9I6cfI68%3D&st=2014-10-24T05%3A58%3A13Z&se=2014-10-24T06%3A18%3A13Z&sp=w","HostingSite":"https://ingenovatebeta.blob.core.windows.net/teeessssttt/7554eb72-f361-4715-a255-a5d6922706b0","id":"7554eb72-f361-4715-a255-a5d6922706b0","Token":"?sv=2013-08-15&sr=b&sig=iVL4zeytgC1H7W09lV2YZspOKlzF433oqsx9I6cfI68%3D&st=2014-10-24T05%3A58%3A13Z&se=2014-10-24T06%3A18%3A13Z&sp=w"}
我正在按如下方式设置我的cors:
CorsRule corsRule = new CorsRule();
corsRule.AllowedOrigins.Add("https://---------------.com");
corsRule.AllowedMethods = CorsHttpMethods.Get | CorsHttpMethods.Post | CorsHttpMethods.Put |
CorsHttpMethods.Options;
corsRule.AllowedHeaders.Add("*");
corsRule.ExposedHeaders.Add("*");
corsRule.MaxAgeInSeconds = (int)TimeSpan.FromDays(5).TotalSeconds;
corsRules.Add(corsRule);
serviceProperty.Cors = corsProperty;
当我仅将文件上传到 GUID 时,这很有效。例如,将我的上传 URI 设置如下:
$.get("/api/sas", (function (json) {
var baseUrl = json.fullurl;
myDropzone.options.url = baseUrl;
myDropzone.processFile(file);
}));
然而,我的所有文件最终都作为 GUID 存储在我的云存储中,没有文件名或扩展名,这使得检索图像等变得困难。在显示的示例中,您可以添加文件名。我遵循了以下示例:
$.get("/api/sas", (function (json) {
var baseUrl = json.fullurl;
var indexOfQueryStart = baseUrl.indexOf("?");
submitUri = baseUrl.substring(0, indexOfQueryStart) + '/' + file.name + baseUrl.substring(indexOfQueryStart);
myDropzone.options.url = submitUri;
myDropzone.processFile(file);
}));
但是它不起作用。此时我收到一个 CORS 错误:
XMLHttpRequest cannot load https://myurl/mycontainer/82b7f14b-ee7c-45a3…2B%2BmtAJw%3D&st=2014-10-23T04%3A34%3A22Z&se=2014-10-23T04%3A54%3A22Z&sp=w. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://stagingrapidrealities.azurewebsites.net' is therefore not allowed access. The response had HTTP status code 403.