Exemplo n.º 1
0
        public async Task FunctionHandler(S3Event s3Event, ILambdaContext context)
        {
            foreach (var record in s3Event.Records)
            {
                if (!_supportedImageTypes.Contains(Path.GetExtension(record.S3.Object.Key).ToLower()))
                {
                    Console.WriteLine($"Object {record.S3.Bucket.Name}:{record.S3.Object.Key} is not a supported image type");
                    continue;
                }

                Console.WriteLine($"Determining whether image {record.S3.Bucket.Name}:{record.S3.Object.Key} has been compressed");

                // Get the existing tag set
                var taggingResponse = await _s3Client.GetObjectTaggingAsync(new GetObjectTaggingRequest
                {
                    BucketName = record.S3.Bucket.Name,
                    Key        = record.S3.Object.Key
                });

                if (taggingResponse.Tagging.Any(tag => tag.Key == "Compressed" && tag.Value == "true"))
                {
                    Console.WriteLine($"Image {record.S3.Bucket.Name}:{record.S3.Object.Key} has already been compressed");
                    continue;
                }

                // Get the existing image
                using (var objectResponse = await _s3Client.GetObjectAsync(record.S3.Bucket.Name, record.S3.Object.Key))
                    using (Stream responseStream = objectResponse.ResponseStream)
                    {
                        Console.WriteLine($"Compressing image {record.S3.Bucket.Name}:{record.S3.Object.Key}");

                        // Use TinyPNG to compress the image
                        TinyPngClient tinyPngClient    = new TinyPngClient(Environment.GetEnvironmentVariable("TinyPNG_API_Key"));
                        var           compressResponse = await tinyPngClient.Compress(responseStream);

                        var downloadResponse = await tinyPngClient.Download(compressResponse);

                        // Upload the compressed image back to S3
                        using (var compressedStream = await downloadResponse.GetImageStreamData())
                        {
                            Console.WriteLine($"Uploading compressed image {record.S3.Bucket.Name}:{record.S3.Object.Key}");

                            await _s3Client.PutObjectAsync(new PutObjectRequest
                            {
                                BucketName  = record.S3.Bucket.Name,
                                Key         = record.S3.Object.Key,
                                InputStream = compressedStream,
                                TagSet      = new List <Tag>
                                {
                                    new Tag
                                    {
                                        Key   = "Compressed",
                                        Value = "true"
                                    }
                                }
                            });
                        }
                    }
            }
        }
Exemplo n.º 2
0
        public async Task CompressionAndDownload()
        {
            var pngx = new TinyPngClient(apiKey);
            pngx.httpClient = new HttpClient(new FakeResponseHandler()
                .Compress()
                .Download());

            var result = await pngx.Compress(Cat);

            var downloadResult = await pngx.Download(result);

            Assert.Equal(16646, (await downloadResult.GetImageByteData()).Length);
        }