使用 Tools forSQS 的 Amazon PowerShell 範例 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 Tools forSQS 的 Amazon PowerShell 範例

下列程式碼範例示範如何搭配 Amazon SQS AWS Tools for PowerShell 使用 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 Add-SQSPermission

for PowerShell 工具

範例 1:此範例允許指定的 從指定的佇列 AWS 帳戶 傳送訊息。

Add-SQSPermission -Action SendMessage -AWSAccountId 80398EXAMPLE -Label SendMessagesFromMyQueue -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 AddPermission

下列程式碼範例示範如何使用 Clear-SQSQueue

for PowerShell 工具

範例 1:此範例會從指定的佇列刪除所有訊息。

Clear-SQSQueue -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 PurgeQueue

下列程式碼範例示範如何使用 Edit-SQSMessageVisibility

for PowerShell 工具

範例 1:此範例會將指定佇列中具有指定接收處理之訊息的可見性逾時變更為 10 小時 (10 小時 * 60 分鐘 * 60 秒 = 36000 秒)。

Edit-SQSMessageVisibility -QueueUrl https://sqs.us-east-1.amazonaws.com/8039EXAMPLE/MyQueue -ReceiptHandle AQEBgGDh...J/Iqww== -VisibilityTimeout 36000

下列程式碼範例示範如何使用 Edit-SQSMessageVisibilityBatch

for PowerShell 工具

範例 1:此範例會變更 2 則訊息在指定佇列中具有指定接收控點的可見性逾時。第一個訊息的可見性逾時會變更為 10 小時 (10 小時 * 60 分鐘 * 60 秒 = 36000 秒)。第二個訊息的可見性逾時會變更為 5 小時 (5 小時 * 60 分鐘 * 60 秒 = 18000 秒)。

$changeVisibilityRequest1 = New-Object Amazon.SQS.Model.ChangeMessageVisibilityBatchRequestEntry $changeVisibilityRequest1.Id = "Request1" $changeVisibilityRequest1.ReceiptHandle = "AQEBd329...v6gl8Q==" $changeVisibilityRequest1.VisibilityTimeout = 36000 $changeVisibilityRequest2 = New-Object Amazon.SQS.Model.ChangeMessageVisibilityBatchRequestEntry $changeVisibilityRequest2.Id = "Request2" $changeVisibilityRequest2.ReceiptHandle = "AQEBgGDh...J/Iqww==" $changeVisibilityRequest2.VisibilityTimeout = 18000 Edit-SQSMessageVisibilityBatch -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue -Entry $changeVisibilityRequest1, $changeVisibilityRequest2

輸出:

Failed Successful ------ ---------- {} {Request2, Request1}

下列程式碼範例示範如何使用 Get-SQSDeadLetterSourceQueue

for PowerShell 工具

範例 1:此範例列出依賴指定佇列作為無效字母佇列的任何佇列的 URLs。

Get-SQSDeadLetterSourceQueue -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyDeadLetterQueue

輸出:

https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyOtherQueue

下列程式碼範例示範如何使用 Get-SQSQueue

for PowerShell 工具

範例 1:此範例會列出所有佇列。

Get-SQSQueue

輸出:

https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/AnotherQueue https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/DeadLetterQueue https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyOtherQueue https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyDeadLetterQueue

範例 2:此範例會列出以指定名稱開頭的任何佇列。

Get-SQSQueue -QueueNamePrefix My

輸出:

https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyOtherQueue https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyDeadLetterQueue
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 ListQueues

下列程式碼範例示範如何使用 Get-SQSQueueAttribute

for PowerShell 工具

範例 1:此範例會列出指定佇列的所有屬性。

Get-SQSQueueAttribute -AttributeName All -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue

輸出:

VisibilityTimeout : 30 DelaySeconds : 0 MaximumMessageSize : 262144 MessageRetentionPeriod : 345600 ApproximateNumberOfMessages : 0 ApproximateNumberOfMessagesNotVisible : 0 ApproximateNumberOfMessagesDelayed : 0 CreatedTimestamp : 2/11/2015 5:53:35 PM LastModifiedTimestamp : 12/29/2015 2:23:17 PM QueueARN : arn:aws:sqs:us-east-1:80398EXAMPLE:MyQueue Policy : {"Version":"2008-10-17","Id":"arn:aws:sqs:us-east-1:80398EXAMPLE:MyQueue/SQSDefaultPolicy","Statement":[{"Sid":"Sid14 495134224EX","Effect":"Allow","Principal":{"AWS":"*"},"Action":"SQS:SendMessage","Resource":"arn:aws:sqs:us-east-1:80 398EXAMPLE:MyQueue","Condition":{"ArnEquals":{"aws:SourceArn":"arn:aws:sns:us-east-1:80398EXAMPLE:MyTopic"}}},{"Sid": "SendMessagesFromMyQueue","Effect":"Allow","Principal":{"AWS":"80398EXAMPLE"},"Action":"SQS:SendMessage","Resource":" arn:aws:sqs:us-east-1:80398EXAMPLE:MyQueue"}]} Attributes : {[QueueArn, arn:aws:sqs:us-east-1:80398EXAMPLE:MyQueue], [ApproximateNumberOfMessages, 0], [ApproximateNumberOfMessagesNotVisible, 0], [ApproximateNumberOfMessagesDelayed, 0]...}

