使用 Azure PowerShell 执行 Azure 队列存储操作Perform Azure Queue storage operations with Azure PowerShell

Azure 队列存储是一项可存储大量消息的服务,用户可以通过 HTTP 或 HTTPS 从世界任何地方访问这些消息。Azure Queue storage is a service for storing large numbers of messages that can be accessed from anywhere in the world via HTTP or HTTPS. 有关详细信息,请参阅 Azure 队列简介For detailed information, see Introduction to Azure Queues. 此操作指南文章介绍常见的队列存储操作。This how-to article covers common Queue storage operations. 你将学习如何执行以下操作:You learn how to:

  • 创建队列Create a queue
  • 检索队列Retrieve a queue
  • 添加消息Add a message
  • 读取消息Read a message
  • 删除消息Delete a message
  • 删除队列Delete a queue

本操作指南需要 Azure PowerShell 模块 Az 0.7 或更高版本。This how-to requires the Azure PowerShell module Az version 0.7 or later. 运行 Get-Module -ListAvailable Az 即可查找版本。Run Get-Module -ListAvailable Az to find the version. 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。If you need to upgrade, see Install Azure PowerShell module.

队列的数据平面没有相应的 PowerShell cmdlet。There are no PowerShell cmdlets for the data plane for queues. 若要执行数据平面操作(如添加消息、读取消息和删除消息),必须使用 PowerShell 中公开的 .NET 存储客户端库。To perform data plane operations such as add a message, read a message, and delete a message, you have to use the .NET storage client library as it is exposed in PowerShell. 创建消息对象,然后可以使用命令(例如 AddMessage)对该消息执行操作。You create a message object and then you can use commands such as AddMessage to perform operations on that message. 本文介绍如何执行该操作。This article shows you how to do that.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

登录 AzureSign in to Azure

运行 Connect-AzAccount -EnvironmentName AzureChinaCloud 命令以登录 Azure 订阅,并按照屏幕上的说明操作。Sign in to your Azure subscription with the Connect-AzAccount -EnvironmentName AzureChinaCloud command and follow the on-screen directions.

Connect-AzAccount -EnvironmentName AzureChinaCloud

检索位置列表Retrieve list of locations

如果你不知道要使用哪个位置,可以列出可用的位置。If you don't know which location you want to use, you can list the available locations. 显示列表后,找到要使用的位置。After the list is displayed, find the one you want to use. 本练习使用“中国东部”。This exercise will use China East. 将此内容存储在变量 location 中,以供以后使用。Store this in the variable location for future use.

Get-AzLocation | Select-Object Location
$location = "China East"

创建资源组Create resource group

使用 New-AzResourceGroup 命令创建资源组。Create a resource group with the New-AzResourceGroup command.

Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. 将资源组名称存储在变量中,以供以后使用。Store the resource group name in a variable for future use. 本示例在“中国东部”区域创建名为“howtoqueuesrg”的资源组。In this example, a resource group named howtoqueuesrg is created in the China East region.

$resourceGroup = "howtoqueuesrg"
New-AzResourceGroup -ResourceGroupName $resourceGroup -Location $location

创建存储帐户Create storage account

使用 New-AzStorageAccount 创建具有本地冗余存储 (LRS) 的标准常规用途存储帐户。Create a standard general-purpose storage account with locally redundant storage (LRS) using New-AzStorageAccount. 获取用于定义要使用的存储帐户的存储帐户上下文。Get the storage account context that defines the storage account to be used. 对存储帐户执行操作时,引用上下文而不是重复提供凭据。When acting on a storage account, you reference the context instead of repeatedly providing the credentials.

