示例#1
0
        public void SNSS3EventTest2()
        {
            // ARRANGE
            string Json = @"
{
""Records"":[
{
""eventVersion"":""2.0"",
""eventSource"":""aws:s3"",
""awsRegion"":""us-east-1"",
""eventTime"":""2018-05-02T12:11:00.000Z"",
""eventName"":""ObjectCreated:Put"",
""userIdentity"":{
""principalId"":""AIDAJDPLRKLG7UEXAMPLE""
},
""requestParameters"":{
""sourceIPAddress"":""127.0.0.1""
},
""responseElements"":{
""x-amz-request-id"":""C3D13FE58DE4C810"",
""x-amz-id-2"":""FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD""
},
""s3"":{
""s3SchemaVersion"":""1.0"",
""configurationId"":""testConfigRule"",
""bucket"":{
""name"":""mybucket"",
""ownerIdentity"":{
""principalId"":""A3NL1KOZZKExample""
},
""arn"":""arn:aws:s3:::mybucket""
},
""object"":{
""key"":""HappyFace.jpg"",
""size"":1024,
""sequencer"":""0055AED6DCD90281E5""
}
}
}
]
}";

            Json = Json.Trim().Replace("\r", "").Replace("\n", "").Replace("\t", "");

            // ACT
            SNSS3RecordSet RecordSet = JsonConvert.DeserializeObject <SNSS3RecordSet>(Json);

            JsonSerializerSettings settings      = new JsonSerializerSettings();
            IsoDateTimeConverter   dateConverter = new IsoDateTimeConverter
            {
                DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff'Z'"
            };

            settings.Converters.Add(dateConverter);

            string Content = JsonConvert.SerializeObject(RecordSet, Formatting.None, settings);

            // ASSERT
            Assert.Equal(Json, Content, true, true, true);
        }
示例#2
0
        public async Task PutObject(SNSEvent snsEvent, ILambdaContext context)
        {
            this._Context = context;

            this._Context.LogInfo($"Received SNS event:\n{JsonConvert.SerializeObject(snsEvent)}");

            ReadPresetResponse Preset = await this._ETClient.ReadPresetAsync(new ReadPresetRequest()
            {
                Id = Environment.GetEnvironmentVariable("PresetId")
            });

            if (Preset != null && (int)Preset.HttpStatusCode >= 200 && (int)Preset.HttpStatusCode <= 299)
            {
                foreach (SNSRecord Record in snsEvent.Records)
                {
                    string Message = Record.Sns.Message;
                    this._Context.LogInfo($"Message:\n{Message}");
                    SNSS3RecordSet RecordSet = JsonConvert.DeserializeObject <SNSS3RecordSet>(Message);

                    foreach (SNSS3Record S3Record in RecordSet.Records)
                    {
                        string Key = S3Record.S3.Object.Key;

                        DateTime Now      = DateTime.Now;
                        string   Prefix   = $"{Now.Year.ToString()}/{Now.Month.ToString("00")}";
                        string   FileName = Path.GetFileNameWithoutExtension(Key);

                        this._Context.LogInfo($"Submitting job for {Key}.");

                        CreateJobRequest Request = new CreateJobRequest()
                        {
                            PipelineId = Environment.GetEnvironmentVariable("Pipeline"),
                            Input      = new JobInput()
                            {
                                AspectRatio = "auto",
                                Key         = Key,
                                Container   = "auto",
                                FrameRate   = "auto",
                                Interlaced  = "auto",
                                Resolution  = "auto"
                            },
                            Output = new CreateJobOutput()
                            {
                                PresetId         = Preset.Preset.Id,
                                Rotate           = "0",
                                ThumbnailPattern = $"{Prefix}/{FileName}_{{count}}",
                                Key = $"{Prefix}/{FileName}.{Preset.Preset.Container}"
                            }
                        };

                        try
                        {
                            CreateJobResponse Response = await this._ETClient.CreateJobAsync(Request);

                            if ((int)Response.HttpStatusCode >= 200 && (int)Response.HttpStatusCode <= 299)
                            {
                                this._Context.LogInfo($"Successfully submitted job for {Response.Job.Input.Key} with id {Response.Job.Id} and arn {Response.Job.Arn}.");
                            }
                            else
                            {
                                this._Context.LogError($"Failed to successfully submit job for {Response.Job.Input.Key} with status code: {(int)Response.HttpStatusCode}");
                            }
                        }
                        catch (Exception e)
                        {
                            this._Context.LogError($"Failed to transcode {Key}.", e);
                        }
                    }
                }
            }
            else
            {
                this._Context.LogError($"Failed to retrieve information about preset {Environment.GetEnvironmentVariable("PresetId")}.");
            }
        }
示例#3
0
        /// <summary>
        /// Entrypoint for the Lambda function
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task ExecSNS(SNSEvent request, ILambdaContext context)
        {
            string DestinationBucket;

            if (String.IsNullOrEmpty(DestinationBucket = await GetDestinationBucket(context)))
            {
                return;
            }

            string PrefixPattern;

            if (String.IsNullOrEmpty(PrefixPattern = await GetPrefixPattern(context)))
            {
                return;
            }

            bool DeleteSource = false;

            Boolean.TryParse(Environment.GetEnvironmentVariable("DELETE_SOURCE"), out DeleteSource);

            foreach (SNSRecord Record in request.Records)
            {
                try
                {
                    string Message = Record.Sns.Message;

                    if (S3TestMessage.IsTestMessage(Message))
                    {
                        context.LogInfo($"Processing test event from SNS: {Message}");
                        return;
                    }

                    SNSS3RecordSet RecordSet = JsonConvert.DeserializeObject <SNSS3RecordSet>(Message);

                    foreach (SNSS3Record S3Record in RecordSet.Records)
                    {
                        try
                        {
                            string Key    = S3Record.S3.Object.Key;
                            string Bucket = S3Record.S3.Bucket.Name;

                            CopyObjectResponse Response = await Copy(Bucket, Key, DestinationBucket, PrefixPattern, context);
                        }
                        catch (AggregateException e)
                        {
                            context.LogError(e);

                            await SendFailureSNS(e.InnerException, context);
                        }
                        catch (Exception e)
                        {
                            context.LogError(e);

                            await SendFailureSNS(e, context);
                        }
                    }
                }
                catch (AggregateException e)
                {
                    context.LogError(e);

                    await SendFailureSNS(e.InnerException, context);
                }
                catch (Exception e)
                {
                    context.LogError(e);

                    await SendFailureSNS(e, context);
                }
            }
        }