Amazon SQS 示例使用Amazon SDK for .NET - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Amazon SQS 示例使用Amazon SDK for .NET

以下代码示例显示如何通过通过Amazon SDK for .NET使用 Amazon SQS 进行操作。

操作代码节选,其中包含了有关如何调用具体的 Amazon SQS 函数。

方案展示如何通过调用多个 Amazon SQS 函数来完成特定任务的代码示例。

每个示例都包含一个指向 GitHub其中包含了有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例显示如何授权 Amazon S3 存储桶向 Amazon SQS 队列发送消息。

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

using System; using System.Threading.Tasks; using Amazon.SQS; public class AuthorizeS3ToSendMessage { /// <summary> /// Initializes the Amazon SQS client object and then calls the /// AuthorizeS3ToSendMessageAsync method to authorize the named /// bucket to send messages in response to S3 events. /// </summary> public static async Task Main() { string queueUrl = "https://sqs.us-east-2.amazonaws.com/0123456789ab/Example_Queue"; string bucketName = "doc-example-bucket"; // Create an Amazon SQS client object using the // default user. If the AWS Region you want to use // is different, supply the AWS Region as a parameter. IAmazonSQS client = new AmazonSQSClient(); var queueARN = await client.AuthorizeS3ToSendMessageAsync(queueUrl, bucketName); if (!string.IsNullOrEmpty(queueARN)) { Console.WriteLine($"The Amazon S3 bucket: {bucketName} has been successfully authorized."); Console.WriteLine($"{bucketName} can now send messages to the queue with ARN: {queueARN}."); } } }

以下代码示例显示如何创建 Amazon SQS 队列。

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.SQS; using Amazon.SQS.Model; public class CreateQueue { /// <summary> /// Initializes the Amazon SQS client object and then calls the /// CreateQueueAsync method to create the new queue. If the call is /// successful, it displays the URL of the new queue on the console. /// </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"; int maxMessage = 256 * 1024; var attrs = new Dictionary<string, string> { { QueueAttributeName.DelaySeconds, TimeSpan.FromSeconds(5).TotalSeconds.ToString() }, { QueueAttributeName.MaximumMessageSize, maxMessage.ToString() }, { QueueAttributeName.MessageRetentionPeriod, TimeSpan.FromDays(4).TotalSeconds.ToString() }, { QueueAttributeName.ReceiveMessageWaitTimeSeconds, TimeSpan.FromSeconds(5).TotalSeconds.ToString() }, { QueueAttributeName.VisibilityTimeout, TimeSpan.FromHours(12).TotalSeconds.ToString() }, }; var request = new CreateQueueRequest { Attributes = attrs, QueueName = queueName, }; var response = await client.CreateQueueAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine("Successfully created Amazon SQS queue."); Console.WriteLine($"Queue URL: {response.QueueUrl}"); } } }

创建 Amazon SQS 队列并向其发送消息。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon; using Amazon.SQS; using Amazon.SQS.Model; public class CreateSendExample { // Specify your AWS Region (an example Region is shown). private static readonly string QueueName = "Example_Queue"; private static readonly RegionEndpoint ServiceRegion = RegionEndpoint.USWest2; private static IAmazonSQS client; public static async Task Main() { client = new AmazonSQSClient(ServiceRegion); var createQueueResponse = await CreateQueue(client, QueueName); string queueUrl = createQueueResponse.QueueUrl; Dictionary<string, MessageAttributeValue> messageAttributes = new Dictionary<string, MessageAttributeValue> { { "Title", new MessageAttributeValue { DataType = "String", StringValue = "The Whistler" } }, { "Author", new MessageAttributeValue { DataType = "String", StringValue = "John Grisham" } }, { "WeeksOn", new MessageAttributeValue { DataType = "Number", StringValue = "6" } }, }; string messageBody = "Information about current NY Times fiction bestseller for week of 12/11/2016."; var sendMsgResponse = await SendMessage(client, queueUrl, messageBody, messageAttributes); } /// <summary> /// Creates a new Amazon SQS queue using the queue name passed to it /// in queueName. /// </summary> /// <param name="client">An SQS client object used to send the message.</param> /// <param name="queueName">A string representing the name of the queue /// to create.</param> /// <returns>A CreateQueueResponse that contains information about the /// newly created queue.</returns> public static async Task<CreateQueueResponse> CreateQueue(IAmazonSQS client, string queueName) { var request = new CreateQueueRequest { QueueName = queueName, Attributes = new Dictionary<string, string> { { "DelaySeconds", "60" }, { "MessageRetentionPeriod", "86400" }, }, }; var response = await client.CreateQueueAsync(request); Console.WriteLine($"Created a queue with URL : {response.QueueUrl}"); return response; } /// <summary> /// Sends a message to an SQS queue. /// </summary> /// <param name="client">An SQS client object used to send the message.</param> /// <param name="queueUrl">The URL of the queue to which to send the /// message.</param> /// <param name="messageBody">A string representing the body of the /// message to be sent to the queue.</param> /// <param name="messageAttributes">Attributes for the message to be /// sent to the queue.</param> /// <returns>A SendMessageResponse object that contains information /// about the message that was sent.</returns> public static async Task<SendMessageResponse> SendMessage( IAmazonSQS client, string queueUrl, string messageBody, Dictionary<string, MessageAttributeValue> messageAttributes) { var sendMessageRequest = new SendMessageRequest { DelaySeconds = 10, MessageAttributes = messageAttributes, MessageBody = messageBody, QueueUrl = queueUrl, }; var response = await client.SendMessageAsync(sendMessageRequest); Console.WriteLine($"Sent a message with id : {response.MessageId}"); return response; } }
  • 有关详细信息,请参阅。CreateQueueAmazon SDK for .NETAPI 参考.

以下代码示例显示如何从 Amazon SQS 队列中删除消息。

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.SQS; using Amazon.SQS.Model; public class DeleteMessage { /// <summary> /// Initializes the Amazon SQS client object. It then calls the /// ReceiveMessageAsync method to retrieve information about the /// available methods before deleting them. /// </summary> public static async Task Main() { string queueUrl = "https://sqs.us-east-2.amazonaws.com/0123456789ab/Example_Queue"; var attributeNames = new List<string>() { "All" }; int maxNumberOfMessages = 5; var visibilityTimeout = (int)TimeSpan.FromMinutes(10).TotalSeconds; var waitTimeSeconds = (int)TimeSpan.FromSeconds(5).TotalSeconds; // 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(); var request = new ReceiveMessageRequest { QueueUrl = queueUrl, AttributeNames = attributeNames, MaxNumberOfMessages = maxNumberOfMessages, VisibilityTimeout = visibilityTimeout, WaitTimeSeconds = waitTimeSeconds, }; var response = await client.ReceiveMessageAsync(request); if (response.Messages.Count > 0) { response.Messages.ForEach(async m => { Console.Write($"Message ID: '{m.MessageId}'"); var delRequest = new DeleteMessageRequest { QueueUrl = "https://sqs.us-east-1.amazonaws.com/0123456789ab/MyTestQueue", ReceiptHandle = m.ReceiptHandle, }; var delResponse = await client.DeleteMessageAsync(delRequest); }); } else { Console.WriteLine("No messages to delete."); } } }

接收来自 Amazon SQS 队列的消息,然后删除该消息。

public static async Task Main() { // If the AWS Region you want to use is different from // the AWS Region defined for the default user, supply // the specify your AWS Region to the client constructor. var client = new AmazonSQSClient(); string queueName = "Example_Queue"; var queueUrl = await GetQueueUrl(client, queueName); Console.WriteLine($"The SQS queue's URL is {queueUrl}"); var response = await ReceiveAndDeleteMessage(client, queueUrl); Console.WriteLine($"Message: {response.Messages[0]}"); } /// <summary> /// Retrieve the queue URL for the queue named in the queueName /// property using the client object. /// </summary> /// <param name="client">The Amazon SQS client used to retrieve the /// queue URL.</param> /// <param name="queueName">A string representing name of the queue /// for which to retrieve the URL.</param> /// <returns>The URL of the queue.</returns> public static async Task<string> GetQueueUrl(IAmazonSQS client, string queueName) { var request = new GetQueueUrlRequest { QueueName = queueName, }; GetQueueUrlResponse response = await client.GetQueueUrlAsync(request); return response.QueueUrl; } /// <summary> /// Retrieves the message from the quque at the URL passed in the /// queueURL parameters using the client. /// </summary> /// <param name="client">The SQS client used to retrieve a message.</param> /// <param name="queueUrl">The URL of the queue from which to retrieve /// a message.</param> /// <returns>The response from the call to ReceiveMessageAsync.</returns> public static async Task<ReceiveMessageResponse> ReceiveAndDeleteMessage(IAmazonSQS client, string queueUrl) { // Receive a single message from the queue. var receiveMessageRequest = new ReceiveMessageRequest { AttributeNames = { "SentTimestamp" }, MaxNumberOfMessages = 1, MessageAttributeNames = { "All" }, QueueUrl = queueUrl, VisibilityTimeout = 0, WaitTimeSeconds = 0, }; var receiveMessageResponse = await client.ReceiveMessageAsync(receiveMessageRequest); // Delete the received message from the queue. var deleteMessageRequest = new DeleteMessageRequest { QueueUrl = queueUrl, ReceiptHandle = receiveMessageResponse.Messages[0].ReceiptHandle, }; await client.DeleteMessageAsync(deleteMessageRequest); return receiveMessageResponse; } }
  • 有关详细信息,请参阅。DeleteMessageAmazon SDK for .NETAPI 参考.

以下代码示例显示如何删除 Amazon SQS 队列。

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

using System; using System.Threading.Tasks; using Amazon.SQS; public class DeleteQueue { /// <summary> /// Initializes the Amazon SQS client object and then calls the /// DeleteQueueAsync method to delete the 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 queueUrl = "https://sqs.us-east-2.amazonaws.com/0123456789ab/New-Example-Queue"; var response = await client.DeleteQueueAsync(queueUrl); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine("Successfully deleted the queue."); } else { Console.WriteLine("Could not delete the crew."); } } }
  • 有关详细信息,请参阅。DeleteQueueAmazon SDK for .NETAPI 参考.

以下代码示例显示如何获取 Amazon SQS 队列的属性。

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.SQS; using Amazon.SQS.Model; public class GetQueueAttributes { /// <summary> /// Initializes the Amazon SQS client and then uses it to call the /// GetQueueAttributesAsync method to retrieve the attributes for the /// 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(); var queueUrl = "https://sqs.us-east-2.amazonaws.com/0123456789ab/New-Example-Queue"; var attrs = new List<string>() { "All" }; var request = new GetQueueAttributesRequest { QueueUrl = queueUrl, AttributeNames = attrs, }; var response = await client.GetQueueAttributesAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { DisplayAttributes(response); } } /// <summary> /// Displays the attributes passed to the method on the console. /// </summary> /// <param name="attrs">The attributes for the Amazon SQS queue.</param> public static void DisplayAttributes(GetQueueAttributesResponse attrs) { Console.WriteLine($"Attributes for queue ARN '{attrs.QueueARN}':"); Console.WriteLine($" Approximate number of messages: {attrs.ApproximateNumberOfMessages}"); Console.WriteLine($" Approximate number of messages delayed: {attrs.ApproximateNumberOfMessagesDelayed}"); Console.WriteLine($" Approximate number of messages not visible: {attrs.ApproximateNumberOfMessagesNotVisible}"); Console.WriteLine($" Queue created on: {attrs.CreatedTimestamp}"); Console.WriteLine($" Delay seconds: {attrs.DelaySeconds}"); Console.WriteLine($" Queue last modified on: {attrs.LastModifiedTimestamp}"); Console.WriteLine($" Maximum message size: {attrs.MaximumMessageSize}"); Console.WriteLine($" Message retention period: {attrs.MessageRetentionPeriod}"); Console.WriteLine($" Visibility timeout: {attrs.VisibilityTimeout}"); Console.WriteLine($" Policy: {attrs.Policy}\n"); Console.WriteLine(" Attributes:"); foreach (var attr in attrs.Attributes) { Console.WriteLine($" {attr.Key}: {attr.Value}"); } } }

以下代码示例显示如何获取 Amazon SQS 队列的 URL。

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

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-Exampe-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."); } } }
  • 有关详细信息,请参阅。GetQueueUrlAmazon SDK for .NETAPI 参考.

以下代码示例显示如何从 Amazon SQS 队列接收消息。

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.SQS; using Amazon.SQS.Model; public class ReceiveFromQueue { public static async Task Main(string[] args) { string queueUrl = "https://sqs.us-east-2.amazonaws.com/0123456789ab/Example_Queue"; var attributeNames = new List<string>() { "All" }; int maxNumberOfMessages = 5; var visibilityTimeout = (int)TimeSpan.FromMinutes(10).TotalSeconds; var waitTimeSeconds = (int)TimeSpan.FromSeconds(5).TotalSeconds; // 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(); var request = new ReceiveMessageRequest { QueueUrl = queueUrl, AttributeNames = attributeNames, MaxNumberOfMessages = maxNumberOfMessages, VisibilityTimeout = visibilityTimeout, WaitTimeSeconds = waitTimeSeconds, }; var response = await client.ReceiveMessageAsync(request); if (response.Messages.Count > 0) { DisplayMessages(response.Messages); } } /// <summary> /// Display message information for a list of Amazon SQS messages. /// </summary> /// <param name="messages">The list of Amazon SQS Message objects to display.</param> public static void DisplayMessages(List<Message> messages) { messages.ForEach(m => { Console.WriteLine($"For message ID {m.MessageId}:"); Console.WriteLine($" Body: {m.Body}"); Console.WriteLine($" Receipt handle: {m.ReceiptHandle}"); Console.WriteLine($" MD5 of body: {m.MD5OfBody}"); Console.WriteLine($" MD5 of message attributes: {m.MD5OfMessageAttributes}"); Console.WriteLine(" Attributes:"); foreach (var attr in m.Attributes) { Console.WriteLine($"\t {attr.Key}: {attr.Value}"); } }); } }

接收来自 Amazon SQS 队列的消息,然后删除该消息。

public static async Task Main() { // If the AWS Region you want to use is different from // the AWS Region defined for the default user, supply // the specify your AWS Region to the client constructor. var client = new AmazonSQSClient(); string queueName = "Example_Queue"; var queueUrl = await GetQueueUrl(client, queueName); Console.WriteLine($"The SQS queue's URL is {queueUrl}"); var response = await ReceiveAndDeleteMessage(client, queueUrl); Console.WriteLine($"Message: {response.Messages[0]}"); } /// <summary> /// Retrieve the queue URL for the queue named in the queueName /// property using the client object. /// </summary> /// <param name="client">The Amazon SQS client used to retrieve the /// queue URL.</param> /// <param name="queueName">A string representing name of the queue /// for which to retrieve the URL.</param> /// <returns>The URL of the queue.</returns> public static async Task<string> GetQueueUrl(IAmazonSQS client, string queueName) { var request = new GetQueueUrlRequest { QueueName = queueName, }; GetQueueUrlResponse response = await client.GetQueueUrlAsync(request); return response.QueueUrl; } /// <summary> /// Retrieves the message from the quque at the URL passed in the /// queueURL parameters using the client. /// </summary> /// <param name="client">The SQS client used to retrieve a message.</param> /// <param name="queueUrl">The URL of the queue from which to retrieve /// a message.</param> /// <returns>The response from the call to ReceiveMessageAsync.</returns> public static async Task<ReceiveMessageResponse> ReceiveAndDeleteMessage(IAmazonSQS client, string queueUrl) { // Receive a single message from the queue. var receiveMessageRequest = new ReceiveMessageRequest { AttributeNames = { "SentTimestamp" }, MaxNumberOfMessages = 1, MessageAttributeNames = { "All" }, QueueUrl = queueUrl, VisibilityTimeout = 0, WaitTimeSeconds = 0, }; var receiveMessageResponse = await client.ReceiveMessageAsync(receiveMessageRequest); // Delete the received message from the queue. var deleteMessageRequest = new DeleteMessageRequest { QueueUrl = queueUrl, ReceiptHandle = receiveMessageResponse.Messages[0].ReceiptHandle, }; await client.DeleteMessageAsync(deleteMessageRequest); return receiveMessageResponse; } }
  • 有关详细信息,请参阅。ReceiveMessageAmazon SDK for .NETAPI 参考.

以下代码示例显示如何将消息发送至 Amazon SQS 队列。

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

using System; using System.Threading.Tasks; using Amazon.SQS; using Amazon.SQS.Model; public class SendMessageToQueue { /// <summary> /// Initialize the Amazon SQS client object and use the /// SendMessageAsync method to send a message to an Amazon SQS queue. /// </summary> public static async Task Main() { string messageBody = "This is a sample message to send to the example queue."; string queueUrl = "https://sqs.us-east-2.amazonaws.com/0123456789ab/Example_Queue"; // Create an Amazon SQS client object using the // default user. If the AWS Region you want to use // is different, supply the AWS Region as a parameter. IAmazonSQS client = new AmazonSQSClient(); var request = new SendMessageRequest { MessageBody = messageBody, QueueUrl = queueUrl, }; var response = await client.SendMessageAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Successfully sent message. Message ID: {response.MessageId}"); } else { Console.WriteLine("Could not send message."); } } }

创建 Amazon SQS 队列并向其发送消息。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon; using Amazon.SQS; using Amazon.SQS.Model; public class CreateSendExample { // Specify your AWS Region (an example Region is shown). private static readonly string QueueName = "Example_Queue"; private static readonly RegionEndpoint ServiceRegion = RegionEndpoint.USWest2; private static IAmazonSQS client; public static async Task Main() { client = new AmazonSQSClient(ServiceRegion); var createQueueResponse = await CreateQueue(client, QueueName); string queueUrl = createQueueResponse.QueueUrl; Dictionary<string, MessageAttributeValue> messageAttributes = new Dictionary<string, MessageAttributeValue> { { "Title", new MessageAttributeValue { DataType = "String", StringValue = "The Whistler" } }, { "Author", new MessageAttributeValue { DataType = "String", StringValue = "John Grisham" } }, { "WeeksOn", new MessageAttributeValue { DataType = "Number", StringValue = "6" } }, }; string messageBody = "Information about current NY Times fiction bestseller for week of 12/11/2016."; var sendMsgResponse = await SendMessage(client, queueUrl, messageBody, messageAttributes); } /// <summary> /// Creates a new Amazon SQS queue using the queue name passed to it /// in queueName. /// </summary> /// <param name="client">An SQS client object used to send the message.</param> /// <param name="queueName">A string representing the name of the queue /// to create.</param> /// <returns>A CreateQueueResponse that contains information about the /// newly created queue.</returns> public static async Task<CreateQueueResponse> CreateQueue(IAmazonSQS client, string queueName) { var request = new CreateQueueRequest { QueueName = queueName, Attributes = new Dictionary<string, string> { { "DelaySeconds", "60" }, { "MessageRetentionPeriod", "86400" }, }, }; var response = await client.CreateQueueAsync(request); Console.WriteLine($"Created a queue with URL : {response.QueueUrl}"); return response; } /// <summary> /// Sends a message to an SQS queue. /// </summary> /// <param name="client">An SQS client object used to send the message.</param> /// <param name="queueUrl">The URL of the queue to which to send the /// message.</param> /// <param name="messageBody">A string representing the body of the /// message to be sent to the queue.</param> /// <param name="messageAttributes">Attributes for the message to be /// sent to the queue.</param> /// <returns>A SendMessageResponse object that contains information /// about the message that was sent.</returns> public static async Task<SendMessageResponse> SendMessage( IAmazonSQS client, string queueUrl, string messageBody, Dictionary<string, MessageAttributeValue> messageAttributes) { var sendMessageRequest = new SendMessageRequest { DelaySeconds = 10, MessageAttributes = messageAttributes, MessageBody = messageBody, QueueUrl = queueUrl, }; var response = await client.SendMessageAsync(sendMessageRequest); Console.WriteLine($"Sent a message with id : {response.MessageId}"); return response; } }
  • 有关详细信息,请参阅。SendMessageAmazon SDK for .NETAPI 参考.