你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

解决存储帐户名称错误

本文描述如何使用 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 'westus'
in resource group 'demostorage'. A resource with the same name cannot be created in location 'eastus'.
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.

原因

导致错误的常见原因是,存储帐户名称使用无效字符或名称重复。 存储帐户名称必须满足以下条件:

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

解决方案

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

以下示例使用与 uniqueString 中的值连接的字符串 storage 指定一个前缀。

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)}'