private async Task <(Guid, string)> ProcessS3File(S3Event evnt, ILambdaLogger logger) { var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return(Guid.Empty, null); } try { if (!s3Event.Object.Key.Contains(Config.GetSection("RiskIdFilePrefix").Value)) { logger.LogLine($"File {s3Event.Object.Key} from bucket {s3Event.Bucket.Name} is not an valid file"); return(Guid.Empty, null); } var response = await this.S3Client.GetObjectMetadataAsync(s3Event.Bucket.Name, s3Event.Object.Key); Guid id = Guid.NewGuid(); if (response != null) { string fileName = $@"PendingProcessing/{Path.GetFileNameWithoutExtension(s3Event.Object.Key)}_{id}" + $"{Path.GetExtension(s3Event.Object.Key)}"; await S3Client.CopyObjectAsync(s3Event.Bucket.Name, s3Event.Object.Key, s3Event.Bucket.Name, fileName) .ConfigureAwait(false); await S3Client.DeleteObjectAsync(s3Event.Bucket.Name, s3Event.Object.Key).ConfigureAwait(false); using (StreamReader reader = new StreamReader( await S3Client.GetObjectStreamAsync(s3Event.Bucket.Name, fileName, null))) { var content = await reader.ReadToEndAsync(); return(id, content); } } } catch (Exception e) { logger.LogLine(e.Message); logger.LogLine(e.StackTrace); throw; } return(Guid.Empty, null); }
/// <summary> /// This method is called for every Lambda invocation. This method takes in an S3 event object and can be used /// to respond to S3 notifications. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <string> ExtractFiles(CodePipelineEvent evnt, ILambdaContext context) { Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(evnt, Newtonsoft.Json.Formatting.Indented)); var inputArtifact = evnt.Job.data.inputArtifacts.First(); var outputArtifact = evnt.Job.data.outputArtifacts.First(); //var credentials = new BasicAWSCredentials(evnt.Job.data.artifactCredentials.accessKeyId, evnt.Job.data.artifactCredentials.secretAccessKey); //IAmazonS3 s3PipelineArtifactAccess = new AmazonS3Client(credentials); using (var objectStream = await S3Client.GetObjectStreamAsync(inputArtifact.location.s3Location.bucketName, inputArtifact.location.s3Location.objectKey, new Dictionary <string, object>())) { var memoryStream = new MemoryStream(); objectStream.CopyTo(memoryStream); context.Logger.LogLine("Fetched object stream async, Bucket: " + inputArtifact.location.s3Location.bucketName); using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Read)) { context.Logger.LogLine("About to read entries"); foreach (var entry in archive.Entries) { context.Logger.LogLine("File: " + entry.FullName); using (var fileStream = entry.Open()) { var fileMemStream = new MemoryStream(); fileStream.CopyTo(fileMemStream); fileMemStream.Position = 0; var fileKey = outputArtifact.location.s3Location.objectKey + "/" + entry.FullName; context.Logger.LogLine(fileKey); await this.S3Client.UploadObjectFromStreamAsync(outputArtifact.location.s3Location.bucketName, fileKey, fileMemStream, additionalProperties : null); } } } } await CodePipelineClient.PutJobSuccessResultAsync(new Amazon.CodePipeline.Model.PutJobSuccessResultRequest { JobId = evnt.Job.id }); return("Success"); }