$storageAccountName = "howtoqueuestorage"
$storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup `
  -Name $storageAccountName `
  -Location $location `
  -SkuName Standard_LRS

$ctx = $storageAccount.Context

创建队列Create a queue

以下示例首先使用存储帐户上下文(包括存储帐户名称及其访问密钥)与 Azure 存储建立连接。The following example first establishes a connection to Azure Storage using the storage account context, which includes the storage account name and its access key. 接下来,它将调用 New-AzStorageQueue cmdlet 以创建名为“howtoqueue”的队列。Next, it calls New-AzStorageQueue cmdlet to create a queue named 'howtoqueue'.

$queueName = "howtoqueue"
$queue = New-AzStorageQueue -Name $queueName -Context $ctx

有关命名 Azure 队列服务命名约定的信息,请参阅 Naming Queues and Metadata(命名队列和元数据)。For information on naming conventions for Azure Queue Service, see Naming Queues and Metadata.

检索队列Retrieve a queue

可以查询和检索存储帐户中的特定队列,或者所有队列的列表。You can query and retrieve a specific queue or a list of all the queues in a Storage account. 以下示例演示如何检索存储帐户中的所有队列以及特定队列;这两个命令都使用 Get-AzStorageQueue cmdlet。The following examples demonstrate how to retrieve all queues in the storage account, and a specific queue; both commands use the Get-AzStorageQueue cmdlet.

# Retrieve a specific queue
$queue = Get-AzStorageQueue -Name $queueName -Context $ctx
# Show the properties of the queue
$queue

# Retrieve all queues and show their names
Get-AzStorageQueue -Context $ctx | Select-Object Name

向队列添加消息Add a message to a queue

影响队列中的实际消息的操作使用 PowerShell 中公开的 .NET 存储客户端库。Operations that impact the actual messages in the queue use the .NET storage client library as exposed in PowerShell. 若要在队列中添加消息,请创建消息对象 Microsoft.Azure.Storage.Queue.CloudQueueMessage 类的新实例。To add a message to a queue, create a new instance of the message object, Microsoft.Azure.Storage.Queue.CloudQueueMessage class. 接下来,调用 AddMessage 方法。Next, call the AddMessage method. 可从字符串(UTF-8 格式)或字节数组创建 CloudQueueMessage。A CloudQueueMessage can be created from either a string (in UTF-8 format) or a byte array.

以下示例演示如何向队列中添加消息。The following example demonstrates how to add a message to your queue.

# Create a new message using a constructor of the CloudQueueMessage class
$queueMessage = [Microsoft.Azure.Storage.Queue.CloudQueueMessage]::new("This is message 1")

# Add a new message to the queue
$queue.CloudQueue.AddMessageAsync($QueueMessage)

# Add two more messages to the queue
$queueMessage = [Microsoft.Azure.Storage.Queue.CloudQueueMessage]::new("This is message 2")
$queue.CloudQueue.AddMessageAsync($QueueMessage)

$queueMessage = [Microsoft.Azure.Storage.Queue.CloudQueueMessage]::new("This is message 3")
$queue.CloudQueue.AddMessageAsync($QueueMessage)

如果使用 Azure 存储资源管理器,可以连接到 Azure 帐户并查看存储帐户中的队列,然后在队列中向下钻取以查看队列中的消息。If you use the Azure Storage Explorer, you can connect to your Azure account and view the queues in the storage account, and drill down into a queue to view the messages on the queue.

从队列中读取一条消息,然后删除该消息Read a message from the queue, then delete it

尽量以先进先出的顺序读取消息。Messages are read in best-try first-in-first-out order. 但不能保证完全这样做。This is not guaranteed. 从队列中读取消息时,消息将对查看此队列的所有其他进程不可见。When you read the message from the queue, it becomes invisible to all other processes looking at the queue. 这可确保如果代码因硬件或软件故障而无法处理消息,则代码的其他实例可以获取相同消息并重试。This ensures that if your code fails to process the message due to hardware or software failure, another instance of your code can get the same message and try again.

不可见超时定义了消息在再次可供处理之前保持不可见的时间。This invisibility timeout defines how long the message remains invisible before it is available again for processing. 默认为 30 秒。The default is 30 seconds.

代码分两步从队列中读取消息。Your code reads a message from the queue in two steps. 调用 Microsoft.Azure.Storage.Queue.CloudQueue.GetMessage 方法时,将获取队列中的下一条消息。When you call the Microsoft.Azure.Storage.Queue.CloudQueue.GetMessage method, you get the next message in the queue. GetMessage 返回的消息变得对从此队列读取消息的任何其他代码不可见。A message returned from GetMessage becomes invisible to any other code reading messages from this queue. 若要完成从队列中删除消息,请调用 Microsoft.Azure.Storage.Queue.CloudQueue.DeleteMessage 方法。To finish removing the message from the queue, you call the Microsoft.Azure.Storage.Queue.CloudQueue.DeleteMessage method.

在以下示例中,你将读完三条队列消息,然后等待 10 秒(不可见超时)。In the following example, you read through the three queue messages, then wait 10 seconds (the invisibility timeout). 然后再次读取这三条消息,读取后,通过调用 DeleteMessage 删除该消息。Then you read the three messages again, deleting the messages after reading them by calling DeleteMessage. 如果在删除消息后尝试读取队列,$queueMessage 将返回为 NULL。If you try to read the queue after the messages are deleted, $queueMessage will be returned as NULL.

# Set the amount of time you want to entry to be invisible after read from the queue
# If it is not deleted by the end of this time, it will show up in the queue again
$invisibleTimeout = [System.TimeSpan]::FromSeconds(10)

# Read the message from the queue, then show the contents of the message. Read the other two messages, too.
$queueMessage = $queue.CloudQueue.GetMessageAsync($invisibleTimeout,$null,$null)
$queueMessage.Result
$queueMessage = $queue.CloudQueue.GetMessageAsync($invisibleTimeout,$null,$null)
$queueMessage.Result
$queueMessage = $queue.CloudQueue.GetMessageAsync($invisibleTimeout,$null,$null)
$queueMessage.Result

# After 10 seconds, these messages reappear on the queue.
# Read them again, but delete each one after reading it.
# Delete the message.
$queueMessage = $queue.CloudQueue.GetMessageAsync($invisibleTimeout,$null,$null)
$queueMessage.Result
$queue.CloudQueue.DeleteMessageAsync($queueMessage.Result.Id,$queueMessage.Result.popReceipt)
$queueMessage = $queue.CloudQueue.GetMessageAsync($invisibleTimeout,$null,$null)
$queueMessage.Result
$queue.CloudQueue.DeleteMessageAsync($queueMessage.Result.Id,$queueMessage.Result.popReceipt)
$queueMessage = $queue.CloudQueue.GetMessageAsync($invisibleTimeout,$null,$null)
$queueMessage.Result
$queue.CloudQueue.DeleteMessageAsync($queueMessage.Result.Id,$queueMessage.Result.popReceipt)

删除队列Delete a queue

若要删除队列及其中包含的所有消息,请调用 Remove-AzStorageQueue cmdlet。To delete a queue and all the messages contained in it, call the Remove-AzStorageQueue cmdlet. 以下示例演示如何使用 Remove-AzStorageQueue cmdlet 删除本练习中使用的特定队列。The following example shows how to delete the specific queue used in this exercise using the Remove-AzStorageQueue cmdlet.

# Delete the queue
Remove-AzStorageQueue -Name $queueName -Context $ctx

清理资源Clean up resources

若要删除已在此练习中创建的所有资产,请删除该资源组。To remove all of the assets you have created in this exercise, remove the resource group. 这会一并删除组中包含的所有资源。This also deletes all resources contained within the group. 在这种情况下,它会删除创建的存储帐户以及资源组本身。In this case, it removes the storage account created and the resource group itself.

Remove-AzResourceGroup -Name $resourceGroup

后续步骤Next steps

本操作指南文章介绍了使用 PowerShell 进行队列存储管理的基本知识,其中包括如何:In this how-to article, you learned about basic Queue storage management with PowerShell, including how to:

  • 创建队列Create a queue
  • 检索队列Retrieve a queue
  • 添加消息Add a message
  • 读取下一条消息Read the next message
  • 删除消息Delete a message
  • 删除队列Delete a queue

Azure PowerShell 存储 cmdletAzure PowerShell Storage cmdlets

Microsoft Azure 存储资源管理器Microsoft Azure Storage Explorer