Exemplo n.º 1
0
        /// <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");
        }
Exemplo n.º 2
0
        /// <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");
        }