Skip to content

Commit a99b8bb

Browse files
authored
feat(s3-service)!: move to AWS-SDK v3 (#216)
1 parent e78a26b commit a99b8bb

File tree

7 files changed

+8342
-5809
lines changed

7 files changed

+8342
-5809
lines changed

__tests__/download.unit.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const fs = require('fs') // Require Node.js file system
77
// Require Sinon.js library
88
const sinon = require('sinon')
99

10-
const AWS = require('aws-sdk') // AWS SDK (automatically available in Lambda)
1110
const S3 = require('../lib/s3-service') // Init S3 Service
1211

1312
// Init API instance

__tests__/getLink.unit.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ const delay = ms => new Promise(res => setTimeout(res, ms))
55
// Require Sinon.js library
66
const sinon = require('sinon')
77

8-
const AWS = require('aws-sdk') // AWS SDK (automatically available in Lambda)
9-
// AWS.config.credentials = new AWS.SharedIniFileCredentials({profile: 'madlucas'})
10-
118
const S3 = require('../lib/s3-service') // Init S3 Service
129

1310
// Init API instance

__tests__/responses.unit.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const sinon = require('sinon') // Require Sinon.js library
4-
const AWS = require('aws-sdk') // AWS SDK (automatically available in Lambda)
54
const S3 = require('../lib/s3-service') // Init S3 Service
65
const { gzipSync, brotliCompressSync, deflateSync } = require('zlib')
76

__tests__/sendFile.unit.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ const fs = require('fs') // Require Node.js file system
77
// Require Sinon.js library
88
const sinon = require('sinon')
99

10-
const AWS = require('aws-sdk') // AWS SDK (automatically available in Lambda)
11-
// AWS.config.credentials = new AWS.SharedIniFileCredentials({profile: 'madlucas'})
12-
1310
const S3 = require('../lib/s3-service'); // Init S3 Service
1411

1512
// Init API instance

lib/s3-service.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,39 @@
66
*/
77

88
// Require AWS SDK
9-
const AWS = require('aws-sdk'); // AWS SDK
9+
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3'); // AWS SDK
10+
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner');
1011

1112
// Export
12-
module.exports = new AWS.S3();
13+
exports.client = new S3Client();
14+
15+
exports.getObject = (params) => {
16+
const cmd = new GetObjectCommand(params);
17+
const promise = this.client.send(cmd);
18+
return {
19+
promise: () => promise,
20+
};
21+
};
22+
23+
exports.getSignedUrl = async (
24+
type,
25+
{ Expires, ...params },
26+
callback = () => {}
27+
) => {
28+
let command;
29+
switch (type) {
30+
case 'getObject':
31+
command = new GetObjectCommand(params);
32+
break;
33+
default:
34+
throw new Error('Invalid command type');
35+
}
36+
return getSignedUrl(this.client, command, { expiresIn: Expires })
37+
.then((url) => {
38+
callback(null, url);
39+
return url;
40+
})
41+
.catch((err) => {
42+
callback(err);
43+
});
44+
};

0 commit comments

Comments
 (0)