我已经使用.net中的aws工具包从lambda函数中的aws sqs提取消息,但是消息没有出现。在这些代码中,sqs控制台在cloudwatch上也变为空,同样的receiveMessageResponse也变为空。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon;
using Amazon.Lambda.Core;
using Amazon.SQS;
using Amazon.SQS.Model;
[assembly:
LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace AWSLambda1
{
public class Function
{
/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public string FunctionHandler(string input, ILambdaContext context)
{
var sqs = new AmazonSQSClient();
System.Console.Write("sqs", sqs);
var queueUrl = "myqueueUrl";
var receiveMessageRequest = new ReceiveMessageRequest{ QueueUrl = queueUrl};
var receiveMessageResponse = sqs.ReceiveMessageAsync(receiveMessageRequest);
System.Console.Write("M", receiveMessageResponse);
return "true";
}
}
}
答案 0 :(得分:0)
这是我用来读取队列的代码段,尝试使用它并查看其是否有效
static async Task<string> Start()
{
string AccessKey = "";
string SecretKey = "";
string SQSqueues = "https://sqs.";
RegionEndpoint Region = RegionEndpoint.
bool processedSQS = false;
AWSCredentials credentials = new BasicAWSCredentials(AccessKey, SecretKey);
var AwsRegion = Region;
var SQSClient = new AmazonSQSClient(credentials, Region);
var SQSRequest = new ReceiveMessageRequest
{
MessageAttributeNames = AttributesList,
AttributeNames = new List<string>() { "All" },
MaxNumberOfMessages = 10,
QueueUrl = SQSqueues,
VisibilityTimeout = (int)TimeSpan.FromMinutes(10).TotalSeconds,
WaitTimeSeconds = (int)TimeSpan.FromSeconds(5).TotalSeconds
};
var SQSResponse = SQSClient.ReceiveMessageAsync(SQSRequest);
SQSResponse.Wait();
string JsonPass = null;
if (SQSResponse.Result.Messages.Count > 0)
{
foreach (var Message in SQSResponse.Result.Messages)
{
string SqsMessage = Message.Body;
bool CallUpload = await Insert.GetConfig(SqsMessage, credentials, AwsRegion, tableName);
if (CallUpload == true)
{
var deleteRequest = new DeleteMessageRequest
{
QueueUrl = SQSqueues,
ReceiptHandle = Message.ReceiptHandle
};
var deleteResponse = SQSClient.DeleteMessageAsync(deleteRequest);
deleteResponse.Wait();
}
}
}
return "";
}
答案 1 :(得分:0)
您可以使用这两个代码段从 FIFO SQS 中推送和拉取数据
推送数据 -
IAmazonSQS sqs = new AmazonSQSClient(RegionEndpoint.APSoutheast1);
var sqsRequest = new CreateQueueRequest
{
QueueName = "SQSChatFIFOQueue.fifo"
};
sqsRequest.Attributes.Add("FifoQueue", "true");
var createQueueResponse = sqs.CreateQueueAsync(sqsRequest).Result;
var myQueueUrl = createQueueResponse.QueueUrl;
var listQueueRequest = new ListQueuesRequest();
var listQueueResponse = sqs.ListQueuesAsync(listQueueRequest);
foreach (var url in listQueueResponse.Result.QueueUrls)
{
Console.WriteLine(url);
}
var sqsMessageRequest = new SendMessageRequest
{
QueueUrl = myQueueUrl,
MessageBody = userId,
MessageGroupId = "test",
MessageDeduplicationId = "test"
};
sqs.SendMessageAsync(sqsMessageRequest);
return myQueueUrl;
拉取数据-
var sqs = new AmazonSQSClient(RegionEndpoint.APSoutheast1);
var queueUrl = sqs.GetQueueUrlAsync("SQSChatFIFOQueue.fifo").Result.QueueUrl;
var receiveMessageRequest = new ReceiveMessageRequest
{
QueueUrl = queueUrl
};
var receiveMessageResponce = sqs.ReceiveMessageAsync(receiveMessageRequest).Result;