【问题标题】:ARM - How can I get the access key from a storage account to use in AppSettings later in the template?ARM - 如何从存储帐户获取访问密钥,以便稍后在模板中的 AppSettings 中使用?
【发布时间】:2016-01-18 11:09:26
【问题描述】:

我正在创建一个 Azure 资源管理器模板,用于实例化多个资源,包括一个 Azure 存储帐户和一个带有 Web 应用程序的 Azure 应用程序服务。

我希望能够从新创建的存储帐户中捕获主访问密钥(或完整的连接字符串,无论哪种方式都可以),并将其用作 Web 的 AppSettings 之一的值应用程序。

这可能吗?

【问题讨论】:

    标签: azure-storage azure-resource-manager


    【解决方案1】:

    使用listkeys 辅助函数。

    "appSettings": [
        {
          "name": "STORAGE_KEY",
          "value": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value]"
        }
    ]
    

    这个快速入门做了类似的事情:

    https://azure.microsoft.com/en-us/documentation/articles/cache-web-app-arm-with-redis-cache-provision/

    【讨论】:

    • 我将 .key1 添加到您的条目中(这是获取主访问密钥的属性),但这让我继续前进。谢谢你,也感谢艾米丽。
    【解决方案2】:

    自从另一个答案被接受后,语法发生了变化。你现在遇到的错误是'Template language expression property 'key1' doesn't exist, available properties are 'keys'

    键现在表示为键数组,语法现在是:

    "StorageAccount": "[Concat('DefaultEndpointsProtocol=https;AccountName=',variables('StorageAccountName'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('StorageAccountName')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value)]",
    

    见:http://samcogan.com/retrieve-azure-storage-key-in-arm-script/

    【讨论】:

    • 为了避免被这个 和未来 更改破坏,请使用接受 api 版本的 listKeys 重载。喜欢listKeys(resourceId(...), '2015-05-01-preview').key1
    【解决方案3】:

    我两次遇到这个问题。第一次是在 2015 年,最后一次是在 2017 年 5 月。 我需要将连接字符串添加到 WebApp - 我想在从 ARM 模板部署期间从生成的资源中自动添加字符串。以后不要手动添加此值会有所帮助。

    我第一次使用旧版本的函数 listKeys(看起来旧版本返回的结果不是作为对象而是作为值):

    "AzureWebJobsStorage": {
        "type": "Custom",
        "value": "[concat(variables('storageConnectionString'), listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')), '2015-05-01-preview').key1)]"
    },
    

    今天工作模板的最后一个版本是:

    "resources": [
        {
          "apiVersion": "2015-08-01",
          "type": "config",
          "name": "connectionstrings",
          "dependsOn": [
            "[resourceId('Microsoft.Web/Sites/', parameters('webSiteName'))]"
          ],
          "properties": {
            "DefaultConnection": {
              "value": "[concat('Data Source=tcp:', reference(resourceId('Microsoft.Sql/servers/', parameters('sqlserverName'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', parameters('databaseName'), ';User Id=', parameters('administratorLogin'), '@', parameters('sqlserverName'), ';Password=', parameters('administratorLoginPassword'), ';')]",
              "type": "SQLServer"
            },
            "AzureWebJobsStorage": {
              "type": "Custom",
              "value": "[concat(variables('storageConnectionString'), listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageName')), '2016-01-01').keys[0].value)]"
            },
            "AzureWebJobsDashboard": {
              "type": "Custom",
              "value": "[concat(variables('storageConnectionString'), listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageName')), '2016-01-01').keys[0].value)]"
            }
          }
        },
    

    谢谢。

    【讨论】:

    【解决方案4】:

    以下是向 ADLA 添加存储帐户的示例

    "storageAccounts": [
                      {
                        "name": "[parameters('DataLakeAnalyticsStorageAccountname')]",
                        "properties": {
                          "accessKey": "[listKeys(variables('storageAccountid'),'2015-05-01-preview').key1]"
                        }
                    }
                ],
    

    在变量中你可以保留

    "variables": {
            "apiVersion": "[providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]]",
             "storageAccountid": "[concat(resourceGroup().id,'/providers/','Microsoft.Storage/storageAccounts/', parameters('DataLakeAnalyticsStorageAccountname'))]"
        },
    

    【讨论】:

      猜你喜欢
      • 2020-05-20
      • 1970-01-01
      • 2019-04-06
      • 2018-02-09
      • 1970-01-01
      • 2021-04-06
      • 2017-01-02
      • 2016-06-28
      • 2021-02-16
      相关资源
      最近更新 更多