为 VM 映像生成 SAS URI

注意

共享访问签名 (SAS) URI 可用于发布你的虚拟机 (VM)。 或者,你可以通过 Azure 计算库在合作伙伴中心共享映像。 请参阅使用已批准的基础映像创建虚拟机使用自己的映像创建虚拟机以了解详细说明。

开始之前,需要执行以下操作:

从 VM 提取 VHD

注意

如果已在存储帐户中上传了 VHD,则可以跳过此步骤。

要从 VM 中提取 VHD,你需要先创建 VM 磁盘的快照,然后将 VHD 从快照中提取到你的存储帐户中。

创建 VM 磁盘的快照

  1. 登录到 Azure 门户
  2. 选择“创建资源”,然后搜索并选择“快照”。
  3. 在“快照”边栏选项卡中选择“创建”。
  4. 选择“订阅”。 选择所选订阅中的现有资源组或创建新资源组,并输入要创建的新资源组的名称。 这是快照将关联的资源组。
  5. 输入快照的“名称” 。
  6. 对于“源类型”,选择“磁盘”。
    1. 选择“源订阅”,即包含 VM 磁盘的订阅。 这可能不同于新快照的目标订阅。
  7. 对于源磁盘,选择要获取其快照的托管磁盘。
  8. 对于“存储类型”,请选择“标准 HDD”,除非你需要将其存储在高性能 SSD 上。
  9. 选择“查看 + 创建” 。 成功验证后,选择“创建”。

将 VHD 提取到你的存储帐户中

使用以下脚本将快照导出到你的存储帐户中的某个 VHD。 对于每个参数,相应地插入你的信息。

#Provide the subscription Id where the snapshot is created
subscriptionId=yourSubscriptionId

#Provide the name of your resource group where the snapshot is created
resourceGroupName=myResourceGroupName

#Provide the snapshot name
snapshotName=mySnapshot

#Provide Shared Access Signature (SAS) expiry duration in seconds (such as 3600)
#Know more about SAS here: https://learn.microsoft.com/azure/storage/storage-dotnet-shared-access-signature-part-1
sasExpiryDuration=3600

#Provide storage account name where you want to copy the underlying VHD file.
storageAccountName=mystorageaccountname

#Name of the storage container where the downloaded VHD will be stored.
storageContainerName=mystoragecontainername

#Provide the access key for the storage account that you want to copy the VHD to.
storageAccountKey=mystorageaccountkey

#Give a name to the destination VHD file to which the VHD will be copied.
destinationVHDFileName=myvhdfilename.vhd

az account set --subscription $subscriptionId

sas=$(az snapshot grant-access --resource-group $resourceGroupName --name $snapshotName --duration-in-seconds $sasExpiryDuration --query [accessSas] -o tsv)

az storage blob copy start --destination-blob $destinationVHDFileName --destination-container $storageContainerName --account-name $storageAccountName --account-key $storageAccountKey --source-uri $sas

此脚本使用以下命令生成快照的 SAS URI,然后使用该 SAS URI 将基础 VHD 复制到存储帐户。

命令 说明
az disk grant-access 生成只读 SAS,使用该 SAS 可以将基础 VHD 文件复制到存储帐户或将其下载到本地
az storage blob copy start 将 Blob 从一个存储帐户异步复制到另一个存储帐户。 使用 az storage blob show 检查新 Blob 的状态。

生成 SAS URI

有两个用于创建 SAS 地址 (URI) 的常用工具:

  • Azure 存储浏览器 – 在 Azure 门户上提供。
  • Azure CLI – 建议用于非 Windows 操作系统,以及自动化或连续集成环境。

使用工具 1:Azure 存储资源浏览器

  1. 转到你的存储帐户
  2. 打开“存储浏览器”并选择“Blob 容器”。
  3. 在“容器”中,右键单击 VHD 文件,然后选择“生成 SAS”。
  4. 在显示的“共享访问签名”菜单中填写以下字段:
    • 权限– 选择读取权限。 不要提供“写入”或“删除”权限。
    • 开始日期/时间 - 这是 VHD 访问权限的开始日期。 若要防范 UTC 时间更改,提供的日期要比当前日期早一天。 例如,如果当前日期是 2022 年 7 月 15 日,则将日期设置为 07/14/2022。
    • 到期日期/时间 – 这是 VHD 访问的权限到期日期。 请提供自当前日期开始算起的至少三周后的日期。
  5. 若要为此 VHD 创建关联的 SAS URI,请选择“生成 SAS 令牌和 URL”。
  6. 复制 Blob SAS URL,并将其保存到位于安全位置的某个文本文件。
  7. 对要发布的每个 VHD 重复这些步骤。

