public SQSModule() : base("/sqs") { Post("/", async(x, ct) => { using (var sqs = new Amazon.SQS.AmazonSQSClient(new StoredProfileAWSCredentials("GovBrands"), RegionEndpoint.USEast1)) { var resp = await sqs.SendMessageAsync(new SendMessageRequest { QueueUrl = "https://sqs.us-east-1.amazonaws.com/066432702053/demo", MessageAttributes = new Dictionary <string, MessageAttributeValue> { { "Source", new MessageAttributeValue { StringValue = "Nancy", DataType = "String" } } }, MessageBody = "Message from the Web at " + DateTime.UtcNow }); return(resp.MessageId); } }); }
/// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public void FunctionHandler(ILambdaContext context) { string storageConnectionString; if (!ssmParameterManager.TryGetValue(Constants.StorageConnectionStringSSMPath, out storageConnectionString)) { throw new Exception("Storage connection path not defined."); } else { Console.WriteLine($"Storage connection path is set at '{storageConnectionString}'"); } if (!S3Manager.CheckS3Parameters()) { return; } // Check whether the connection string can be parsed. if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount)) { string ContainerNames; if (ssmParameterManager.TryGetValue(Constants.StorageContainerNamesSSMPath, out ContainerNames)) { var arrContainerNames = ContainerNames.Split(',', StringSplitOptions.RemoveEmptyEntries); List <Task> lstTasks = new List <Task>(); foreach (var ContainerName in arrContainerNames) { var t = Task.Factory.StartNew(() => { var lstBlobItems = AzureManager.ListBlobContainer(storageAccount, ContainerName, null); if (lstBlobItems.Count > 0) { Console.WriteLine("Listing S3 items."); var S3Objects = S3Manager.GetS3Items(); Console.WriteLine($"{S3Objects.Count.ToString()} items retrieved from S3."); foreach (var S3Object in S3Objects) { Console.WriteLine($"Item retrieved from S3: {S3Object.BucketName} - {S3Object.Key} ({S3Object.Size.ToString()} bytes) - ETag {S3Object.ETag}"); } StringBuilder sb = new StringBuilder(); Amazon.SQS.AmazonSQSClient sqsClient = new Amazon.SQS.AmazonSQSClient(); lstBlobItems.ForEach((a) => { if (!S3Objects.Any(s => s.Key == a.BlobName && s.Size == a.Size)) { CopyItem copyItem = new CopyItem { BlobItem = a }; // No S3 objects that match the Azure blob, copy it over var strCopyItem = JsonConvert.SerializeObject(copyItem); SendMessageRequest sendRequest = new SendMessageRequest { MessageBody = strCopyItem, QueueUrl = System.Environment.GetEnvironmentVariable("QueueName") }; var sendMessageResult = sqsClient.SendMessageAsync(sendRequest).GetAwaiter().GetResult(); Console.WriteLine($"Item not found in S3, adding to copy queue - {a.BlobName} - send message result: {sendMessageResult.HttpStatusCode.ToString()}"); } }); } }); lstTasks.Add(t); } Task.WaitAll(lstTasks.ToArray()); } } else { // Otherwise, let the user know that they need to define the environment variable. throw new Exception("Storage connection path not defined."); } }