Search code examples
azurecorsazure-storageazure-blob-storage

Setting CORS rules using Azure.Storage.Blobs


I'm trying to migrate from the deprecated Microsoft.WindowsAzure.Storage to Azure.Storage. In my API app, I have a method that I call occasionally to programmatically set the CORS rules in my Azure Storage account.

How do I add CORS rules to the properties using the new Azure.Storage.Blobs?

My original code that worked under Microsoft.WindowsAzure.Storage is as follows. In the following code, the _client is an instance of CloudBlobClient. I understand that in Azure.Storage.Blobs, I need to use BlobServiceClient which I now do but as I said, some parts of the following code are not working because some methods/properties are no longer there. I'm sure they're moved somewhere else but I haven't been able to figure out where.

public async Task ConfigureCors()
{
    var ALLOWED_CORS_ORIGINS = new List<String> { "http://localhost:49065", "https://myappdomain.com", "https://www.myappdomain", "https://login.microsoftonline.com" };
    var ALLOWED_CORS_HEADERS = new List<String> { "x-ms-meta-qqfilename", "Content-Type", "x-ms-blob-type", "x-ms-blob-content-type" };
    const CorsHttpMethods ALLOWED_CORS_METHODS = CorsHttpMethods.Get | CorsHttpMethods.Delete | CorsHttpMethods.Put | CorsHttpMethods.Options;
    const int ALLOWED_CORS_AGE_DAYS = 5;

    var properties = await _client.GetServicePropertiesAsync();

    properties.DefaultServiceVersion = "2013-08-15";
    await _client.SetServicePropertiesAsync(properties);

    var addRule = true;
    if (addRule)
    {
       var ruleWideOpenWriter = new CorsRule()
       {
           AllowedHeaders = ALLOWED_CORS_HEADERS,
           AllowedOrigins = ALLOWED_CORS_ORIGINS,
           AllowedMethods = ALLOWED_CORS_METHODS,
           MaxAgeInSeconds = (int)TimeSpan.FromDays(ALLOWED_CORS_AGE_DAYS).TotalSeconds
       };
       properties.Cors.CorsRules.Clear();
       properties.Cors.CorsRules.Add(ruleWideOpenWriter);
       await _client.SetServicePropertiesAsync(properties);
    }
}

Looks like I can get and set properties by changing _client.GetServicePropertiesAsync() to _client.GetPropertiesAsync() but DefaultServiceVersion is no longer there. Also I can't seem to find the right way to set CORS rules.

I'd appreciate your suggestions. Thanks!


Solution

  • You can use the code below when using Azure.Storage.Blobs(I'm using sync method, please change it to async method if you need that):

            var properties = blobServiceClient.GetProperties().Value;
    
            properties.DefaultServiceVersion = "xxx";
    
            BlobCorsRule rule = new BlobCorsRule();
            rule.AllowedHeaders= "x-ms-meta-qqfilename,Content-Type,x-ms-blob-type,x-ms-blob-content-type";
            rule.AllowedMethods = "GET,DELETE,PUT,OPTIONS";
            rule.AllowedOrigins = "http://localhost:49065,https://myappdomain.com,https://www.myappdomain,https://login.microsoftonline.com";
            rule.MaxAgeInSeconds = 3600; // in seconds
    
            properties.Cors.Add(rule);
    
            blobServiceClient.SetProperties(properties);