如何通过 PowerShell 使用Azure 队列存储

Azure 队列存储是一项服务,用于存储大量可通过 HTTP 或 HTTPS 从世界上任何地方访问的消息。 有关详细信息,请参阅Azure 队列存储简介。 本操作方法文章介绍常见的队列存储操作。 你将学会如何:

  • 创建队列
  • 检索队列
  • 添加消息
  • 检索消息
  • 删除一条消息
  • 删除队列

本操作指南需要 Azure PowerShell (Az) 模块 v12.0.0。 运行 Get-Module -ListAvailable Az 以查找当前安装的版本。 如果需要升级,请参阅安装 Azure PowerShell 模块

没有用于队列数据平面的 PowerShell cmdlet。 若要执行数据平面操作(例如添加消息、读取消息和删除消息),必须使用 .NET 存储客户端库,因为它在 PowerShell 中公开。 创建消息对象,然后可以使用命令,例如 AddMessage 对该消息执行操作。 本文介绍如何执行此操作。

注释

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

登录到 Azure

运行 Connect-AzAccount -EnvironmentName AzureChinaCloud 命令以登录 Azure 订阅,并按照屏幕上的说明操作。 如果需要,可以通过添加 TenantIdSubscription 参数并包括相应的值来指定订阅。

Connect-AzAccount -EnvironmentName AzureChinaCloud

获取位置列表

如果不知道要使用的位置,可以使用 cmdlet 列出可用位置 Get-AzLocation ,如提供的示例所示。 显示列表后,选择一个位置并将其存储在变量中 location 供将来使用。 本练习中的示例使用 chinaeast2 位置。

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

创建资源组

Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。 为资源组选择一个名称,并将其存储在变量中 resourceGroup 以供将来使用。 此示例使用名称 howtoqueuesrg

通过调用 New-AzResourceGroup cmdlet 并向参数提供名称和位置 ResourceGroupName 来创建资源组,如下所示。

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

创建存储帐户

Azure 存储帐户是一个具有唯一名称的资源,其中包含你的所有数据对象,如 Blob、文件、队列和表。

为存储帐户选择一个名称,并将其存储在变量中 storageAccountName 以供将来使用。 此示例使用名称 howtoqueuestorage

接下来,使用 New-AzStorageAccount cmdlet 创建具有本地冗余存储(LRS)的标准常规用途存储帐户。 最后,设置定义存储帐户的存储帐户上下文,将其 ctx 保存到变量。 使用变量引用上下文可以针对存储帐户执行操作,而无需重复提供凭据。

$storageAccountName = "howtoqueuestorage"

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

$ctx = $storageAccount.Context

创建队列

首先,选择存储帐户的名称并将其存储在变量中 queueName 。 此示例使用名称 howtoqueuestorage。 接下来,使用 New-AzStorageQueue cmdlet 创建队列,并将变量queueName传递给ctxNameContext参数,如下所示。

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

有关Azure 队列存储命名约定的信息,请参阅命名队列和元数据

检索队列

可以使用 Get-AzStorageQueue cmdlet 检索特定队列或存储帐户中所有队列的列表。 以下示例演示如何使用 Get-AzStorageQueue cmdlet 检索所有队列,以及如何使用参数指定队列 Name

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

# Retrieve a specific queue
$queue = Get-AzStorageQueue -Name $queueName -Context $ctx

# Show the properties of the queue
$queue

将消息添加到队列

影响队列中消息的操作使用 PowerShell 中公开的.NET存储客户端库。 若要将消息添加到队列,请将消息作为字符串 QueueClient 传递给类 SendMessage 的方法。

消息字符串必须采用 UTF-8 格式。

以下示例演示如何将消息添加到队列。

# Create a new message using a constructor of the CloudQueueMessage class
$queueMessage = "This is message 1"

# Add a new message to the queue
$queue.QueueClient.SendMessageAsync($queueMessage)

# Add two more messages to the queue
$queueMessages = @("This is message 2","This is message 3")
$queueMessages | foreach {$queue.QueueClient.SendMessageAsync($_)}

如果使用Azure 存储资源管理器,可以连接到Azure帐户并查看存储帐户中的队列,然后向下钻取到队列以查看队列中的消息。

从队列中检索消息

虽然这并非总能得到保证,但消息会按 尽力先进先出 的顺序从队列中取出。

