AWS 文檔 AWS SDK示例 GitHub 回購中有更多SDK示例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
搭ListQueues
配 AWS SDK或使用 CLI
下列程式碼範例會示範如何使用ListQueues
。
- C++
-
- SDK對於 C ++
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; //! List the Amazon Simple Queue Service (Amazon SQS) queues within an AWS account. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SQS::listQueues(const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SQS::SQSClient sqsClient(clientConfiguration); Aws::SQS::Model::ListQueuesRequest listQueuesRequest; Aws::String nextToken; // Used for pagination. Aws::Vector<Aws::String> allQueueUrls; do { if (!nextToken.empty()) { listQueuesRequest.SetNextToken(nextToken); } const Aws::SQS::Model::ListQueuesOutcome outcome = sqsClient.ListQueues( listQueuesRequest); if (outcome.IsSuccess()) { const Aws::Vector<Aws::String> &queueUrls = outcome.GetResult().GetQueueUrls(); allQueueUrls.insert(allQueueUrls.end(), queueUrls.begin(), queueUrls.end()); nextToken = outcome.GetResult().GetNextToken(); } else { std::cerr << "Error listing queues: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!nextToken.empty()); std::cout << allQueueUrls.size() << " Amazon SQS queue(s) found." << std::endl; for (const auto &iter: allQueueUrls) { std::cout << " " << iter << std::endl; } return true; }
-
如需詳API細資訊,請參閱AWS SDK for C++ API參考ListQueues中的。
-
- CLI
-
- AWS CLI
-
列出佇列
此範例會列出所有佇列。
命令:
aws sqs list-queues
輸出:
{ "QueueUrls": [ "https://queue.amazonaws.com/80398EXAMPLE/MyDeadLetterQueue", "https://queue.amazonaws.com/80398EXAMPLE/MyQueue", "https://queue.amazonaws.com/80398EXAMPLE/MyOtherQueue", "https://queue.amazonaws.com/80398EXAMPLE/TestQueue1", "https://queue.amazonaws.com/80398EXAMPLE/TestQueue2" ] }
此範例只會列出以「My」開頭的佇列。
命令:
aws sqs list-queues --queue-name-prefix
My
輸出:
{ "QueueUrls": [ "https://queue.amazonaws.com/80398EXAMPLE/MyDeadLetterQueue", "https://queue.amazonaws.com/80398EXAMPLE/MyQueue", "https://queue.amazonaws.com/80398EXAMPLE/MyOtherQueue" ] }
-
如需詳API細資訊,請參閱AWS CLI 指令參考ListQueues
中的。
-
- Go
-
- SDK對於轉到 V2
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 package main import ( "context" "fmt" "log" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/sqs" ) // main uses the AWS SDK for Go V2 to create an Amazon Simple Queue Service // (Amazon SQS) client and list the queues in your account. // This example uses the default settings specified in your shared credentials // and config files. func main() { sdkConfig, err := config.LoadDefaultConfig(context.TODO()) if err != nil { fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") fmt.Println(err) return } sqsClient := sqs.NewFromConfig(sdkConfig) fmt.Println("Let's list the queues for your account.") var queueUrls []string paginator := sqs.NewListQueuesPaginator(sqsClient, &sqs.ListQueuesInput{}) for paginator.HasMorePages() { output, err := paginator.NextPage(context.TODO()) if err != nil { log.Printf("Couldn't get queues. Here's why: %v\n", err) break } else { queueUrls = append(queueUrls, output.QueueUrls...) } } if len(queueUrls) == 0 { fmt.Println("You don't have any queues!") } else { for _, queueUrl := range queueUrls { fmt.Printf("\t%v\n", queueUrl) } } }
-
如需詳API細資訊,請參閱AWS SDK for Go API參考ListQueues
中的。
-
- Java
-
- SDK對於爪哇 2.x
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 String prefix = "que"; try { ListQueuesRequest listQueuesRequest = ListQueuesRequest.builder().queueNamePrefix(prefix).build(); ListQueuesResponse listQueuesResponse = sqsClient.listQueues(listQueuesRequest); for (String url : listQueuesResponse.queueUrls()) { System.out.println(url); } } catch (SqsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); }
-
如需詳API細資訊,請參閱AWS SDK for Java 2.x API參考ListQueues中的。
-
- JavaScript
-
- SDK對於 JavaScript (3)
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 列出您的 Amazon SQS 隊列。
import { paginateListQueues, SQSClient } from "@aws-sdk/client-sqs"; const client = new SQSClient({}); export const main = async () => { const paginatedListQueues = paginateListQueues({ client }, {}); /** @type {string[]} */ const urls = []; for await (const page of paginatedListQueues) { const nextUrls = page.QueueUrls?.filter((qurl) => !!qurl) || []; urls.push(...nextUrls); urls.forEach((url) => console.log(url)); } return urls; };
-
如需詳細資訊,請參閱《AWS SDK for JavaScript 開發人員指南》。
-
如需詳API細資訊,請參閱AWS SDK for JavaScript API參考ListQueues中的。
-
- SDK對於 JavaScript (第 2 個)
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 列出您的 Amazon SQS 隊列。
// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create an SQS service object var sqs = new AWS.SQS({ apiVersion: "2012-11-05" }); var params = {}; sqs.listQueues(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.QueueUrls); } });
-
如需詳細資訊,請參閱《AWS SDK for JavaScript 開發人員指南》。
-
如需詳API細資訊,請參閱AWS SDK for JavaScript API參考ListQueues中的。
-
- Kotlin
-
- SDK對於科特林
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 suspend fun listQueues() { println("\nList Queues") val prefix = "que" val listQueuesRequest = ListQueuesRequest { queueNamePrefix = prefix } SqsClient { region = "us-east-1" }.use { sqsClient -> val response = sqsClient.listQueues(listQueuesRequest) response.queueUrls?.forEach { url -> println(url) } } }
-
有API關詳細資訊,請參閱ListQueues
中AWS SDK的以取得 Kotlin API 的參考資料
-
- PowerShell
-
- 適用的工具 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 指令程ListQueues式參考中的。
-
- Python
-
- SDK對於 Python(肉毒桿菌 3)
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 def get_queues(prefix=None): """ Gets a list of SQS queues. When a prefix is specified, only queues with names that start with the prefix are returned. :param prefix: The prefix used to restrict the list of returned queues. :return: A list of Queue objects. """ if prefix: queue_iter = sqs.queues.filter(QueueNamePrefix=prefix) else: queue_iter = sqs.queues.all() queues = list(queue_iter) if queues: logger.info("Got queues: %s", ", ".join([q.url for q in queues])) else: logger.warning("No queues found.") return queues
-
如需詳API細資訊,請參閱ListQueues中AWS SDK的〈〉以取得 Python (Boto3) API 參考資料。
-
- Ruby
-
- SDK對於紅寶石
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 require "aws-sdk-sqs" require "aws-sdk-sts" # @param sqs_client [Aws::SQS::Client] An initialized Amazon SQS client. # @example # list_queue_urls(Aws::SQS::Client.new(region: 'us-west-2')) def list_queue_urls(sqs_client) queues = sqs_client.list_queues queues.queue_urls.each do |url| puts url end rescue StandardError => e puts "Error listing queue URLs: #{e.message}" end # Lists the attributes of a queue in Amazon Simple Queue Service (Amazon SQS). # # @param sqs_client [Aws::SQS::Client] An initialized Amazon SQS client. # @param queue_url [String] The URL of the queue. # @example # list_queue_attributes( # Aws::SQS::Client.new(region: 'us-west-2'), # 'https://sqs.us-west-2.amazonaws.com/111111111111/my-queue' # ) def list_queue_attributes(sqs_client, queue_url) attributes = sqs_client.get_queue_attributes( queue_url: queue_url, attribute_names: ["All"] ) attributes.attributes.each do |key, value| puts "#{key}: #{value}" end rescue StandardError => e puts "Error getting queue attributes: #{e.message}" end # Full example call: # Replace us-west-2 with the AWS Region you're using for Amazon SQS. def run_me region = "us-west-2" queue_name = "my-queue" sqs_client = Aws::SQS::Client.new(region: region) puts "Listing available queue URLs..." list_queue_urls(sqs_client) sts_client = Aws::STS::Client.new(region: region) # For example: # 'https://sqs.us-west-2.amazonaws.com/111111111111/my-queue' queue_url = "https://sqs." + region + ".amazonaws.com/" + sts_client.get_caller_identity.account + "/" + queue_name puts "\nGetting information about queue '#{queue_name}'..." list_queue_attributes(sqs_client, queue_url) end
-
如需詳API細資訊,請參閱AWS SDK for Ruby API參考ListQueues中的。
-
- Rust
-
- SDK對於銹
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 擷取區域中列出的第一個 Amazon SQS 佇列。
async fn find_first_queue(client: &Client) -> Result<String, Error> { let queues = client.list_queues().send().await?; let queue_urls = queues.queue_urls(); Ok(queue_urls .first() .expect("No queues in this account and Region. Create a queue to proceed.") .to_string()) }
-
如需詳API細資訊,請參閱ListQueues
中AWS SDK的以取得 Rust API 參考
-
- SAP ABAP
-
- SDK對於 SAP ABAP
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 TRY. oo_result = lo_sqs->listqueues( ). " oo_result is returned for testing purposes. " MESSAGE 'Retrieved list of queues.' TYPE 'I'. ENDTRY.
-
如需詳API細資訊,請參閱ListQueues中AWS SDK的以供SAPABAPAPI參考。
-