-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhandler.js
74 lines (62 loc) · 1.59 KB
/
handler.js
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
const Multipart = require("lambda-multipart");
const AWS = require("aws-sdk");
const s3 = new AWS.S3();
const uuidv4 = require("uuid/v4");
module.exports.upload = async (event, context) => {
const { fields, files } = await parseMultipartFormData(event);
if (files == null || files.length == 0) {
// no file found in http request
return {
statusCode: 200
};
}
await Promise.all(
files.map(async file => {
await uploadFileIntoS3(file);
})
);
return {
statusCode: 201
};
};
const parseMultipartFormData = async event => {
return new Promise((resolve, reject) => {
const parser = new Multipart(event);
parser.on("finish", result => {
resolve({ fields: result.fields, files: result.files });
});
parser.on("error", error => {
return reject(error);
});
});
};
const uploadFileIntoS3 = async file => {
const ext = getFileExtension(file);
const options = {
Bucket: process.env.file_s3_bucket_name,
Key: `${uuidv4()}.${ext}`,
Body: file
};
try {
await s3.upload(options).promise();
console.log(
`File uploaded into S3 bucket: "${
process.env.file_s3_bucket_name
}", with key: "${fileName}"`
);
} catch (err) {
console.error(err);
throw err;
}
};
const getFileExtension = file => {
const headers = file["headers"];
if (headers == null) {
throw new Error(`Missing "headers" from request`);
}
const contentType = headers["content-type"];
if (contentType == "image/jpeg") {
return "jpg";
}
throw new Error(`Unsupported content type "${contentType}".`);
};