-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (49 loc) · 1.63 KB
/
index.js
File metadata and controls
57 lines (49 loc) · 1.63 KB
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
import express from "express";
import bodyParser from "body-parser";
import Path from "path";
import http from "http";
import { HttpRequest } from "@aws-sdk/protocol-http";
import { S3RequestPresigner } from "@aws-sdk/s3-request-presigner";
import { parseUrl } from "@aws-sdk/url-parser";
import { Hash } from "@aws-sdk/hash-node";
import { formatUrl } from "@aws-sdk/util-format-url";
import dotenv from "dotenv";
import path from "path";
const ROOT_DIRECTORY = Path.resolve(); // to get the root
// add env directory
dotenv.config({ path: path.join(ROOT_DIRECTORY, ".env") });
// express
const app = express();
const PORT = process.env.PORT; // Port where our server will run!
app.use(bodyParser.json()); // parse json
const router = express.Router();
// config
const bucket = process.env.bucket;
const region = process.env.region;
const credentials = {
accessKeyId: process.env.accessKeyId,
secretAccessKey: process.env.secretAccessKey,
};
router.post("/:filename", async (req, res) => {
try {
const fileToGet = req.params.filename;
const s3ObjectUrl = parseUrl(
`https://${bucket}.s3.${region}.amazonaws.com/${fileToGet}`
);
const presigner = new S3RequestPresigner({
credentials,
region,
sha256: Hash.bind(null, "sha256"),
});
// Create a GET request from S3 url.
const url = await presigner.presign(new HttpRequest(s3ObjectUrl));
console.log("PRESIGNED URL: ", formatUrl(url));
res.send({ url: formatUrl(url) });
} catch (e) {
res.status(400).send(e);
}
});
app.use("/api", router);
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}/api`);
});