範例 2:此範例會分別列出指定佇列的指定屬性。

Get-SQSQueueAttribute -AttributeName MaximumMessageSize, VisibilityTimeout -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue

輸出:

VisibilityTimeout : 30 DelaySeconds : 0 MaximumMessageSize : 262144 MessageRetentionPeriod : 345600 ApproximateNumberOfMessages : 0 ApproximateNumberOfMessagesNotVisible : 0 ApproximateNumberOfMessagesDelayed : 0 CreatedTimestamp : 2/11/2015 5:53:35 PM LastModifiedTimestamp : 12/29/2015 2:23:17 PM QueueARN : arn:aws:sqs:us-east-1:80398EXAMPLE:MyQueue Policy : {"Version":"2008-10-17","Id":"arn:aws:sqs:us-east-1:80398EXAMPLE:MyQueue/SQSDefaultPolicy","Statement":[{"Sid":"Sid14 495134224EX","Effect":"Allow","Principal":{"AWS":"*"},"Action":"SQS:SendMessage","Resource":"arn:aws:sqs:us-east-1:80 398EXAMPLE:MyQueue","Condition":{"ArnEquals":{"aws:SourceArn":"arn:aws:sns:us-east-1:80398EXAMPLE:MyTopic"}}},{"Sid": "SendMessagesFromMyQueue","Effect":"Allow","Principal":{"AWS":"80398EXAMPLE"},"Action":"SQS:SendMessage","Resource":" arn:aws:sqs:us-east-1:80398EXAMPLE:MyQueue"}]} Attributes : {[MaximumMessageSize, 262144], [VisibilityTimeout, 30]}
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 GetQueueAttributes

下列程式碼範例示範如何使用 Get-SQSQueueUrl

for PowerShell 工具

範例 1:此範例會列出具有指定名稱的佇列 URL。

Get-SQSQueueUrl -QueueName MyQueue

輸出:

https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 GetQueueUrl

下列程式碼範例示範如何使用 New-SQSQueue

for PowerShell 工具

範例 1:此範例會建立具有指定名稱的佇列。

New-SQSQueue -QueueName MyQueue

輸出:

https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 CreateQueue

下列程式碼範例示範如何使用 Receive-SQSMessage

for PowerShell 工具

範例 1:此範例會列出指定佇列最多可接收 10 則訊息的資訊。如果指定訊息屬性存在,該資訊將包含這些值。

Receive-SQSMessage -AttributeName SenderId, SentTimestamp -MessageAttributeName StudentName, StudentGrade -MessageCount 10 -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue

輸出:

Attributes : {[SenderId, AIDAIAZKMSNQ7TEXAMPLE], [SentTimestamp, 1451495923744]} Body : Information about John Doe's grade. MD5OfBody : ea572796e3c231f974fe75d89EXAMPLE MD5OfMessageAttributes : 48c1ee811f0fe7c4e88fbe0f5EXAMPLE MessageAttributes : {[StudentGrade, Amazon.SQS.Model.MessageAttributeValue], [StudentName, Amazon.SQS.Model.MessageAttributeValue]} MessageId : 53828c4b-631b-469b-8833-c093cEXAMPLE ReceiptHandle : AQEBpfGp...20Q5cg==
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 ReceiveMessage

下列程式碼範例示範如何使用 Remove-SQSMessage

for PowerShell 工具

範例 1:此範例會從指定的佇列刪除具有指定接收處理常式的訊息。

Remove-SQSMessage -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue -ReceiptHandle AQEBd329...v6gl8Q==
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 DeleteMessage

下列程式碼範例示範如何使用 Remove-SQSMessageBatch

for PowerShell 工具

範例 1:此範例會從指定的佇列刪除具有指定接收控點的 2 則訊息。

$deleteMessageRequest1 = New-Object Amazon.SQS.Model.DeleteMessageBatchRequestEntry $deleteMessageRequest1.Id = "Request1" $deleteMessageRequest1.ReceiptHandle = "AQEBX2g4...wtJSQg==" $deleteMessageRequest2 = New-Object Amazon.SQS.Model.DeleteMessageBatchRequestEntry $deleteMessageRequest2.Id = "Request2" $deleteMessageRequest2.ReceiptHandle = "AQEBqOVY...KTsLYg==" Remove-SQSMessageBatch -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue -Entry $deleteMessageRequest1, $deleteMessageRequest2

輸出:

Failed Successful ------ ---------- {} {Request1, Request2}
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 DeleteMessageBatch

下列程式碼範例示範如何使用 Remove-SQSPermission

for PowerShell 工具

範例 1:此範例會從指定的佇列中移除具有指定標籤的許可設定。

