vls-s3 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,16 @@
1
- import { PutObjectCommandInput, GetObjectCommandInput } from '@aws-sdk/client-s3';
1
+ import { PutObjectCommandInput, GetObjectCommandInput, PutObjectCommandOutput } from '@aws-sdk/client-s3';
2
2
  export declare class LambdaS3Client {
3
- private readonly bucketName;
4
- constructor(bucketName: string);
5
- uploadFile(payload: Omit<PutObjectCommandInput, 'Bucket'>): Promise<void>;
3
+ private client;
4
+ private readonly config;
5
+ constructor(config: Config);
6
+ private getClient;
7
+ uploadFile(payload: Omit<PutObjectCommandInput, 'Bucket'>): Promise<PutObjectCommandOutput>;
6
8
  getSignedURL(payload: Omit<GetObjectCommandInput, 'Bucket'>, expiresIn?: number): Promise<string>;
7
9
  }
10
+ type Config = {
11
+ bucketName: string;
12
+ region?: string;
13
+ accessKey?: string;
14
+ accessKeyID?: string;
15
+ };
16
+ export {};
@@ -3,20 +3,38 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LambdaS3Client = void 0;
4
4
  const client_s3_1 = require("@aws-sdk/client-s3");
5
5
  const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
6
- const s3Client = new client_s3_1.S3Client({ region: process.env.REGION });
7
6
  class LambdaS3Client {
8
- constructor(bucketName) {
9
- this.bucketName = bucketName;
7
+ constructor(config) {
8
+ this.client = null;
9
+ this.config = config;
10
+ }
11
+ getClient() {
12
+ if (!this.client) {
13
+ this.client = new client_s3_1.S3Client({
14
+ region: this.config.region ?? process.env.REGION,
15
+ ...(this.config.accessKeyID && this.config.accessKey
16
+ ? {
17
+ credentials: {
18
+ accessKeyId: this.config.accessKeyID,
19
+ secretAccessKey: this.config.accessKey
20
+ }
21
+ }
22
+ : {})
23
+ });
24
+ }
25
+ return this.client;
10
26
  }
11
27
  async uploadFile(payload) {
12
- await s3Client.send(new client_s3_1.PutObjectCommand({
13
- Bucket: this.bucketName,
28
+ const client = this.getClient();
29
+ return await client.send(new client_s3_1.PutObjectCommand({
30
+ Bucket: this.config.bucketName,
14
31
  ...payload
15
32
  }));
16
33
  }
17
34
  async getSignedURL(payload, expiresIn = 3 * 24 * 60 * 60) {
18
- return await (0, s3_request_presigner_1.getSignedUrl)(s3Client, new client_s3_1.GetObjectCommand({
19
- Bucket: this.bucketName,
35
+ const client = this.getClient();
36
+ return await (0, s3_request_presigner_1.getSignedUrl)(client, new client_s3_1.GetObjectCommand({
37
+ Bucket: this.config.bucketName,
20
38
  ...payload
21
39
  }), {
22
40
  expiresIn
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vls-s3",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "main": "dist/lambda-s3-client.js",
5
5
  "types": "dist/lambda-s3-client.d.ts",
6
6
  "scripts": {
@@ -3,23 +3,43 @@ import {
3
3
  PutObjectCommand,
4
4
  PutObjectCommandInput,
5
5
  S3Client,
6
- GetObjectCommandInput
6
+ GetObjectCommandInput,
7
+ PutObjectCommandOutput
7
8
  } from '@aws-sdk/client-s3';
8
9
  import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
9
10
 
10
- const s3Client = new S3Client({ region: process.env.REGION });
11
-
12
11
  export class LambdaS3Client {
13
- private readonly bucketName: string;
12
+ private client: S3Client | null = null;
13
+ private readonly config: Config;
14
+
15
+ constructor(config: Config) {
16
+ this.config = config;
17
+ }
18
+
19
+ private getClient(): S3Client {
20
+ if (!this.client) {
21
+ this.client = new S3Client({
22
+ region: this.config.region ?? process.env.REGION,
23
+ ...(this.config.accessKeyID && this.config.accessKey
24
+ ? {
25
+ credentials: {
26
+ accessKeyId: this.config.accessKeyID,
27
+ secretAccessKey: this.config.accessKey
28
+ }
29
+ }
30
+ : {})
31
+ });
32
+ }
14
33
 
15
- constructor(bucketName: string) {
16
- this.bucketName = bucketName;
34
+ return this.client;
17
35
  }
18
36
 
19
- async uploadFile(payload: Omit<PutObjectCommandInput, 'Bucket'>): Promise<void> {
20
- await s3Client.send(
37
+ async uploadFile(payload: Omit<PutObjectCommandInput, 'Bucket'>): Promise<PutObjectCommandOutput> {
38
+ const client = this.getClient();
39
+
40
+ return await client.send(
21
41
  new PutObjectCommand({
22
- Bucket: this.bucketName,
42
+ Bucket: this.config.bucketName,
23
43
  ...payload
24
44
  })
25
45
  );
@@ -29,10 +49,12 @@ export class LambdaS3Client {
29
49
  payload: Omit<GetObjectCommandInput, 'Bucket'>,
30
50
  expiresIn: number = 3 * 24 * 60 * 60
31
51
  ): Promise<string> {
52
+ const client = this.getClient();
53
+
32
54
  return await getSignedUrl(
33
- s3Client,
55
+ client,
34
56
  new GetObjectCommand({
35
- Bucket: this.bucketName,
57
+ Bucket: this.config.bucketName,
36
58
  ...payload
37
59
  }),
38
60
  {
@@ -41,3 +63,10 @@ export class LambdaS3Client {
41
63
  );
42
64
  }
43
65
  }
66
+
67
+ type Config = {
68
+ bucketName: string;
69
+ region?: string;
70
+ accessKey?: string;
71
+ accessKeyID?: string;
72
+ };