-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackend.js
More file actions
37 lines (31 loc) · 803 Bytes
/
backend.js
File metadata and controls
37 lines (31 loc) · 803 Bytes
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
const AWS = require("aws-sdk");
const s3 = new AWS.S3({
signatureVersion: "v4",
});
const checkAccess = (filename) => {
// this check depends on your authorization logic
return true;
};
module.exports.handler = async (event, context) => {
const filename = event.path.replace(/^\//, ""); // remove leading slash: /book.pdf => book.pdf
if (checkAccess(filename)) {
const params = {
Bucket: process.env.BUCKET,
Key: filename
};
const signedUrl = await s3.getSignedUrlPromise("getObject", params);
return {
statusCode: 200,
headers: {
// Because the frontend and the API are on different domains we need to add this CORS header
"Access-Control-Allow-Origin": "*",
},
body: signedUrl,
};
}else {
return {
statusCode: 403,
body: "Forbidden",
};
}
};