Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure CORS by using Azure Resource Manager template

I'm trying to set CORS rule for my storage account as suggested here under Configure CORS by using Azure Resource Manager tools: https://docs.microsoft.com/en-us/azure/app-service-api/app-service-api-cors-consume-javascript

by adding property cors:

    "resources": [
    {
        "type": "Microsoft.Storage/storageAccounts",
        "sku": {
            "name": "Standard_RAGRS",
            "tier": "Standard"
        },
        "kind": "Storage",
        "name": "[parameters('storageAccounts_teststoragejkjk_name')]",
        "apiVersion": "2016-01-01",
        "location": "westus",
        "tags": {},
        "properties": {
            "cors": {"allowedOrigins": ["*"]}
        },
        "resources": [],
        "dependsOn": []
    }
]

Deployment returns succes and I can see Write StorageAccount operation under Activity Log in Azure Portal but Cors Rules aren't added anywhere and when i download template from Azure it doesn't have this "cors property".

I also tried manually adding Corse Rule (i need it only on my Blob) and automation scripts (including deployment.ps) still looks the same...

Any suggestion on how to configure Cors rule on blob storage using ARM templates?

like image 231
letlampa Avatar asked Dec 15 '16 21:12

letlampa


People also ask

What is the use of Azure Resource Manager templates?

Because they are native to the Azure platform, Resource Manager templates let you track your deployments in the portal, use deep integration with other Azure services and provision any Azure resource as soon as it is available.

How do I enable CORS in Azure Blob?

By default, CORS is disabled for each service. To enable CORS, you need to set the appropriate service properties using version 2013-08-15 or later for the Blob, Queue, and Table services, or version 2015-02-21 or for the File service. You enable CORS by adding CORS rules to the service properties.


2 Answers

As @JBA pointed out this now works via ARM templates.

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "name": "storageAccountName",
      "apiVersion": "2018-02-01",
      "location": "northeurope",
      "kind": "StorageV2",
      "sku": {
        "name": "Standard_LRS",
        "tier": "Standard"
      },
      "tags": {},
      "dependsOn": [],
      "properties": {
        "accessTier": "Hot"
      },
      "resources": [
        {
          "name": "default",
          "type": "blobServices",
          "apiVersion": "2018-11-01",
          "dependsOn": [
            "storageAccountName"
          ],
          "properties": {
            "cors": {
              "corsRules": [
                {
                  "allowedOrigins": [
                    "https://mywebsite.com"
                  ],
                  "allowedMethods": [
                    "GET"
                  ],
                  "maxAgeInSeconds": 0,
                  "exposedHeaders": [
                    "*"
                  ],
                  "allowedHeaders": [
                    "*"
                  ]
                }
              ]
            }
          },
          "resources": []
        },
        {
          "type": "blobServices/containers",
          "apiVersion": "2018-03-01-preview",
          "name": "[concat('default/', 'myFilesToShare')]",
          "dependsOn": [
            "storageAccountName"
          ],
          "properties": {
            "publicAccess": "Blob"
          }
        }
      ]
    }
  ]
}
like image 91
James Wood Avatar answered Sep 29 '22 23:09

James Wood


What is your deployment client? If you are using Powershell to deploy ARM (w you probably are) why not use Set-AzureStorageCORSRule?

PS C:\>$CorsRules = (@{
AllowedHeaders=@("x-ms-blob-content-type","x-ms-blob-content-disposition");
AllowedOrigins=@("*");
MaxAgeInSeconds=30;
AllowedMethods=@("Get","Connect")},
@{
AllowedOrigins=@("http://www.fabrikam.com","http://www.contoso.com");
ExposedHeaders=@("x-ms-meta-data*","x-ms-meta-customheader");
AllowedHeaders=@("x-ms-meta-target*","x-ms-meta-customheader");
MaxAgeInSeconds=30;
AllowedMethods=@("Put")})

PS C:\> Set-AzureStorageCORSRule -ServiceType Blob -CorsRules $CorsRules

like image 42
EvertonMc Avatar answered Sep 30 '22 00:09

EvertonMc