如何使用ARM模板在Azure Data Lake容器中创建文件夹
ARM模板创建ADLS Gen2容器内目录的解决方案
你之前的写法存在错误,Microsoft.Storage/storageAccounts/blobServices/containers 资源仅用于创建容器本身,无法通过拼接路径的方式直接创建容器下的目录。ADLS Gen2启用层级命名空间(HNS)后,目录是独立的资源对象,原生ARM目前没有直接对应的资源类型可直接创建。
推荐方案:使用ARM原生部署脚本(deploymentScript)资源实现
该方案所有逻辑都封装在ARM模板内完成,不需要你手动操作存储账户,只要拥有ARM部署权限即可。模板会自动创建系统托管身份并分配对应存储账户的「存储Blob数据贡献者」权限,完成目录创建后自动清理临时资源。
完整可运行ARM模板示例
{ "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "storageAccountDLName": { "type": "string" }, "sku": { "type": "string", "defaultValue": "Standard_LRS" }, "targetContainerName": { "type": "string", "defaultValue": "eventconnector-transformed-data-fs" }, "targetDirectoryName": { "type": "string", "defaultValue": "debugging" }, "location": { "type": "string", "defaultValue": "[resourceGroup().location]" } }, "variables": { "storageAccountId": "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountDLName'))]", "blobContributorRoleId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'ba92f5b4-2d11-453d-a403-e96b0029c9fe')]" }, "resources": [ { "type": "Microsoft.Storage/storageAccounts", "apiVersion": "2021-02-01", "sku": { "name": "[parameters('sku')]", "tier": "Standard" }, "kind": "StorageV2", "name": "[parameters('storageAccountDLName')]", "location": "[parameters('location')]", "properties": { "isHnsEnabled": true, "networkAcls": { "bypass": "AzureServices", "virtualNetworkRules": [], "ipRules": [], "defaultAction": "Allow" } }, "resources": [ { "type": "blobServices/containers", "name": "[concat('default/', parameters('targetContainerName'))]", "apiVersion": "2021-02-01", "properties": {}, "dependsOn": [ "[variables('storageAccountId')]" ] } ] }, { "type": "Microsoft.Authorization/roleAssignments", "apiVersion": "2020-04-01-preview", "name": "[guid(resourceGroup().id, 'adlsDirCreateRole', parameters('storageAccountDLName'))]", "properties": { "roleDefinitionId": "[variables('blobContributorRoleId')]", "principalId": "[reference('createAdlsDirectory', '2020-10-01').identity.principalId]", "principalType": "ServicePrincipal", "scope": "[variables('storageAccountId')]" }, "dependsOn": [ "[variables('storageAccountId')]" ] }, { "type": "Microsoft.Resources/deploymentScripts", "apiVersion": "2020-10-01", "name": "createAdlsDirectory", "location": "[parameters('location')]", "kind": "AzureCLI", "identity": { "type": "SystemAssigned" }, "properties": { "azCliVersion": "2.30.0", "scriptContent": "az storage fs directory create --account-name ${STORAGE_ACCOUNT} --file-system ${CONTAINER_NAME} --name ${DIRECTORY_NAME} --auth-mode login", "environmentVariables": [ { "name": "STORAGE_ACCOUNT", "value": "[parameters('storageAccountDLName')]" }, { "name": "CONTAINER_NAME", "value": "[parameters('targetContainerName')]" }, { "name": "DIRECTORY_NAME", "value": "[parameters('targetDirectoryName')]" } ], "retentionInterval": "PT1H", "cleanupPreference": "OnSuccess" }, "dependsOn": [ "[resourceId('Microsoft.Storage/storageAccounts/blobServices/containers', parameters('storageAccountDLName'), 'default', parameters('targetContainerName'))]", "[resourceId('Microsoft.Authorization/roleAssignments', guid(resourceGroup().id, 'adlsDirCreateRole', parameters('storageAccountDLName')))]" ] } ] }
注意事项
- 如果需要创建多级目录,直接修改
targetDirectoryName参数为完整路径即可,比如a/b/c,命令会自动递归创建所有父级目录 - 脚本执行成功后会自动清理临时资源,不会在资源组中留下冗余对象
替代方案
如果你使用CI/CD工具(如Azure DevOps、GitHub Actions)做部署流程,也可以在ARM部署完成的步骤后,新增Azure CLI/PowerShell任务,执行同样的az storage fs directory create命令创建目录,逻辑和部署脚本方案一致,仅将执行逻辑放到流水线中即可。
内容的提问来源于stack exchange,提问作者abautista
相关产品推荐
相关产品推荐

