将 GetQueueUrl 与 AWS SDK 或 CLI 配合使用 - AWS SDK 代码示例

AWS 文档 SDK 示例 GitHub 存储库中还有更多 AWS SDK 示例。

GetQueueUrl 与 AWS SDK 或 CLI 配合使用

以下代码示例演示如何使用 GetQueueUrl

.NET
适用于 .NET 的 SDK
注意

查看 GitHub,了解更多信息。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Threading.Tasks; using Amazon.SQS; using Amazon.SQS.Model; public class GetQueueUrl { /// <summary> /// Initializes the Amazon SQS client object and then calls the /// GetQueueUrlAsync method to retrieve the URL of an Amazon SQS /// queue. /// </summary> public static async Task Main() { // If the Amazon SQS message queue is not in the same AWS Region as your // default user, you need to provide the AWS Region as a parameter to the // client constructor. var client = new AmazonSQSClient(); string queueName = "New-Example-Queue"; try { var response = await client.GetQueueUrlAsync(queueName); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"The URL for {queueName} is: {response.QueueUrl}"); } } catch (QueueDoesNotExistException ex) { Console.WriteLine(ex.Message); Console.WriteLine($"The queue {queueName} was not found."); } } }
  • 有关 API 详细信息,请参阅《适用于 .NET 的 AWS SDK API Reference》中的 GetQueueUrl

C++
SDK for C++
注意

查看 GitHub,了解更多信息。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; //! Get the URL for an Amazon Simple Queue Service (Amazon SQS) queue. /*! \param queueName: An Amazon SQS queue name. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SQS::getQueueUrl(const Aws::String &queueName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SQS::SQSClient sqsClient(clientConfiguration); Aws::SQS::Model::GetQueueUrlRequest request; request.SetQueueName(queueName); const Aws::SQS::Model::GetQueueUrlOutcome outcome = sqsClient.GetQueueUrl(request); if (outcome.IsSuccess()) { std::cout << "Queue " << queueName << " has url " << outcome.GetResult().GetQueueUrl() << std::endl; } else { std::cerr << "Error getting url for queue " << queueName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 有关 API 详细信息,请参阅《适用于 C++ 的 AWS SDK API Reference》中的 GetQueueUrl

CLI
AWS CLI

获取队列 URL

此示例将获取指定队列的 URL。

命令:

aws sqs get-queue-url --queue-name MyQueue

输出:

{ "QueueUrl": "https://queue.amazonaws.com/80398EXAMPLE/MyQueue" }
  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 GetQueueUrl

Java
适用于 Java 的 SDK 2.x
注意

查看 GitHub,了解更多信息。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

GetQueueUrlResponse getQueueUrlResponse = sqsClient .getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build()); return getQueueUrlResponse.queueUrl();
  • 有关 API 详细信息,请参阅《AWS SDK for Java 2.x API Reference》中的 GetQueueUrl

JavaScript
SDK for JavaScript (v3)
注意

查看 GitHub,了解更多信息。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

获取 Amazon SQS 队列的 URL。

import { GetQueueUrlCommand, SQSClient } from "@aws-sdk/client-sqs"; const client = new SQSClient({}); const SQS_QUEUE_NAME = "test-queue"; export const main = async (queueName = SQS_QUEUE_NAME) => { const command = new GetQueueUrlCommand({ QueueName: queueName }); const response = await client.send(command); console.log(response); return response; };
SDK for JavaScript (v2)
注意

查看 GitHub,了解更多信息。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

获取 Amazon SQS 队列的 URL。

// 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 = { QueueName: "SQS_QUEUE_NAME", }; sqs.getQueueUrl(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.QueueUrl); } });
PowerShell
Tools for PowerShell V4

示例 1:此示例列出了具有指定名称的队列的 URL。

Get-SQSQueueUrl -QueueName MyQueue

输出

https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue
  • 有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 GetQueueUrl

Tools for PowerShell V5

示例 1:此示例列出了具有指定名称的队列的 URL。

Get-SQSQueueUrl -QueueName MyQueue

输出

https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue
  • 有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V5)》中的 GetQueueUrl

Python
适用于 Python 的 SDK (Boto3)
注意

查看 GitHub,了解更多信息。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

def get_queue(name): """ Gets an SQS queue by name. :param name: The name that was used to create the queue. :return: A Queue object. """ try: queue = sqs.get_queue_by_name(QueueName=name) logger.info("Got queue '%s' with URL=%s", name, queue.url) except ClientError as error: logger.exception("Couldn't get queue named %s.", name) raise error else: return queue
  • 有关 API 详细信息,请参阅《AWS SDK for Python (Boto3) API Reference》中的 GetQueueUrl

SAP ABAP
适用于 SAP ABAP 的 SDK
注意

查看 GitHub,了解更多信息。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

TRY. oo_result = lo_sqs->getqueueurl( iv_queuename = iv_queue_name ). " oo_result is returned for testing purposes. " MESSAGE 'Queue URL retrieved.' TYPE 'I'. CATCH /aws1/cx_sqsqueuedoesnotexist. MESSAGE 'The requested queue does not exist.' TYPE 'E'. ENDTRY.
  • 有关 API 详细信息,请参阅《AWS SDK for SAP ABAP API Reference》中的 GetQueueUrl