AWS 文檔 AWS SDK示例 GitHub 回購中有更多SDK示例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
搭GetQueueAttributes
配 AWS SDK或使用 CLI
下列程式碼範例會示範如何使用GetQueueAttributes
。
動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:
- .NET
-
- AWS SDK for .NET
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 /// <summary> /// Get the ARN for a queue from its URL. /// </summary> /// <param name="queueUrl">The URL of the queue.</param> /// <returns>The ARN of the queue.</returns> public async Task<string> GetQueueArnByUrl(string queueUrl) { var getAttributesRequest = new GetQueueAttributesRequest() { QueueUrl = queueUrl, AttributeNames = new List<string>() { QueueAttributeName.QueueArn } }; var getAttributesResponse = await _amazonSQSClient.GetQueueAttributesAsync( getAttributesRequest); return getAttributesResponse.QueueARN; }
-
如需詳API細資訊,請參閱AWS SDK for .NET API參考GetQueueAttributes中的。
-
- C++
-
- SDK對於 C ++
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::SQS::SQSClient sqsClient(clientConfiguration); Aws::SQS::Model::GetQueueAttributesRequest request; request.SetQueueUrl(queueURL); request.AddAttributeNames(Aws::SQS::Model::QueueAttributeName::QueueArn); Aws::SQS::Model::GetQueueAttributesOutcome outcome = sqsClient.GetQueueAttributes(request); if (outcome.IsSuccess()) { const Aws::Map<Aws::SQS::Model::QueueAttributeName, Aws::String> &attributes = outcome.GetResult().GetAttributes(); const auto &iter = attributes.find( Aws::SQS::Model::QueueAttributeName::QueueArn); if (iter != attributes.end()) { queueARN = iter->second; std::cout << "The queue ARN '" << queueARN << "' has been retrieved." << std::endl; } } else { std::cerr << "Error with SQS::GetQueueAttributes. " << outcome.GetError().GetMessage() << std::endl; }
-
如需詳API細資訊,請參閱AWS SDK for C++ API參考GetQueueAttributes中的。
-
- CLI
-
- AWS CLI
-
若要取得佇列的屬性
此範例會取得所有指定佇列的屬性。
命令:
aws sqs get-queue-attributes --queue-url
https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue
--attribute-namesAll
輸出:
{ "Attributes": { "ApproximateNumberOfMessagesNotVisible": "0", "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:us-east-1:80398EXAMPLE:MyDeadLetterQueue\",\"maxReceiveCount\":1000}", "MessageRetentionPeriod": "345600", "ApproximateNumberOfMessagesDelayed": "0", "MaximumMessageSize": "262144", "CreatedTimestamp": "1442426968", "ApproximateNumberOfMessages": "0", "ReceiveMessageWaitTimeSeconds": "0", "DelaySeconds": "0", "VisibilityTimeout": "30", "LastModifiedTimestamp": "1442426968", "QueueArn": "arn:aws:sqs:us-east-1:80398EXAMPLE:MyNewQueue" } }
此範例只會取得指定佇列的最大訊息大小和可見性逾時屬性。
命令:
aws sqs get-queue-attributes --queue-url
https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyNewQueue
--attribute-namesMaximumMessageSize
VisibilityTimeout
輸出:
{ "Attributes": { "VisibilityTimeout": "30", "MaximumMessageSize": "262144" } }
-
如需詳API細資訊,請參閱AWS CLI 指令參考GetQueueAttributes
中的。
-
- Go
-
- SDK對於轉到 V2
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 // SqsActions encapsulates the Amazon Simple Queue Service (Amazon SQS) actions // used in the examples. type SqsActions struct { SqsClient *sqs.Client } // GetQueueArn uses the GetQueueAttributes action to get the Amazon Resource Name (ARN) // of an Amazon SQS queue. func (actor SqsActions) GetQueueArn(queueUrl string) (string, error) { var queueArn string arnAttributeName := types.QueueAttributeNameQueueArn attribute, err := actor.SqsClient.GetQueueAttributes(context.TODO(), &sqs.GetQueueAttributesInput{ QueueUrl: aws.String(queueUrl), AttributeNames: []types.QueueAttributeName{arnAttributeName}, }) if err != nil { log.Printf("Couldn't get ARN for queue %v. Here's why: %v\n", queueUrl, err) } else { queueArn = attribute.Attributes[string(arnAttributeName)] } return queueArn, err }
-
如需詳API細資訊,請參閱AWS SDK for Go API參考GetQueueAttributes
中的。
-
- JavaScript
-
- SDK對於 JavaScript (3)
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 import { GetQueueAttributesCommand, SQSClient } from "@aws-sdk/client-sqs"; const client = new SQSClient({}); const SQS_QUEUE_URL = "queue-url"; export const getQueueAttributes = async (queueUrl = SQS_QUEUE_URL) => { const command = new GetQueueAttributesCommand({ QueueUrl: queueUrl, AttributeNames: ["DelaySeconds"], }); const response = await client.send(command); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: '747a1192-c334-5682-a508-4cd5e8dc4e79', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // Attributes: { DelaySeconds: '1' } // } return response; };
-
如需詳API細資訊,請參閱AWS SDK for JavaScript API參考GetQueueAttributes中的。
-
- PowerShell
-
- 適用的工具 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 指令程GetQueueAttributes式參考中的。
-