Remove-SQSPermission -Label SendMessagesFromMyQueue -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 RemovePermission

下列程式碼範例示範如何使用 Remove-SQSQueue

for PowerShell 工具

範例 1:此範例會刪除指定的佇列。

Remove-SQSQueue -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 DeleteQueue

下列程式碼範例示範如何使用 Send-SQSMessage

for PowerShell 工具

範例 1:此範例會傳送訊息,其中包含指定的屬性和訊息內文,並延遲訊息傳遞 10 秒。

$cityAttributeValue = New-Object Amazon.SQS.Model.MessageAttributeValue $cityAttributeValue.DataType = "String" $cityAttributeValue.StringValue = "AnyCity" $populationAttributeValue = New-Object Amazon.SQS.Model.MessageAttributeValue $populationAttributeValue.DataType = "Number" $populationAttributeValue.StringValue = "1250800" $messageAttributes = New-Object System.Collections.Hashtable $messageAttributes.Add("City", $cityAttributeValue) $messageAttributes.Add("Population", $populationAttributeValue) Send-SQSMessage -DelayInSeconds 10 -MessageAttributes $messageAttributes -MessageBody "Information about the largest city in Any Region." -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue

輸出:

MD5OfMessageAttributes MD5OfMessageBody MessageId ---------------------- ---------------- --------- 1d3e51347bc042efbdf6dda31EXAMPLE 51b0a3256d59467f973009b73EXAMPLE c35fed8f-c739-4d0c-818b-1820eEXAMPLE
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 SendMessage

下列程式碼範例示範如何使用 Send-SQSMessageBatch

for PowerShell 工具

範例 1:此範例會將 2 則具有指定屬性和訊息內文的訊息傳送至指定的佇列。第一個訊息的傳送會延遲 15 秒,第二個訊息會延遲 10 秒。

$student1NameAttributeValue = New-Object Amazon.SQS.Model.MessageAttributeValue $student1NameAttributeValue.DataType = "String" $student1NameAttributeValue.StringValue = "John Doe" $student1GradeAttributeValue = New-Object Amazon.SQS.Model.MessageAttributeValue $student1GradeAttributeValue.DataType = "Number" $student1GradeAttributeValue.StringValue = "89" $student2NameAttributeValue = New-Object Amazon.SQS.Model.MessageAttributeValue $student2NameAttributeValue.DataType = "String" $student2NameAttributeValue.StringValue = "Jane Doe" $student2GradeAttributeValue = New-Object Amazon.SQS.Model.MessageAttributeValue $student2GradeAttributeValue.DataType = "Number" $student2GradeAttributeValue.StringValue = "93" $message1 = New-Object Amazon.SQS.Model.SendMessageBatchRequestEntry $message1.DelaySeconds = 15 $message1.Id = "FirstMessage" $message1.MessageAttributes.Add("StudentName", $student1NameAttributeValue) $message1.MessageAttributes.Add("StudentGrade", $student1GradeAttributeValue) $message1.MessageBody = "Information about John Doe's grade." $message2 = New-Object Amazon.SQS.Model.SendMessageBatchRequestEntry $message2.DelaySeconds = 10 $message2.Id = "SecondMessage" $message2.MessageAttributes.Add("StudentName", $student2NameAttributeValue) $message2.MessageAttributes.Add("StudentGrade", $student2GradeAttributeValue) $message2.MessageBody = "Information about Jane Doe's grade." Send-SQSMessageBatch -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue -Entry $message1, $message2

輸出:

Failed Successful ------ ---------- {} {FirstMessage, SecondMessage}
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 SendMessageBatch

下列程式碼範例示範如何使用 Set-SQSQueueAttribute

for PowerShell 工具

範例 1:此範例示範如何設定將佇列訂閱至 SNS 主題的政策。當訊息發佈至主題時,訊息會傳送至訂閱的佇列。

# create the queue and topic to be associated $qurl = New-SQSQueue -QueueName "myQueue" $topicarn = New-SNSTopic -Name "myTopic" # get the queue ARN to inject into the policy; it will be returned # in the output's QueueARN member but we need to put it into a variable # so text expansion in the policy string takes effect $qarn = (Get-SQSQueueAttribute -QueueUrl $qurl -AttributeName "QueueArn").QueueARN # construct the policy and inject arns $policy = @" { "Version": "2008-10-17", "Id": "$qarn/SQSPOLICY", "Statement": [ { "Sid": "1", "Effect": "Allow", "Principal": "*", "Action": "SQS:SendMessage", "Resource": "$qarn", "Condition": { "ArnEquals": { "aws:SourceArn": "$topicarn" } } } ] } "@ # set the policy Set-SQSQueueAttribute -QueueUrl $qurl -Attribute @{ Policy=$policy }

範例 2:此範例會設定指定佇列的指定屬性。

Set-SQSQueueAttribute -Attribute @{"DelaySeconds" = "10"; "MaximumMessageSize" = "131072"} -QueueUrl https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 SetQueueAttributes