根据用例,可以从队列中检索一个或多个消息。 还可以修改消息的可见性,允许或阻止其他进程访问同一消息。

可通过两种方法从队列中检索消息:

  • 接收:使用 Receive 检索消息时,会将该消息出队,并递增其 DequeueCount 属性。 除非删除消息,否则将重新插入队列中以再次进行处理。
  • 查看:使用 Peek 检索消息时,可以“预览”队列中的消息。 Peek 不会将消息出队,也不会增加其 DequeueCount 属性。

接收消息

当你读取队列中的消息时,如果使用诸如 ReceiveMessage 之类的方法,该消息会被暂时出队,并暂时对其他进程不可见。 此 可见性超时 定义消息保持不可见的时间。 默认可见性超时为 30 秒。

如果在可见性超时通过之前未处理消息,则会递增其 DequeueCount 属性,并在队列末尾重新插入该消息。 重新插入相同的消息可确保另一个进程可以检索相同的消息,然后重试。

以下示例将 invisibleTimeout 变量设置为 10 秒,然后从队列中读取两条消息。

# 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
$visibilityTimeout = [System.TimeSpan]::FromSeconds(10)

# Read the message from the queue, then show the contents of the message. 
# Read the next message, too.
$queueMessage = $queue.QueueClient.ReceiveMessage($visibilityTimeout)
$queueMessage.Value
$queueMessage = $queue.QueueClient.ReceiveMessage($visibilityTimeout)
$queueMessage.Value

您可以通过使用 ReceiveMessages 方法并传入一个整数值来指定要返回的最大消息数,从而同时从队列中检索多条消息。

# 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
$visibilityTimeout = [System.TimeSpan]::FromSeconds(10)

# Read the messages from the queue, then show the contents of the messages.
$queueMessage = $queue.QueueClient.ReceiveMessages(5,$visibilityTimeout)
$queueMessage.Value

预览消息

对于可能涉及共享队列或预览消息而不更改其可见性的用例,可以使用 PeekMessagePeekMessages 方法。 与前面的 ReceiveMessages 示例一样,可以通过传递整数值来指定最大消息数,从而同时查看多个消息。

以下示例使用 PeekMessagePeekMessages 方法从队列中检索消息。

# Read the message from the queue, then show the contents of the message. 
$queueMessage = $queue.QueueClient.PeekMessage()
$queueMessage.Value

# Read the next four messages, then show the contents of the messages.
$queueMessage = $queue.QueueClient.PeekMessages(4)
$queueMessage.Value

从队列中删除消息

为防止意外删除,在永久删除消息之前,必须同时提供 MessageIdPopReceipt 这两个属性。 由于此要求,最简单的方法是使用双重过程删除消息。

首先,通过调用 ReceiveMessageReceiveMessages 方法提取队列中的下一条消息。 若要完成从队列中删除消息,请将从消息获取的值传递给 DeleteMessage 该方法。

以下示例演示了此过程。

# 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
$visibilityTimeout = [System.TimeSpan]::FromSeconds(10)

# Receive one message from the queue, then delete the message. 
$queueMessage = $queue.QueueClient.ReceiveMessage($visibilityTimeout)
$queue.QueueClient.DeleteMessage($queueMessage.Value.MessageId, $queueMessage.Value.PopReceipt)

# Receive four message from the queue, then delete the messages. 
$queueMessage = $queue.QueueClient.ReceiveMessages(4,$visibilityTimeout)
$queueMessage.Value | foreach { $queue.QueueClient.DeleteMessage($_.MessageId, $_.PopReceipt)}

删除队列

若要删除队列及其中包含的所有消息,请调用 QueueClientDelete 的方法。 以下示例演示如何删除本练习中使用的特定队列。

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

清理资源

删除资源组以删除在本练习中创建的资产和资源。 在这种情况下,存储帐户和资源组本身也会被删除。

Remove-AzResourceGroup -Name $resourceGroup

后续步骤

本操作说明文章介绍了使用 PowerShell 进行基本的队列存储管理,包括如何:

  • 创建队列
  • 检索队列
  • 添加消息
  • 查看消息
  • 删除邮件
  • 删除队列

Azure PowerShell存储 cmdlet

Microsoft Azure 存储资源管理器