使用Azure Resource Manager模板配置CORS

时间:2016-12-15 21:47:08

标签: azure powershell cors azure-storage-blobs azure-resource-manager

我正在尝试使用Azure Resource Manager工具在配置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": []
    }
]

部署返回成功,我可以在Azure门户的活动日志下看到Write StorageAccount操作,但Cors Rules不会在任何地方添加,当我从Azure下载模板时,它没有这个“cors属性”。

我还尝试手动添加Corse规则(我只需要在我的Blob上),自动化脚本(包括deployment.ps)看起来仍然相同......

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

5 个答案:

答案 0 :(得分:3)

您的部署客户端是什么? 如果您使用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

答案 1 :(得分:2)

  

我正在尝试为我的存储帐户设置CORS规则

我创建了一个类似的ARM template来创建存储帐户资源,我发现它似乎无法识别/接受cors和其他属性(例如我定义的val),除了 accountType 属性

{
  "$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,表,队列和文件共享)配置Cors设置,似乎它无法让我们在存储中配置Cors设置部署存储帐户模板时直接帐户级别。 enter image description here

答案 2 :(得分:1)

存储帐户CORS当前不受存储资源提供程序支持,因此无法通过模板进行设置。正如Fred所指出的,CORS只能通过data plane API设置在服务上。

答案 3 :(得分:1)

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

经过测试,可以正常工作

答案 4 :(得分:0)

正如@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"
          }
        }
      ]
    }
  ]
}