如何在windows azure中为Blob存储配置CORS设置

Raj*_*lar 6 c# azure azure-storage

我在azure存储中创建了几个容器,并将一些文件上传到这些容器中.现在我需要给容器/ blob提供域级访问权限.所以我从代码级别尝试了它,如下所示.

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));

        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        ServiceProperties blobServiceProperties = new ServiceProperties();
            blobServiceProperties.Cors.CorsRules.Add(new CorsRule(){
                AllowedHeaders = new List<string>() {"*"},
                ExposedHeaders = new List<string>() {"*"},
                AllowedMethods = CorsHttpMethods.Post | CorsHttpMethods.Put | CorsHttpMethods.Get | CorsHttpMethods.Delete ,
                AllowedOrigins = new List<string>() { "http://localhost:8080/"},
                MaxAgeInSeconds = 3600,
            });

          blobClient.SetServiceProperties(GetBlobServiceProperties());  
Run Code Online (Sandbox Code Playgroud)

但是,如果我从代码创建所有内容,上面的代码似乎是有效的(如果我错了,请纠正我).我也觉得像设置下面这里,

 <CorsRule>
  <AllowedOrigins>http://www.contoso.com, http://www.fabrikam.com</AllowedOrigins>
  <AllowedMethods>PUT,GET</AllowedMethods>
  <AllowedHeaders>x-ms-meta-data*,x-ms-meta-target,x-ms-meta-source</AllowedHeaders>
  <ExposedHeaders>x-ms-meta-*</ExposedHeaders>
  <MaxAgeInSeconds>200</MaxAgeInSeconds>
</CorsRule>
Run Code Online (Sandbox Code Playgroud)

但我没有得到这个代码必须放在哪里.我的意思是在哪个文件中.或者从azure门户创建容器或blob时是否有任何CORS设置.请协助.任何帮助都会很明显.谢谢!

Nic*_*sen 12

以下回答了标题中实际提出的问题.似乎提问者已经知道如何在他的代码中做到这一点,但这是我对此的回答.不幸的是,MS推出的代码示例远非容易或清晰,所以我希望这有助于其他人.在这个解决方案中,您只需要一个CloudStorageAccount实例,您可以从中调用此函数(作为扩展方法).

//用法:

        // -- example usage (in this case adding a wildcard CORS rule to this account --

        CloudStorageAccount acc = getYourStorageAccount();

        acc.SetCORSPropertiesOnBlobService(cors => {
            var wildcardRule = new CorsRule() { AllowedMethods = CorsHttpMethods.Get, AllowedOrigins = { "*" } };
            cors.CorsRules.Add(wildcardRule);
            return cors;
        });
Run Code Online (Sandbox Code Playgroud)

//代码:

    /// <summary>
    /// Allows caller to replace or alter the current CorsProperties on a given CloudStorageAccount.
    /// </summary>
    /// <param name="storageAccount">Storage account.</param>
    /// <param name="alterCorsRules">The returned value will replace the 
    /// current ServiceProperties.Cors (ServiceProperties) value. </param>
    public static void SetCORSPropertiesOnBlobService(this CloudStorageAccount storageAccount,
        Func<CorsProperties, CorsProperties> alterCorsRules)
    {
        if (storageAccount == null || alterCorsRules == null) throw new ArgumentNullException();

        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        ServiceProperties serviceProperties = blobClient.GetServiceProperties();

        serviceProperties.Cors = alterCorsRules(serviceProperties.Cors) ?? new CorsProperties();

        blobClient.SetServiceProperties(serviceProperties);
    }
Run Code Online (Sandbox Code Playgroud)

考虑CorsRule类的属性可能会有所帮助:

        CorsRule corsRule = new CorsRule() {
            AllowedMethods = CorsHttpMethods.Get,       // Gets or sets the HTTP methods permitted to execute for this origin
            AllowedOrigins = { "*" },                   // (IList<string>) Gets or sets domain names allowed via CORS.
            //AllowedHeaders = { "*" },                 // (IList<string>) Gets or sets headers allowed to be part of the CORS request
            //ExposedHeaders = null,                    // (IList<string>) Gets or sets response headers that should be exposed to client via CORS
            //MaxAgeInSeconds = 33333                   // Gets or sets the length of time in seconds that a preflight response should be cached by browser
        };
Run Code Online (Sandbox Code Playgroud)


Gau*_*tri 4

让我尝试回答你的问题。如您所知,Azure Storage提供了用于管理存储内容的 REST API。那里有一个操作Set Blob Service Properties,您在那里做的事情之一就是管理 blob 服务的 CORS 规则。您在问题中包含的 XML 是此操作的请求负载。您提到的 C# 代码实际上是存储客户端库,它本质上是用 .Net 编写的 REST API 的包装器。因此,当您使用上面的代码时,它实际上会调用 REST API 并发送 XML。

现在讨论设置 CORS 规则的选项,有几种方法可以实现这一目标。如果您有兴趣以编程方式设置它们,那么您可以编写一些使用 REST API 的代码,也可以像上面所做的那样直接使用 .Net 存储客户端库。您可以简单地创建一个控制台应用程序,将代码放入其中并执行它来设置 CORS 规则。如果您正在寻找一些工具来执行此操作,那么您可以尝试以下工具之一: