|
1 | | -var s3 = new (require('aws-sdk')).S3({params:{Bucket: process.env.S3_BUCKET_NAME}}); |
| 1 | +const { S3Client, GetObjectCommand, PutObjectCommand } = require('@aws-sdk/client-s3'); |
| 2 | + |
| 3 | +const s3Client = new S3Client({}); |
| 4 | +const bucketName = process.env.S3_BUCKET_NAME; |
2 | 5 |
|
3 | 6 | module.exports = { |
4 | 7 |
|
5 | | - requestReceived: function(req, res, next) { |
| 8 | + requestReceived: async function(req, res, next) { |
6 | 9 | if(req.method !== 'GET') { |
7 | 10 | return next(); |
8 | 11 | } |
9 | 12 |
|
10 | | - var key = req.prerender.url; |
| 13 | + let key = req.prerender.url; |
11 | 14 |
|
12 | 15 | if (process.env.S3_PREFIX_KEY) { |
13 | 16 | key = process.env.S3_PREFIX_KEY + '/' + key; |
14 | 17 | } |
15 | 18 |
|
16 | | - s3.getObject({ |
| 19 | + try { |
| 20 | + const command = new GetObjectCommand({ |
| 21 | + Bucket: bucketName, |
17 | 22 | Key: key |
18 | | - }, function (err, result) { |
19 | | - |
20 | | - if (!err && result) { |
21 | | - return res.send(200, result.Body); |
22 | | - } |
23 | | - |
| 23 | + }); |
| 24 | + const result = await s3Client.send(command); |
| 25 | + const body = await result.Body.transformToString(); |
| 26 | + return res.send(200, body); |
| 27 | + } catch (err) { |
24 | 28 | next(); |
25 | | - }); |
| 29 | + } |
26 | 30 | }, |
27 | 31 |
|
28 | | - pageLoaded: function(req, res, next) { |
| 32 | + pageLoaded: async function(req, res, next) { |
29 | 33 | if(req.prerender.statusCode !== 200) { |
30 | 34 | return next(); |
31 | 35 | } |
32 | 36 |
|
33 | | - var key = req.prerender.url; |
34 | | - |
| 37 | + let key = req.prerender.url; |
| 38 | + |
35 | 39 | if (process.env.S3_PREFIX_KEY) { |
36 | 40 | key = process.env.S3_PREFIX_KEY + '/' + key; |
37 | 41 | } |
38 | 42 |
|
39 | | - s3.putObject({ |
40 | | - Key: key, |
41 | | - ContentType: 'text/html;charset=UTF-8', |
42 | | - StorageClass: 'REDUCED_REDUNDANCY', |
43 | | - Body: req.prerender.content |
44 | | - }, function(err, result) { |
45 | | - |
46 | | - if (err) console.error(err); |
| 43 | + try { |
| 44 | + const command = new PutObjectCommand({ |
| 45 | + Bucket: bucketName, |
| 46 | + Key: key, |
| 47 | + ContentType: 'text/html;charset=UTF-8', |
| 48 | + StorageClass: 'REDUCED_REDUNDANCY', |
| 49 | + Body: req.prerender.content |
| 50 | + }); |
| 51 | + await s3Client.send(command); |
| 52 | + } catch (err) { |
| 53 | + console.error(err); |
| 54 | + } |
47 | 55 |
|
48 | | - next(); |
49 | | - }); |
| 56 | + next(); |
50 | 57 | } |
51 | 58 | }; |
52 | 59 |
|
0 commit comments