注意

如果需要生成具有读取和列出权限的 SAS URI,可以通过在容器级别生成它来实现。 请执行上述步骤,但请确保同时选择读取权限和列出权限。 完成步骤 6 后,需要在“?sp=rl”之前添加 blob 文件名,以引用容器中的 VHD 文件。 例如,如果 VHD 文件为“mytestvhd.vhd”,则 SAS URI 将如下所示: <container-service-endpoint/mytestvhd.vhd?<sas-connection-string

使用工具 2:Azure CLI

  1. 在 Azure CLI 中,运行以下命令:Azure CLICopy

  2. 打开 Cloud Shell,运行以下命令: az storage container generate-sas --connection-string 'DefaultEndpointsProtocol=https;AccountName=\<account-name\>;AccountKey=\<account-key\>;EndpointSuffix=core.windows.net' --name \<container-name\> --permissions r --start '\<start-date\>' --expiry '\<expiry-date\>'

  3. 在运行上述命令之前,请记住插入以下参数值。

    参数值 说明
    account-name Azure 存储帐户名称。
    account-key Azure 存储帐户密钥。
    container-name 托管 VHD 文件的 Blob 容器。
    start-date 这是 VHD 访问权限的开始日期。 请提供当前日期的前一天的日期。 例如,如果当前日期是 2022 年 7 月 15 日,则将日期设置为 07/14/2022。 以 UTC 日期/时间格式 (YYYY-MM-DDT00:00:00Z) 提供日期,例如 2022-04-01T00:00:00Z。
    expiry-date 这是 VHD 访问的权限到期日期。 请提供自当前日期开始算起的至少三周后的日期。 以 UTC 日期/时间格式 (YYYY-MM-DDT00:00:00Z) 提供日期,例如 2022-04-01T00:00:00Z。
  4. 复制 SAS 连接字符串,并将其保存到位于安全位置的某个文本文件。 编辑此字符串,以添加 VHD 位置信息来创建最终 SAS URI。

  5. 在 Azure 门户中,转到包含与新 URI 关联的 VHD 的 Blob 容器。

  6. 复制 Blob 服务终结点的 URL。

  7. 编辑包含步骤 2 中所述 SAS 连接字符串的文本文件。 使用以下格式创建完整 SAS URI。 请务必在终结点 URL 和连接字符串之间插入“?”。

    <blob-service-endpoint-url>?<sas-connection-string>

虚拟机 SAS 故障消息

此表显示在合作伙伴中心提供共享访问签名 (SAS) URI 时遇到的常见错误以及建议的解决方案。

问题 失败消息 Fix
在 SAS URI 中未找到“?” Must be a valid Azure shared access signature URI. 确保提供的 SAS URI 使用正确的语法并包含“?”字符。
语法: <blob-service-endpoint-url>?<sas-connection-string>
SAS URI 中没有“st”参数 Specified SAS URL cannot be reached. 使用正确的开始日期(“st”)值更新 SAS URI。
SAS URI 中没有“se”参数 The end date parameter (se) is required. 使用正确的“结束日期”(“se”)值更新 SAS URI。
SAS URI 中不存在“sp=r” Missing Permissions (sp) must include 'read' (r). 更新 SAS URI,将权限设置为 Read(“sp=r”)。
SAS URI 授权错误 Failure: Copying Images. Not able to download blob due to authorization error. 检查并更正 SAS URI 格式。 必要时重新生成。
SAS URI 的“st”和“se”参数没有完整的日期时间规范 The start time parameter (st) is not a valid date string.
OR
The end date parameter (se) is not a valid date string.
SAS URI“开始日期”和“结束日期”参数(“st”和“se”子字符串)必须具有完整的日期时间格式 (YYYY-MM-DDT00:00:00Z),例如 11-02-2017T00:00:00Z。 缩短的版本无效(默认情况下,Azure CLI 中的某些命令可能会生成缩短的值)。

有关详细信息,请参阅使用共享访问签名 (SAS) 授予对 Azure 存储资源的有限访问权限

验证 SAS URI

在合作伙伴中心发布 SAS URI 之前请先对其进行检查,以免在提交请求后发生 SAS URI 相关的任何问题。 此过程是可选的,但建议这样做。

  • URI 包含 VHD 映像文件名(包括扩展名 .vhd)。
  • Sp=r 出现在 URI 中间附近。 此字符串显示“已授予读取权限”。
  • sr=c 出现时,这表示指定了容器级访问权限。
  • 将 URI 复制并粘贴到浏览器中以对 blob 进行测试下载(可以在下载完成之前取消该操作)。

后续步骤