解决存储帐户名称错误

本文描述如何使用 Bicep 文件或 Azure 资源管理器模板(ARM 模板)解决在部署期间可能出现的 Azure 存储帐户名称错误。 导致错误的常见原因包括:存储帐户名称具有无效字符,或者存储帐户使用与现有存储帐户相同的名称。 存储帐户名称在 Azure 中必须是全局唯一的。

症状

在部署期间,存储账户名称无效会导致出现错误代码。 下面是存储帐户名称错误的一些示例。

帐户名称无效

出现此错误的情况是,存储帐户名称包含禁用字符,例如大写字母或特殊字符(如感叹号)。

Code=AccountNameInvalid
Message=S!torageckrexph7isnoc is not a valid storage account name. Storage account name must be
between 3 and 24 characters in length and use numbers and lower-case letters only.

无效的资源位置

如果尝试在相同的资源组中部署一个同名但使用不同位置的新存储帐户,作为 Azure 订阅中的现有存储帐户。 该错误指示已存在存储帐户且无法在新位置创建该帐户。 选择其他名称来创建新的存储账户。

Code=InvalidResourceLocation
Message=The resource 'storageckrexph7isnoc' already exists in location 'chinanorth'
in resource group 'demostorage'. A resource with the same name cannot be created in location 'chinanorth'.
Please select a new resource name.

其他资源组中的存储帐户

如果尝试在订阅中的另一个资源组中部署一个名称和位置与现有存储帐户相同的新存储帐户。

Code=StorageAccountInAnotherResourceGroup
Message=The account storageckrexph7isnoc is already in another resource group in this subscription.

存储帐户已被占用

如果您尝试部署一个与 Azure 中已存在的存储帐户同名的新存储帐户。 现有的存储帐户名可能存在于你的订阅、租户,或是整个 Azure 中的任意位置。 存储帐户名称在 Azure 中必须是全局唯一的。

Code=StorageAccountAlreadyTaken
Message=The storage account named storageckrexph7isnoc is already taken.

此错误有两个主要原因。

原因 1

存储帐户名称使用了无效字符或为重复名称。 存储帐户名称必须满足以下条件:

  • 长度介于 3 至 24 个字符之间,只包含小写字母和数字。
  • 在 Azure 中必须全局唯一。 存储帐户名称在 Azure 中不能重复。

解决方案 1

可通过将前缀或后缀与 uniqueString 函数中的值进行连接来创建唯一名称。

以下示例指定了一个前缀,该前缀由字符串 storage 与来自 uniqueString 的值连接而成。

Bicep 使用带有 uniqueString字符串内插

resource storageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' = {
  name: 'storage${uniqueString(resourceGroup().id)}'

请确保存储帐户名称不超过 24 个字符。 uniqueString 函数返回 13 个字符。 如果要连接前缀或后缀,请提供一个不超过 11 个字符的值。

以下示例使用一个名为 storageNamePrefix 的参数,它会创建一个最多包含 11 个字符的前缀。

@description('The prefix value for the storage account name.')
@maxLength(11)
param storageNamePrefix string = 'storage'

然后,将 storageNamePrefix 参数的值与 uniqueString 值连接起来创建一个存储帐户名称。

name: '${storageNamePrefix}${uniqueString(resourceGroup().id)}'