Azure bicep存储帐户循环blob错误

fsi0uk1n  于 2023-01-31  发布在  其他
关注(0)|答案(2)|浏览(135)

我使用此脚本循环存储帐户阵列,并使用一些默认配置创建它们。

param storageAccounts array = [
  'storage2312'
  'storage2we1'
]

resource storage_Accounts 'Microsoft.Storage/storageAccounts@2021-04-01' = [ for storageName in storageAccounts :{
  name: storageName
  location: 'westeurope'
  sku: {
    name: 'Standard_RAGRS'
  }
  kind: 'StorageV2'
  properties: {
    allowCrossTenantReplication: true
    minimumTlsVersion: 'TLS1_2'
    allowBlobPublicAccess: false
    allowSharedKeyAccess: true
    networkAcls: {
      bypass: 'AzureServices'
      virtualNetworkRules: []
      ipRules: []
      defaultAction: 'Allow'
    }
    supportsHttpsTrafficOnly: true
    encryption: {
      services: {
        file: {
          keyType: 'Account'
          enabled: true
        }
        blob: {
          keyType: 'Account'
          enabled: true
        }
      }
      keySource: 'Microsoft.Storage'
    }
    accessTier: 'Hot'
  }
}]
resource storage_Accounts_name_default 'Microsoft.Storage/storageAccounts/blobServices@2021-04-01' = [ for (storageName, i) in storageAccounts :{
  parent: storage_Accounts[i]
  name: storageName
  properties: {
    changeFeed: {
      enabled: false
    }
    restorePolicy: {
      enabled: false
    }
    containerDeleteRetentionPolicy: {
      enabled: true
      days: 7
    }
    cors: {
      corsRules: []
    }
    deleteRetentionPolicy: {
      enabled: true
      days: 30
    }
    isVersioningEnabled: true
  }
}]

脚本运行正常,存储帐户也已创建,但脚本在最后抛出以下错误:

{"status":"Failed","error":{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.","details":[{"code":"NotFound","message":"{\r\n  \"error\": {\r\n    \"code\": \"HttpResourceNotFound\",\r\n    \"message\": \"The request url resourcegroups/storageAccounts/providers/Microsoft.Storage/storageAccounts/storage2we1/blobServices/storage2we1?api-version=2021-04-01 is not found.\"\r\n  }\r\n}"},{"code":"NotFound","message":"{\r\n  \"error\": {\r\n    \"code\": \"HttpResourceNotFound\",\r\n    \"message\": \"The request url /resourcegroups/storageAccounts/providers/Microsoft.Storage/storageAccounts/storage2312/blobServices/storage2312?api-version=2021-04-01 is not found.\"\r\n  }\r\n}"}]}}

手动创建存储帐户时,我将$logs作为默认容器,但当我运行脚本并创建存储帐户时,容器下没有任何内容,我认为错误是由于此原因。使用二头肌时,我是否必须声明默认容器?任何帮助和解释都是最受欢迎的。
非常感谢

ozxc1zmp

ozxc1zmp1#

根据提供的here示例,请将以下代码行(在您配置blob服务的第二个代码段中)从:

name: storageName

name: 'default'

因此,有效的二头肌模板如下所示:

param storageAccounts array = [
  'storage2'
]
resource storage_Accounts 'Microsoft.Storage/storageAccounts@2021-04-01' = [ for storageName in storageAccounts :{
  name: storageName
  location: 'westeurope'
  sku: {
    name: 'Standard_RAGRS'
    tier: 'Standard'
  }
  kind: 'StorageV2'
  properties: {
    allowCrossTenantReplication: true
    minimumTlsVersion: 'TLS1_2'
    allowBlobPublicAccess: false
    allowSharedKeyAccess: true
    networkAcls: {
      bypass: 'AzureServices'
      virtualNetworkRules: []
      ipRules: []
      defaultAction: 'Allow'
    }
    supportsHttpsTrafficOnly: true
    encryption: {
      services: {
        file: {
          keyType: 'Account'
          enabled: true
        }
        blob: {
          keyType: 'Account'
          enabled: true
        }
      }
      keySource: 'Microsoft.Storage'
    }
    accessTier: 'Hot'
  }
}]

resource storage_Accounts_name_default 'Microsoft.Storage/storageAccounts/blobServices@2021-04-01' = [ for (storageName, i) in storageAccounts :{
  parent: storage_Accounts[i]
  name: 'default'
  properties: {
    changeFeed: {
      enabled: false
    }
    restorePolicy: {
      enabled: false
    }
    containerDeleteRetentionPolicy: {
      enabled: true
      days: 7
    }
    cors: {
      corsRules: []
    }
    deleteRetentionPolicy: {
      enabled: true
      days: 30
    }
    isVersioningEnabled: true
  }
}]
wz8daaqr

wz8daaqr2#

下面是具有更多泛型的替代解决方案
要创建的容器的定义

var containerNames = [
  'web'
  'blob'
  'test'
]

接下来,我们需要定义存储帐户

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: replace(storageName, '-', '')
  location: location
  kind:'StorageV2'
  sku:{
    name: skuName
  }
  properties: {
    minimumTlsVersion:'TLS1_2'
    accessTier: 'Cool'
    allowBlobPublicAccess: true
    networkAcls:{
      bypass:'AzureServices'
      defaultAction:'Allow'
      virtualNetworkRules:[]
    }
    supportsHttpsTrafficOnly: true
  }
}

那么我们需要使用composition来定义容器

resource newContainers 'Microsoft.Storage/storageAccounts/blobServices/containers@2022-09-01' = [for containerName in containerNames:{
  name:'${storageAccount.name}/default/${containerName}'
  dependsOn: [storageAccount]
  properties:{
    publicAccess:'None'
  }  
}]

需要注意的几件事:

  • 此解决方案未针对无效输入进行清理
  • 存储帐户的名称区分大小写
  • 确保测试了代码

相关问题