-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.ts
60 lines (55 loc) · 1.5 KB
/
handler.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
import { APIGatewayProxyHandler } from "aws-lambda";
import { S3 } from "aws-sdk";
import "source-map-support/register";
const s3 = new S3();
const bucketName = process.env.BUCKET_NAME!;
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
}
export const put: APIGatewayProxyHandler = async event => {
const { key } = event.pathParameters;
try {
await s3
.putObject({
Bucket: bucketName,
Key: key,
Body: event.body
})
.promise();
return { statusCode: 200, headers, body: "true" };
} catch (error) {
console.error(`PutError`, key, error);
return { statusCode: 400, headers, body: "false" };
}
};
export const get: APIGatewayProxyHandler = async event => {
const { key } = event.pathParameters;
try {
const obj = await s3
.getObject({
Bucket: bucketName,
Key: key
})
.promise();
return { statusCode: 200, headers, body: obj.Body.toString("utf-8") };
} catch (error) {
console.error(`GetError`, key, error);
return { statusCode: 400, headers, body: "null" };
}
};
export const erase: APIGatewayProxyHandler = async event => {
const { key } = event.pathParameters;
try {
await s3
.deleteObject({
Bucket: bucketName,
Key: key
})
.promise();
return { statusCode: 200, headers, body: "true" };
} catch (error) {
console.error(`DeleteError`, key, error);
return { statusCode: 400, headers, body: "false" };
}
};