【问题标题】:Configure CORS by using Azure Resource Manager template使用 Azure 资源管理器模板配置 CORS
【发布时间】:2017-05-01 15:04:49
【问题描述】:

我正在尝试按照使用 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": []
    }
]

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我认为没有办法通过模板做到这一点,所以我同意你的看法:
      • 我使用了 Set-AzureStorageCORSRule -ServiceType Blob -CorsRules $CorsRules -Context $StorageAccountContext
      • @letlampa 很高兴为您提供帮助 :) 不要忘记将问题标记为已回答
      【解决方案3】:

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

      我创建了一个类似的ARM template 来创建存储帐户资源,我发现它似乎无法识别/接受除accountType 属性之外的cors 和其他属性(例如我定义的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、表、队列和文件共享)配置 Cors 设置,似乎它无法让我们在存储中配置 Cors 设置部署存储帐户模板时直接帐户级别。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

          已经测试过了

          【讨论】:

          • 能否提供您用于测试的模板
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-08
          相关资源
          最近更新 更多