Skip to content

Instantly share code, notes, and snippets.

@OksanaH
Created June 14, 2021 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OksanaH/edd735c72ca2027f02f37bcefc2fb3fd to your computer and use it in GitHub Desktop.
Save OksanaH/edd735c72ca2027f02f37bcefc2fb3fd to your computer and use it in GitHub Desktop.
Function.cs
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace TransformXML.Lambda
{ public class Handler
{
protected async Task<HttpResponseMessage> Transform(JObject request, ILambdaContext context)
{
try
{
var s3Client = new AmazonS3Client();
var input3Url = request["getObjectContext"]["inputS3Url"].ToString();
var reqRoute = request["getObjectContext"]["outputRoute"].ToString();
var token = request["getObjectContext"]["outputToken"].ToString();
using var httpClient = new HttpClient();
var original = await httpClient.GetAsync(input3Url);
var content = await original.Content.ReadAsStringAsync();
var receivedXml = XDocument.Parse(content);
var transformedXml = new XElement("article", receivedXml.Root.Element("body").Value);
var toSend = new WriteGetObjectResponseRequest()
{
Body = ToStream(transformedXml),
RequestRoute = reqRoute,
RequestToken = token
};
var response = await s3Client.WriteGetObjectResponseAsync(toSend);
}
catch (Exception ex)
{
context.Logger.Log($"ERROR: {ex.Message}; {ex.StackTrace}");
}
return new HttpResponseMessage() { StatusCode = System.Net.HttpStatusCode.OK };
}
private Stream ToStream(XElement onlyBodyXML)
{
return new MemoryStream(Encoding.UTF8.GetBytes(onlyBodyXML.ToString()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment