使用 Azure 资源管理器模板配置 CORS

Configure CORS by using Azure Resource Manager template

我正在尝试按照使用 Azure 资源管理器工具配置 CORS 下的建议为我的存储帐户设置 CORS 规则:https://docs.microsoft.com/en-us/azure/app-service-api/app-service-api-cors-consume-javascript

通过添加 属性 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": []
    }
]

部署 returns 成功,我可以在 Activity 登录 Azure 门户中看到 Write StorageAccount 操作,但 Cors 规则没有添加到任何地方,当我从 Azure 下载模板时它没有这个"cors property".

我也尝试手动添加 Corse Rule(我只在我的 Blob 上需要它)和自动化脚本(包括 deployment.ps)看起来仍然一样...

关于如何使用 ARM 模板在 blob 存储上配置 Cors 规则有什么建议吗?

I'm trying to set CORS rule for my storage account

我创建了一个类似ARM template的存储账户资源,我发现好像没有recognize/acceptcors和其他属性(比如我定义的val)除了账户类型 属性.

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": { },
  "variables": { },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2015-06-15",
      "name": "[concat('storage', uniqueString(resourceGroup().id))]",
      "location": "[resourceGroup().location]",
      "properties": {
        "accountType": "Standard_LRS",
        "cors": {
          "allowedHeaders": [ "*" ],
          "allowedMethods": [ "get", "post", "put" ],
          "allowedOrigins": [ "*" ],
          "exposedHeaders": [ "*" ],
          "maximumAge": 5
        },
        "val": "123"
      }
    }
  ],
  "outputs": { }
}

此外,正如我们所知,我们可以为 azure 存储服务(blob、table、队列和文件共享)配置 Cors 设置,似乎确实如此在部署存储帐户模板时,我们无法直接在存储帐户级别配置 Cors 设置。

您的部署客户端是什么? 如果您使用 Powershell 部署 ARM(您可能是),为什么不使用 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

存储资源提供商目前不支持存储帐户CORS,因此无法通过模板设置。正如 Fred 指出的那样,只能通过 data plane API.

在服务上设置 CORS

我在谷歌搜索时遇到了这个话题。现在可以通过 ARM 模板在存储帐户的 blob 服务上设置 CORS https://docs.microsoft.com/en-us/azure/templates/microsoft.storage/2018-07-01/storageaccounts/blobservices

已测试并且有效

正如@JBA 指出的那样,现在可以通过 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"
          }
        }
      ]
    }
  ]
}