public State ReplicateObject(State state, ILambdaContext context) { state.Message += ", Goodbye"; if (!string.IsNullOrEmpty(state.Bucket)) { state.Message += " " + state.Bucket; } try { // Create a CopyObject request CopyObjectRequest request = new CopyObjectRequest { SourceBucket = state.Bucket, SourceKey = state.Key, DestinationBucket = state.TargetBucket, DestinationKey = state.Key }; // Issue request S3Client.CopyObjectAsync(request); } catch (Exception e) { context.Logger.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); state.StateException = true; } return(state); }
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> /// The only artifact should be the one passed to the above function. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <string> CopyArtifactExtractedFiles(CodePipelineEvent evnt, ILambdaContext context) { context.Logger.LogLine(Newtonsoft.Json.JsonConvert.SerializeObject(evnt, Newtonsoft.Json.Formatting.Indented)); var artifact = evnt.Job.data.inputArtifacts.First(); var toCopy = JsonConvert.DeserializeObject <ArtifactsToCopy>(evnt.Job.data.actionConfiguration.configuration.UserParameters); var outputFolderFromPriorStep = artifact.location.s3Location.objectKey + "_output/"; var filePrefix = outputFolderFromPriorStep + toCopy.SourcePrefix; var objects = await S3Client.ListObjectsV2Async(new Amazon.S3.Model.ListObjectsV2Request { BucketName = artifact.location.s3Location.bucketName, Prefix = filePrefix, MaxKeys = 100000 }); context.Logger.LogLine("Objects: Keys=" + objects.KeyCount + " NextConinuationToken: " + objects.NextContinuationToken); foreach (var o in objects.S3Objects) { context.Logger.LogLine("File: " + o.Key); await S3Client.CopyObjectAsync(artifact.location.s3Location.bucketName, o.Key, toCopy.DestBucket, o.Key.Replace(filePrefix, "")); } context.Logger.LogLine("Call PutJobSuccessResultAsync, JobID: " + evnt.Job.id); await CodePipelineClient.PutJobSuccessResultAsync(new Amazon.CodePipeline.Model.PutJobSuccessResultRequest { JobId = evnt.Job.id }); context.Logger.LogLine("Completed"); return("Success"); }