-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction.cs
152 lines (135 loc) · 5.07 KB
/
Function.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Util;
using MetadataExtractor;
using Newtonsoft.Json;
using Directory = MetadataExtractor.Directory;
[assembly: LambdaSerializer(typeof(DefaultLambdaJsonSerializer))]
namespace MetadataExtractorFunction
{
public class Function
{
private static async Task Main()
{
Func<S3EventNotification, ILambdaContext, string> func = FunctionHandler;
using var handlerWrapper = HandlerWrapper.GetHandlerWrapper(func, new DefaultLambdaJsonSerializer());
using var bootstrap = new LambdaBootstrap(handlerWrapper);
await bootstrap.RunAsync();
}
public static string FunctionHandler(S3EventNotification input, ILambdaContext context)
{
var logger = context.Logger;
using (var s3Client = new AmazonS3Client())
{
foreach (var record in input.Records)
{
var dic = ExtractMetadata(record, s3Client, logger);
if (dic != null)
{
SaveMetaData(record, dic, s3Client, logger);
}
}
}
return "Ok";
}
private static void SaveMetaData(S3EventNotification.S3EventNotificationRecord record,
Dictionary<string, string> dic,
IAmazonS3 s3Client,
ILambdaLogger logger)
{
try
{
var newKey = $"{record.S3.Object.Key}.metadata.json";
var result = s3Client.PutObjectAsync(new PutObjectRequest
{
BucketName = record.S3.Bucket.Name,
Key = newKey,
ContentType = "application/json",
ContentBody = JsonConvert.SerializeObject(dic)
}, CancellationToken.None).Result;
if (result.HttpStatusCode != HttpStatusCode.OK)
{
throw new InvalidOperationException($"Could not save {newKey}");
}
}
catch (Exception ex)
{
logger.LogLine(ex.Message);
throw;
}
}
private static Dictionary<string, string> ExtractMetadata(S3EventNotification.S3EventNotificationRecord record,
IAmazonS3 s3Client,
ILambdaLogger logger)
{
if (record.S3.Object.Key.EndsWith(".json") ||
record.S3.Object.Key.EndsWith(".txt"))
{
logger.LogLine("Skipping text content");
return null;
}
using (var ms = new MemoryStream())
{
try
{
var result = s3Client.GetObjectAsync(new GetObjectRequest
{
BucketName = record.S3.Bucket.Name,
Key = record.S3.Object.Key
}, CancellationToken.None).Result;
if (result.HttpStatusCode != HttpStatusCode.OK)
{
logger.LogLine($"Could not read {record.S3.Bucket.Name}://{record.S3.Object.Key}");
logger.LogLine($"S3 status code {result.HttpStatusCode}");
return null;
}
result.ResponseStream.CopyTo(ms);
ms.Position = 0;
}
catch (Exception ex)
{
logger.LogLine($"Error reading {record.S3.Bucket.Name}://{record.S3.Object.Key}");
logger.LogLine(ex.ToString());
return null;
}
IReadOnlyList<Directory> directories;
try
{
directories = ImageMetadataReader.ReadMetadata(ms);
}
catch (Exception ex)
{
logger.LogLine($"Could not parse {record.S3.Bucket.Name}://{record.S3.Object.Key}");
logger.LogLine(ex.ToString());
return null;
}
var dic = new Dictionary<string, string>();
foreach (var directory in directories)
{
foreach (var tag in directory.Tags)
{
try
{
dic.Add($"{directory.Name}.{tag.Name}", tag.Description);
}
catch (Exception ex)
{
logger.LogLine($"Error adding tag {tag.Name} - ignoring this error");
logger.LogLine(ex.ToString());
}
}
}
return dic;
}
}
}
}