generated from andybalham/blog-step-functions-aws-toolkit
/
fileHashWriter.ts
79 lines (63 loc) · 2.42 KB
/
fileHashWriter.ts
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
/* eslint-disable no-console */
/* eslint-disable import/prefer-default-export */
import { S3Event } from 'aws-lambda/trigger/s3';
// eslint-disable-next-line import/no-extraneous-dependencies
import S3, { GetObjectRequest } from 'aws-sdk/clients/s3';
import hash from 'object-hash';
// eslint-disable-next-line import/no-extraneous-dependencies
import { DocumentClient } from 'aws-sdk/clients/dynamodb';
import { File } from '../contracts/File';
import { FileHash } from '../contracts/FileHash';
import { FileSectionType } from '../contracts/FileSectionType';
const fileHashesTableName = process.env.FILE_HASHES_TABLE_NAME;
const documentClient = new DocumentClient();
const s3 = new S3();
export const handler = async (event: S3Event): Promise<void> => {
//
console.log(JSON.stringify(event));
if (fileHashesTableName === undefined) throw new Error('fileHashesTableName === undefined');
// eslint-disable-next-line no-restricted-syntax
for (const eventRecord of event.Records) {
//
const eventS3 = eventRecord.s3;
const getObjectRequest: GetObjectRequest = {
Bucket: eventS3.bucket.name,
Key: eventS3.object.key,
};
// eslint-disable-next-line no-await-in-loop
const getObjectOutput = await s3.getObject(getObjectRequest).promise();
if (getObjectOutput.Body === undefined) {
throw new Error(`GetObjectOutput.Body is undefined: ${JSON.stringify(getObjectRequest)}`);
}
const file = JSON.parse(getObjectOutput.Body.toString('utf-8')) as File<any>;
console.log(`file: ${JSON.stringify(file)}`);
const headerHashItem: FileHash = {
s3Key: eventS3.object.key,
fileType: file.header.fileType,
sectionType: FileSectionType.Header,
sectionHash: hash(file.header),
};
// eslint-disable-next-line no-await-in-loop
await documentClient
.put({
TableName: fileHashesTableName,
Item: headerHashItem,
})
.promise();
console.log(`headerHashItem: ${JSON.stringify(headerHashItem)}`);
const bodyHashItem: FileHash = {
s3Key: eventS3.object.key,
fileType: file.header.fileType,
sectionType: FileSectionType.Body,
sectionHash: hash(file.body),
};
// eslint-disable-next-line no-await-in-loop
await documentClient
.put({
TableName: fileHashesTableName,
Item: bodyHashItem,
})
.promise();
console.log(`bodyHashItem: ${JSON.stringify(bodyHashItem)}`);
}
};