vtlab-generic-functions 1.0.7 → 1.0.9

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/s3bucket/index.js +28 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vtlab-generic-functions",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "scripts": {
5
5
  "test": "jest"
6
6
  },
package/s3bucket/index.js CHANGED
@@ -4,6 +4,7 @@ const config = require('../config');
4
4
  const multer = require('multer');
5
5
  const multerS3 = require('multer-s3');
6
6
  const { promisesCollector } = require('../utils');
7
+ const stream = require("stream");
7
8
 
8
9
  /**
9
10
  * Uploads a file to an S3 bucket.
@@ -106,8 +107,25 @@ const downloadFileFromS3 = async (path) => {
106
107
  Key: path
107
108
  });
108
109
 
109
- const data = await s3Client.send(command);
110
- return data;
110
+ try {
111
+ let response = await s3Client.send(command);
112
+ let Body = response.Body;
113
+
114
+ if (Body && Body instanceof stream.Readable) {
115
+ // Handling the stream
116
+ const chunks = [];
117
+ for await (const chunk of Body) {
118
+ chunks.push(chunk);
119
+ }
120
+ response.Body = Buffer.concat(chunks);
121
+ return response;
122
+ } else {
123
+ // If Body is not a stream, return as is
124
+ return response;
125
+ }
126
+ } catch (error) {
127
+ throw new Error(error.message);
128
+ }
111
129
  } catch (error) {
112
130
  throw error;
113
131
  }
@@ -157,7 +175,14 @@ const generatePresignedUrl = async (bucket, key, expirationInSeconds = 604000) =
157
175
  if(expirationInSeconds > 604000){ //7 days is the max expiration time for a presigned url v4
158
176
  expirationInSeconds = 604000;
159
177
  }
160
- const s3Client = new S3Client();
178
+ const allConfig = await config.get();
179
+ const s3Client = new S3Client({
180
+ credentials: {
181
+ accessKeyId: allConfig.awsCredentials.accessKeyId,
182
+ secretAccessKey: allConfig.awsCredentials.secretAccessKey,
183
+ },
184
+ region: allConfig.awsCredentials.region
185
+ });
161
186
 
162
187
  const command = new GetObjectCommand({
163
188
  Bucket: bucket,