@qrvey/object-storage 0.0.1 → 0.0.3

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.
package/dist/cjs/index.js CHANGED
@@ -5,247 +5,594 @@ var clientS3 = require('@aws-sdk/client-s3');
5
5
  var s3RequestPresigner = require('@aws-sdk/s3-request-presigner');
6
6
 
7
7
  var __defProp = Object.defineProperty;
8
- var __getOwnPropNames = Object.getOwnPropertyNames;
9
- var __knownSymbol = (name, symbol) => {
10
- if (symbol = Symbol[name])
11
- return symbol;
12
- throw Error("Symbol." + name + " is not defined");
8
+ var __defProps = Object.defineProperties;
9
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
10
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
11
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
12
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
13
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
14
+ var __spreadValues = (a, b) => {
15
+ for (var prop in b || (b = {}))
16
+ if (__hasOwnProp.call(b, prop))
17
+ __defNormalProp(a, prop, b[prop]);
18
+ if (__getOwnPropSymbols)
19
+ for (var prop of __getOwnPropSymbols(b)) {
20
+ if (__propIsEnum.call(b, prop))
21
+ __defNormalProp(a, prop, b[prop]);
22
+ }
23
+ return a;
13
24
  };
14
- var __esm = (fn, res) => function __init() {
15
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
25
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
26
+
27
+ // src/shared/utils/errorHandler.ts
28
+ var errorMessages = {
29
+ AccessDenied: "Access denied",
30
+ AccountProblem: "There is a problem with your account",
31
+ AllAccessDisabled: "All access to this resource has been disabled",
32
+ BucketAlreadyExists: "The requested bucket name is not available",
33
+ BucketNotEmpty: "The bucket you tried to delete is not empty",
34
+ EntityTooLarge: "The entity you are trying to upload is too large",
35
+ ExpiredToken: "The provided token has expired",
36
+ InternalError: "An internal server error has occurred",
37
+ InvalidAccessKeyId: "The AWS access key ID you provided does not exist in our records",
38
+ InvalidBucketName: "The specified bucket name is not valid",
39
+ InvalidObjectState: "The operation is not valid for the current state of the object",
40
+ InvalidToken: "The provided token is invalid",
41
+ NoSuchBucket: "The specified bucket does not exist",
42
+ NoSuchKey: "The specified key does not exist",
43
+ PreconditionFailed: "The condition specified in the request is not met",
44
+ RequestTimeout: "The request timed out",
45
+ ServiceUnavailable: "The service is currently unavailable",
46
+ SignatureDoesNotMatch: "The request signature we calculated does not match the signature you provided",
47
+ SlowDown: "Please reduce your request rate",
48
+ TemporaryRedirect: "Temporary redirect",
49
+ DEFAULT: "An unknown error occurred"
16
50
  };
17
- var __export = (target, all) => {
18
- for (var name in all)
19
- __defProp(target, name, { get: all[name], enumerable: true });
51
+ var ErrorHandler = class {
52
+ static handleError(errorCode, errorMessage, errorObj) {
53
+ const errorResponse = {
54
+ code: errorCode,
55
+ message: errorMessage || errorMessages[errorCode] || errorMessages["DEFAULT"],
56
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
57
+ error: errorObj ? {
58
+ name: errorObj.name,
59
+ message: errorObj.message,
60
+ stack: errorObj.stack || null
61
+ } : null
62
+ };
63
+ return errorResponse;
64
+ }
20
65
  };
21
- var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
66
+
67
+ // src/services/storage/blob/blobHelpers.ts
68
+ function BlobPropertiesResponseToObjectResponse(blobProperties) {
69
+ return {
70
+ lastModified: blobProperties.lastModified,
71
+ contentLength: blobProperties.contentLength,
72
+ contentType: blobProperties.contentType,
73
+ eTag: blobProperties.etag,
74
+ metadata: blobProperties.metadata,
75
+ contentEncoding: blobProperties.contentEncoding,
76
+ cacheControl: blobProperties.cacheControl,
77
+ contentDisposition: blobProperties.contentDisposition,
78
+ contentLanguage: blobProperties.contentLanguage
79
+ };
80
+ }
22
81
 
23
82
  // src/services/storage/blob/blobStorage.service.ts
24
- var blobStorage_service_exports = {};
25
- __export(blobStorage_service_exports, {
26
- BlobStorageService: () => BlobStorageService
27
- });
28
- var BlobStorageService;
29
- var init_blobStorage_service = __esm({
30
- "src/services/storage/blob/blobStorage.service.ts"() {
31
- BlobStorageService = class {
32
- constructor(containerName) {
33
- this.containerName = containerName;
34
- this.blobServiceClient = storageBlob.BlobServiceClient.fromConnectionString(
35
- process.env.AZURE_STORAGE_CONNECTION_STRING
83
+ var BlobStorageService = class {
84
+ /**
85
+ * Retrieves the properties of a blob from the container by its name.
86
+ *
87
+ * @param {string} blobName - The name of the blob.
88
+ * @return {Promise<GetObjectResponse>} A promise that resolves to the blob properties.
89
+ */
90
+ constructor(containerName) {
91
+ this.containerName = containerName;
92
+ this.blobServiceClient = storageBlob.BlobServiceClient.fromConnectionString(
93
+ process.env.AZURE_STORAGE_CONNECTION_STRING
94
+ );
95
+ }
96
+ getUploadUrl(key, expiresInMinutes) {
97
+ throw new Error("Method not implemented.");
98
+ }
99
+ /**
100
+ * Retrieves the properties of a blob from the container by its name.
101
+ *
102
+ * @param {string} blobName - The name of the blob.
103
+ * @return {Promise<GetObjectResponse>} A promise that resolves to the blob properties.
104
+ */
105
+ getHeadObject(blobName) {
106
+ return new Promise((resolve, reject) => {
107
+ try {
108
+ const containerClient = this.blobServiceClient.getContainerClient(
109
+ this.containerName
110
+ );
111
+ const blockBlobClient = containerClient.getBlockBlobClient(blobName);
112
+ blockBlobClient.getProperties().then((blobProperties) => {
113
+ return resolve(
114
+ BlobPropertiesResponseToObjectResponse(
115
+ blobProperties
116
+ )
117
+ );
118
+ }).catch(
119
+ (error) => reject(
120
+ ErrorHandler.handleError(
121
+ "DEFAULT",
122
+ "",
123
+ error
124
+ )
125
+ )
126
+ );
127
+ } catch (error) {
128
+ return reject(
129
+ ErrorHandler.handleError("DEFAULT", "", error)
36
130
  );
37
131
  }
38
- async list() {
39
- const list = [];
132
+ });
133
+ }
134
+ /**
135
+ * Retrieves an object from the blob storage service.
136
+ *
137
+ * @param {string} blobName - The name of the blob to retrieve.
138
+ * @return {Promise<GetObjectResponse>} A promise that resolves to the object data, including the body, metadata, content type, and content length, or rejects with an error if there was an issue.
139
+ */
140
+ getObject(blobName) {
141
+ return new Promise((resolve, reject) => {
142
+ try {
40
143
  const containerClient = this.blobServiceClient.getContainerClient(
41
144
  this.containerName
42
145
  );
43
- try {
44
- for (var iter = __forAwait(containerClient.listBlobsFlat()), more, temp, error; more = !(temp = await iter.next()).done; more = false) {
45
- const containerItem = temp.value;
46
- list.push(containerItem.name);
47
- }
48
- } catch (temp) {
49
- error = [temp];
50
- } finally {
51
- try {
52
- more && (temp = iter.return) && await temp.call(iter);
53
- } finally {
54
- if (error)
55
- throw error[0];
56
- }
57
- }
58
- return new Promise((resolve) => resolve({ keys: list }));
146
+ const blockBlobClient = containerClient.getBlockBlobClient(blobName);
147
+ blockBlobClient.download(0).then((downloadBlockBlobResponse) => {
148
+ return resolve({
149
+ body: downloadBlockBlobResponse.readableStreamBody,
150
+ metadata: {},
151
+ contentType: "",
152
+ contentLength: 0
153
+ });
154
+ }).catch(
155
+ (error) => reject(
156
+ ErrorHandler.handleError(
157
+ "DEFAULT",
158
+ "",
159
+ error
160
+ )
161
+ )
162
+ );
163
+ } catch (error) {
164
+ return reject(
165
+ ErrorHandler.handleError("DEFAULT", "", error)
166
+ );
59
167
  }
60
- getObject(blobName) {
61
- return new Promise((resolve, reject) => {
62
- try {
63
- const containerClient = this.blobServiceClient.getContainerClient(
64
- this.containerName
65
- );
66
- const blockBlobClient = containerClient.getBlockBlobClient(blobName);
67
- blockBlobClient.download(0).then((downloadBlockBlobResponse) => {
68
- return resolve({
69
- body: downloadBlockBlobResponse.readableStreamBody,
70
- metadata: {},
71
- contentType: "",
72
- contentLength: 0
73
- });
74
- });
75
- } catch (error) {
76
- return reject(error);
77
- }
78
- });
168
+ });
169
+ }
170
+ /**
171
+ * Retrieves a signed URL for the blob with the specified name that expires after a certain period.
172
+ *
173
+ * @param {string} blobName - The name of the blob to generate the signed URL for.
174
+ * @param {number} expiresInMinutes - The duration in minutes until the signed URL expires.
175
+ * @return {Promise<string>} A Promise that resolves with the signed URL.
176
+ */
177
+ getDownloadUrl(blobName, expiresInMinutes) {
178
+ return new Promise((resolve, reject) => {
179
+ try {
180
+ const containerClient = this.blobServiceClient.getContainerClient(
181
+ this.containerName
182
+ );
183
+ const blockBlobClient = containerClient.getBlockBlobClient(blobName);
184
+ const startDate = /* @__PURE__ */ new Date();
185
+ const expiryDate = new Date(startDate);
186
+ expiryDate.setMinutes(
187
+ startDate.getMinutes() + expiresInMinutes
188
+ );
189
+ blockBlobClient.generateSasUrl({
190
+ permissions: storageBlob.BlobSASPermissions.parse("r"),
191
+ startsOn: startDate,
192
+ expiresOn: expiryDate
193
+ }).then((sasUrl) => resolve(sasUrl)).catch(
194
+ (error) => reject(
195
+ ErrorHandler.handleError(
196
+ "DEFAULT",
197
+ "",
198
+ error
199
+ )
200
+ )
201
+ );
202
+ } catch (error) {
203
+ return reject(
204
+ ErrorHandler.handleError("DEFAULT", "", error)
205
+ );
79
206
  }
80
- async getSignedUrl(blobName, expiresInMinutes) {
81
- return new Promise((resolve, reject) => {
82
- try {
83
- const containerClient = this.blobServiceClient.getContainerClient(
84
- this.containerName
85
- );
86
- const blockBlobClient = containerClient.getBlockBlobClient(blobName);
87
- const startDate = /* @__PURE__ */ new Date();
88
- const expiryDate = new Date(startDate);
89
- expiryDate.setMinutes(
90
- startDate.getMinutes() + expiresInMinutes
91
- );
92
- blockBlobClient.generateSasUrl({
93
- permissions: storageBlob.BlobSASPermissions.parse("r"),
94
- startsOn: startDate,
95
- expiresOn: expiryDate
96
- }).then((sasUrl) => resolve(sasUrl));
97
- } catch (error) {
98
- return reject(error);
99
- }
100
- });
207
+ });
208
+ }
209
+ /**
210
+ * Uploads a file to the blob storage service.
211
+ *
212
+ * @param {string} blobName - The name of the blob to upload.
213
+ * @param {FileContent} body - The content of the file to upload.
214
+ * @param {{ [key: string]: string } | undefined} [metadata] - Optional metadata to associate with the blob.
215
+ * @return {Promise<UploadResponse>} A promise that resolves to the response object containing the key of the uploaded blob, or rejects with an error if there was an issue.
216
+ */
217
+ upload(blobName, body, metadata) {
218
+ return new Promise((resolve, reject) => {
219
+ try {
220
+ const containerClient = this.blobServiceClient.getContainerClient(
221
+ this.containerName
222
+ );
223
+ const blockBlobClient = containerClient.getBlockBlobClient(blobName);
224
+ blockBlobClient.uploadStream(
225
+ body,
226
+ void 0,
227
+ void 0,
228
+ metadata
229
+ ).then(() => resolve({ key: blobName })).catch(
230
+ (error) => reject(
231
+ ErrorHandler.handleError(
232
+ "DEFAULT",
233
+ "",
234
+ error
235
+ )
236
+ )
237
+ );
238
+ } catch (error) {
239
+ return reject(
240
+ ErrorHandler.handleError("DEFAULT", "", error)
241
+ );
101
242
  }
102
- async upload(blobName, body, metadata) {
103
- return new Promise((resolve, reject) => {
104
- try {
105
- const containerClient = this.blobServiceClient.getContainerClient(
106
- this.containerName
107
- );
108
- const blockBlobClient = containerClient.getBlockBlobClient(blobName);
109
- blockBlobClient.uploadStream(body).then(() => resolve({ key: blobName }));
110
- } catch (error) {
111
- return reject(error);
112
- }
113
- });
243
+ });
244
+ }
245
+ /**
246
+ * Deletes a blob from the blob storage service.
247
+ *
248
+ * @param {string} blobName - The name of the blob to be deleted.
249
+ * @return {Promise<boolean>} A promise that resolves to true if the blob is successfully deleted, or rejects with an error if there was an issue.
250
+ */
251
+ delete(blobName) {
252
+ return new Promise((resolve, reject) => {
253
+ try {
254
+ const containerClient = this.blobServiceClient.getContainerClient(
255
+ this.containerName
256
+ );
257
+ const blockBlobClient = containerClient.getBlockBlobClient(blobName);
258
+ blockBlobClient.delete().then(() => resolve(true)).catch(
259
+ (error) => reject(
260
+ ErrorHandler.handleError(
261
+ "DEFAULT",
262
+ "",
263
+ error
264
+ )
265
+ )
266
+ );
267
+ } catch (error) {
268
+ return reject(
269
+ ErrorHandler.handleError("DEFAULT", "", error)
270
+ );
114
271
  }
115
- delete(blobName) {
116
- return new Promise((resolve, reject) => {
117
- try {
118
- const containerClient = this.blobServiceClient.getContainerClient(
119
- this.containerName
120
- );
121
- const blockBlobClient = containerClient.getBlockBlobClient(blobName);
122
- blockBlobClient.delete().then(() => resolve(true));
123
- } catch (error) {
124
- return reject(error);
125
- }
126
- });
272
+ });
273
+ }
274
+ /**
275
+ * Lists a chunk of blobs from the specified container client based on the provided options and continuation token.
276
+ *
277
+ * @param {ListRequestOptions} options - The options for listing the blobs.
278
+ * @param {string | undefined} continuationToken - The continuation token for pagination.
279
+ * @param {number} limit - The maximum number of blobs to list in a single page.
280
+ * @param {ContainerClient} containerClient - The container client to list the blobs from.
281
+ * @return {Promise<{ items: BlobItem[]; continuationToken?: string }>} - A promise that resolves to an object containing the list of blob items and an optional continuation token.
282
+ */
283
+ async listChunk(options, continuationToken, limit, containerClient) {
284
+ const iterator = containerClient.listBlobsFlat({
285
+ prefix: options.prefix
286
+ }).byPage({
287
+ maxPageSize: limit,
288
+ continuationToken
289
+ });
290
+ const items = [];
291
+ const response = (await iterator.next()).value;
292
+ items.push(...response.segment.blobItems);
293
+ return {
294
+ items,
295
+ continuationToken: response.continuationToken
296
+ };
297
+ }
298
+ /**
299
+ * Retrieves a list of blob contents based on the provided options.
300
+ *
301
+ * @param {ListRequestOptions} options - The options for listing the blob contents.
302
+ * @return {Promise<{ contents: BlobItem[]; nextContinuationToken?: string }>} A promise that resolves to the list of blob contents and the next continuation token.
303
+ */
304
+ async listContents(options) {
305
+ var _a;
306
+ let responseContents = [];
307
+ let continuationToken = options.pagination;
308
+ const limit = (_a = options.limit) != null ? _a : 1e3;
309
+ let continueListing = true;
310
+ const containerClient = this.blobServiceClient.getContainerClient(
311
+ this.containerName
312
+ );
313
+ while (continueListing) {
314
+ const response = await this.listChunk(
315
+ options,
316
+ continuationToken,
317
+ limit - responseContents.length,
318
+ containerClient
319
+ );
320
+ continuationToken = response.continuationToken;
321
+ responseContents = responseContents.concat(response.items);
322
+ if (responseContents.length >= limit || !continuationToken) {
323
+ continueListing = false;
127
324
  }
325
+ }
326
+ return {
327
+ contents: responseContents,
328
+ nextContinuationToken: continuationToken
128
329
  };
129
330
  }
130
- });
331
+ /**
332
+ * Lists blobs in the Azure Blob Storage container with pagination.
333
+ * @param options - The options for listing blobs.
334
+ * @returns A promise that resolves to a paginated list of blob names.
335
+ */
336
+ async list(options) {
337
+ var _a;
338
+ let items = [];
339
+ const response = await this.listContents(options);
340
+ items = response.contents.length ? response.contents.map((blob) => {
341
+ return {
342
+ key: blob.name,
343
+ lastModified: blob.properties.lastModified,
344
+ size: blob.properties.contentLength,
345
+ eTag: blob.properties.etag
346
+ };
347
+ }) : [];
348
+ return {
349
+ items,
350
+ pagination: ((_a = response.nextContinuationToken) == null ? void 0 : _a.length) ? response.nextContinuationToken : null,
351
+ count: items.length
352
+ };
353
+ }
354
+ /**
355
+ * Lists all blobs in the Azure Blob Storage container.
356
+ * @param options - The options for listing blobs.
357
+ * @returns A promise that resolves to list of blob names.
358
+ */
359
+ async listAll(options) {
360
+ var _a, _b;
361
+ let allItems = [];
362
+ let pagination = void 0;
363
+ do {
364
+ const response = await this.list(__spreadProps(__spreadValues({}, options), {
365
+ pagination
366
+ }));
367
+ if ((_a = response.items) == null ? void 0 : _a.length)
368
+ allItems = [...allItems, ...response.items];
369
+ pagination = (_b = response.pagination) != null ? _b : void 0;
370
+ } while (pagination);
371
+ return { items: allItems, count: allItems.length };
372
+ }
373
+ };
374
+
375
+ // src/services/storage/s3/s3Helpers.ts
376
+ function listResponseContentsToListResponseItems(responseContents) {
377
+ return responseContents.map((responseContent) => {
378
+ return {
379
+ key: responseContent.Key,
380
+ lastModified: new Date(responseContent.LastModified),
381
+ size: responseContent.Size,
382
+ eTag: responseContent.ETag
383
+ };
384
+ });
385
+ }
386
+ function s3ObjectToObjectResponse(s3Object) {
387
+ return {
388
+ body: s3Object.Body,
389
+ metadata: s3Object.Metadata,
390
+ contentType: s3Object.ContentType,
391
+ contentLength: s3Object.ContentLength,
392
+ contentEncoding: s3Object.ContentEncoding,
393
+ cacheControl: s3Object.CacheControl,
394
+ contentDisposition: s3Object.ContentDisposition,
395
+ contentLanguage: s3Object.ContentLanguage,
396
+ eTag: s3Object.ETag,
397
+ lastModified: s3Object.LastModified
398
+ };
399
+ }
131
400
 
132
401
  // src/services/storage/s3/s3Storage.service.ts
133
- var s3Storage_service_exports = {};
134
- __export(s3Storage_service_exports, {
135
- S3StorageService: () => S3StorageService
136
- });
137
- var S3StorageService;
138
- var init_s3Storage_service = __esm({
139
- "src/services/storage/s3/s3Storage.service.ts"() {
140
- S3StorageService = class {
141
- constructor(bucketName) {
142
- this.bucketName = bucketName;
143
- this.s3Client = new clientS3.S3Client({});
144
- }
145
- /**
146
- * Lists all objects in the S3 bucket.
147
- * @returns A promise that resolves to a list of object keys.
148
- */
149
- async list() {
150
- const allKeys = [];
151
- let continuationToken = void 0;
152
- do {
153
- const command = new clientS3.ListObjectsV2Command({
154
- Bucket: this.bucketName,
155
- ContinuationToken: continuationToken
156
- });
157
- const response = await this.s3Client.send(command);
158
- if (response.Contents) {
159
- for (const item of response.Contents) {
160
- if (item.Key)
161
- allKeys.push(item.Key);
162
- }
163
- }
164
- continuationToken = response.NextContinuationToken;
165
- } while (continuationToken);
166
- return { keys: allKeys };
167
- }
168
- /**
169
- * Retrieves an object from the S3 bucket.
170
- * @param key - The key of the object to retrieve.
171
- * @returns A promise that resolves to the content of the file.
172
- */
173
- async getObject(key) {
174
- var _a, _b;
175
- const command = new clientS3.GetObjectCommand({
176
- Bucket: this.bucketName,
177
- Key: key
178
- });
179
- const response = await this.s3Client.send(command);
180
- return {
181
- body: response.Body,
182
- metadata: (_a = response.Metadata) != null ? _a : {},
183
- contentType: (_b = response.ContentType) != null ? _b : "",
184
- contentLength: response.ContentLength
185
- };
186
- }
187
- /**
188
- * Generates a signed URL for accessing an object in the S3 bucket.
189
- * @param key - The key of the object.
190
- * @param expiresInMinutes - The number of minutes the signed URL should be valid for. Defaults to 60 minutes.
191
- * @returns A promise that resolves to the signed URL.
192
- */
193
- getSignedUrl(key, expiresInMinutes = 60) {
194
- const expiresIn = expiresInMinutes * 60;
195
- const command = new clientS3.GetObjectCommand({
196
- Bucket: this.bucketName,
197
- Key: key
198
- });
199
- return s3RequestPresigner.getSignedUrl(this.s3Client, command, { expiresIn });
200
- }
201
- /**
202
- * Uploads an object to the S3 bucket.
203
- * @param key - The key of the object to upload.
204
- * @param body - The content of the object to upload.
205
- * @param metadata - Optional metadata to associate with the object.
206
- * @returns A promise that resolves to the response containing the key of the uploaded object.
207
- */
208
- async upload(key, body, metadata) {
209
- const command = new clientS3.PutObjectCommand({
210
- Bucket: this.bucketName,
211
- Key: key,
212
- Body: body,
213
- Metadata: metadata
214
- });
215
- await this.s3Client.send(command);
216
- return { key };
217
- }
218
- /**
219
- * Deletes an object from the S3 bucket.
220
- * @param key - The key of the object to delete.
221
- * @returns A promise that resolves to true if the object was deleted successfully.
222
- */
223
- async delete(key) {
224
- const command = new clientS3.DeleteObjectCommand({
225
- Bucket: this.bucketName,
226
- Key: key
227
- });
228
- await this.s3Client.send(command);
229
- return true;
402
+ var S3StorageService = class {
403
+ constructor(bucketName) {
404
+ this.s3Client = new clientS3.S3Client({
405
+ region: process.env.AWS_REGION
406
+ });
407
+ this.bucketName = bucketName;
408
+ }
409
+ /**
410
+ * Retrieves a chunk of objects from the S3 bucket based on the provided options.
411
+ *
412
+ * @param {ListRequestOptions} options - The options for listing the objects.
413
+ * @param {string | undefined} continuationToken - The continuation token for pagination.
414
+ * @param {number} limit - The maximum number of objects to retrieve.
415
+ * @return {Promise<ListObjectsV2CommandOutput>} - A promise that resolves to the response containing the list of objects and metadata.
416
+ */
417
+ async listChunk(options, continuationToken, limit) {
418
+ const command = new clientS3.ListObjectsV2Command({
419
+ Bucket: this.bucketName,
420
+ ContinuationToken: continuationToken || options.pagination,
421
+ Prefix: options.prefix,
422
+ MaxKeys: limit
423
+ });
424
+ return this.s3Client.send(command);
425
+ }
426
+ /**
427
+ * Retrieves a list of contents from the S3 bucket based on the provided options.
428
+ *
429
+ * @param {ListRequestOptions} options - The options for listing the contents.
430
+ * @return {Promise<{ contents: unknown[]; nextContinuationToken?: string }>} - A promise that resolves to an object containing the list of contents and an optional next continuation token.
431
+ */
432
+ async listContents(options) {
433
+ var _a, _b;
434
+ let responseContents = [];
435
+ let continuationToken = void 0;
436
+ const limit = (_a = options.limit) != null ? _a : 1e3;
437
+ let continueListing = true;
438
+ while (continueListing) {
439
+ const listChunkLimit = limit - responseContents.length;
440
+ const response = await this.listChunk(
441
+ options,
442
+ continuationToken,
443
+ listChunkLimit
444
+ );
445
+ continuationToken = response.NextContinuationToken;
446
+ responseContents = responseContents.concat((_b = response.Contents) != null ? _b : []);
447
+ if (responseContents.length >= limit || !continuationToken) {
448
+ continueListing = false;
230
449
  }
450
+ }
451
+ return {
452
+ contents: responseContents,
453
+ nextContinuationToken: continuationToken
454
+ };
455
+ }
456
+ /**
457
+ * Lists objects in the S3 bucket with pagination.
458
+ * @param options - The options for listing objects.
459
+ * @returns A promise that resolves to a paginated list of object keys.
460
+ */
461
+ async list(options) {
462
+ var _a;
463
+ let items = [];
464
+ const response = await this.listContents(options);
465
+ items = response.contents.length ? listResponseContentsToListResponseItems(response.contents) : [];
466
+ return {
467
+ items,
468
+ pagination: (_a = response.nextContinuationToken) != null ? _a : null,
469
+ count: items.length
470
+ };
471
+ }
472
+ /**
473
+ * Lists all objects in the S3 bucket.
474
+ * @param options - The options for listing objects.
475
+ * @returns A promise that resolves to list of object keys.
476
+ */
477
+ async listAll(options) {
478
+ var _a;
479
+ let allItems = [];
480
+ let pagination = void 0;
481
+ do {
482
+ const response = await this.list(__spreadProps(__spreadValues({}, options), {
483
+ pagination
484
+ }));
485
+ if ((_a = response.items) == null ? void 0 : _a.length)
486
+ allItems = [...allItems, ...response.items];
487
+ pagination = response.pagination === null ? void 0 : response.pagination;
488
+ } while (pagination);
489
+ return { items: allItems, count: allItems.length };
490
+ }
491
+ /**
492
+ * Retrieves an object from the S3 bucket.
493
+ * @param key - The key of the object to retrieve.
494
+ * @returns A promise that resolves to the content of the file.
495
+ */
496
+ async getHeadObject(key) {
497
+ const command = new clientS3.HeadObjectCommand({
498
+ Bucket: this.bucketName,
499
+ Key: key
500
+ });
501
+ const response = await this.s3Client.send(command);
502
+ return s3ObjectToObjectResponse(response);
503
+ }
504
+ /**
505
+ * Retrieves an object from the S3 bucket.
506
+ * @param key - The key of the object to retrieve.
507
+ * @returns A promise that resolves to the content of the file.
508
+ */
509
+ async getObject(key) {
510
+ const command = new clientS3.GetObjectCommand({
511
+ Bucket: this.bucketName,
512
+ Key: key
513
+ });
514
+ const response = await this.s3Client.send(command);
515
+ return s3ObjectToObjectResponse(response);
516
+ }
517
+ /**
518
+ * Generates a signed URL for accessing an object in the S3 bucket.
519
+ * @param key - The key of the object.
520
+ * @param expiresInMinutes - The number of minutes the signed URL should be valid for. Defaults to 60 minutes.
521
+ * @returns A promise that resolves to the signed URL.
522
+ */
523
+ getDownloadUrl(key, expiresInMinutes = 60) {
524
+ const expiresIn = expiresInMinutes * 60;
525
+ const command = new clientS3.GetObjectCommand({
526
+ Bucket: this.bucketName,
527
+ Key: key
528
+ });
529
+ return s3RequestPresigner.getSignedUrl(this.s3Client, command, { expiresIn });
530
+ }
531
+ /**
532
+ * Retrieves a signed URL for uploading an object to the S3 bucket.
533
+ *
534
+ * @param {string} key - The key of the object to be uploaded.
535
+ * @param {number} [expiresInMinutes=60] - The number of minutes the signed URL should be valid for. Defaults to 60 minutes.
536
+ * @returns A promise that resolves to the signed URL.
537
+ */
538
+ async getUploadUrl(key, expiresInMinutes = 60) {
539
+ const expiresIn = expiresInMinutes * 60;
540
+ const putObjectCommandParams = {
541
+ Bucket: process.env.UPLOAD_BUCKET_NAME,
542
+ Key: key,
543
+ ACL: "private"
544
+ };
545
+ const command = new clientS3.PutObjectCommand(putObjectCommandParams);
546
+ const signedUrl = await s3RequestPresigner.getSignedUrl(this.s3Client, command, {
547
+ expiresIn
548
+ });
549
+ return {
550
+ signedUrl,
551
+ key: `https://${process.env.UPLOAD_BUCKET_NAME}.s3.${process.env.AWS_DEFAULT_REGION}.amazonaws.com/${key}`
231
552
  };
232
553
  }
233
- });
554
+ /**
555
+ * Uploads an object to the S3 bucket.
556
+ * @param key - The key of the object to upload.
557
+ * @param body - The content of the object to upload.
558
+ * @param metadata - Optional metadata to associate with the object.
559
+ * @returns A promise that resolves to the response containing the key of the uploaded object.
560
+ */
561
+ async upload(key, body, metadata) {
562
+ const command = new clientS3.PutObjectCommand({
563
+ Bucket: this.bucketName,
564
+ Key: key,
565
+ Body: body,
566
+ Metadata: metadata
567
+ });
568
+ await this.s3Client.send(command);
569
+ return { key };
570
+ }
571
+ /**
572
+ * Deletes an object from the S3 bucket.
573
+ * @param key - The key of the object to delete.
574
+ * @returns A promise that resolves to true if the object was deleted successfully.
575
+ */
576
+ async delete(key) {
577
+ const command = new clientS3.DeleteObjectCommand({
578
+ Bucket: this.bucketName,
579
+ Key: key
580
+ });
581
+ await this.s3Client.send(command);
582
+ return true;
583
+ }
584
+ };
234
585
 
235
586
  // src/services/objectStorageFactory.service.ts
236
587
  var ObjectStorageFactory = class {
237
588
  static async instance(bucketName) {
238
589
  var _a;
239
590
  const isBlobStorageService = ((_a = process.env.OBJECT_STORAGE_SERVICE) == null ? void 0 : _a.toLowerCase()) === "azure_blob_storage";
240
- let ObjectStorageService2;
241
591
  if (isBlobStorageService) {
242
- const module = await Promise.resolve().then(() => (init_blobStorage_service(), blobStorage_service_exports));
243
- ObjectStorageService2 = module.BlobStorageService;
592
+ return new BlobStorageService(bucketName);
244
593
  } else {
245
- const module = await Promise.resolve().then(() => (init_s3Storage_service(), s3Storage_service_exports));
246
- ObjectStorageService2 = module.S3StorageService;
594
+ return new S3StorageService(bucketName);
247
595
  }
248
- return new ObjectStorageService2(bucketName);
249
596
  }
250
597
  };
251
598
 
@@ -259,26 +606,89 @@ var ObjectStorageService = class _ObjectStorageService {
259
606
  throw new Error("Bucket name not provided or not valid value");
260
607
  return ObjectStorageFactory.instance(bucketName);
261
608
  }
262
- static async list(bucketName) {
609
+ /**
610
+ * Retrieves a list of objects from the object storage service.
611
+ *
612
+ * @param {ListRequestOptions} options - The options to apply to the list operation.
613
+ * @param {string} [bucketName] - The name of the bucket to list objects from. If not provided, the default bucket name will be used.
614
+ * @return {Promise<ListResponse>} A promise that resolves to the list of objects.
615
+ */
616
+ static async list(options, bucketName) {
263
617
  return _ObjectStorageService.getObjectStorageServiceInstance(
264
618
  bucketName
265
- ).then((instance) => instance.list());
619
+ ).then((instance) => instance.list(options));
266
620
  }
621
+ /**
622
+ * Retrieves a list of all objects from the object storage service.
623
+ *
624
+ * @param {ListRequestOptions} options - The options to apply to the list operation.
625
+ * @param {string} [bucketName] - The name of the bucket to list objects from. If not provided, the default bucket name will be used.
626
+ * @return {Promise<ListResponse>} A promise that resolves to the list of all objects.
627
+ */
628
+ static async listAll(options, bucketName) {
629
+ return _ObjectStorageService.getObjectStorageServiceInstance(
630
+ bucketName
631
+ ).then((instance) => instance.listAll(options));
632
+ }
633
+ /**
634
+ * Retrieves an object from the object storage service.
635
+ *
636
+ * @param {string} key - The key of the object to retrieve.
637
+ * @param {string} [bucketName] - The name of the bucket where the object is stored. If not provided, the default bucket name will be used.
638
+ * @return {Promise<GetObjectResponse>} A promise that resolves to the retrieved object.
639
+ */
267
640
  static async getObject(key, bucketName) {
268
641
  return _ObjectStorageService.getObjectStorageServiceInstance(
269
642
  bucketName
270
643
  ).then((instance) => instance.getObject(key));
271
644
  }
272
- static async getSignedUrl(key, expiresInMinutes, bucketName) {
645
+ /**
646
+ * Retrieves a signed URL for the specified object in the object storage service.
647
+ *
648
+ * @param {string} key - The key of the object for which to generate the signed URL.
649
+ * @param {number} expiresInMinutes - The number of minutes until the signed URL expires.
650
+ * @param {string} [bucketName] - The name of the bucket where the object is stored. If not provided, the default bucket name will be used.
651
+ * @return {Promise<string>} A promise that resolves to the generated signed URL.
652
+ */
653
+ static async getDownloadUrl(key, expiresInMinutes, bucketName) {
654
+ return _ObjectStorageService.getObjectStorageServiceInstance(
655
+ bucketName
656
+ ).then((instance) => instance.getDownloadUrl(key, expiresInMinutes));
657
+ }
658
+ /**
659
+ * Retrieves an upload URL for the specified object in the object storage service.
660
+ *
661
+ * @param {string} key - The key of the object for which to generate the upload URL.
662
+ * @param {number} expiresInMinutes - The number of minutes until the upload URL expires.
663
+ * @param {string} [bucketName] - The name of the bucket where the object will be stored. If not provided, the default bucket name will be used.
664
+ * @return {Promise<string>} A promise that resolves to the generated upload URL.
665
+ */
666
+ static async getUploadUrl(key, expiresInMinutes, bucketName) {
273
667
  return _ObjectStorageService.getObjectStorageServiceInstance(
274
668
  bucketName
275
- ).then((instance) => instance.getSignedUrl(key, expiresInMinutes));
669
+ ).then((instance) => instance.getUploadUrl(key, expiresInMinutes));
276
670
  }
671
+ /**
672
+ * Uploads a file to the object storage service.
673
+ *
674
+ * @param {string} key - The key of the object to upload.
675
+ * @param {FileContent} body - The content of the file to upload.
676
+ * @param {Object} [metadata] - Optional metadata to associate with the object.
677
+ * @param {string} [bucketName] - The name of the bucket where the object will be stored. If not provided, the default bucket name will be used.
678
+ * @return {Promise<UploadResponse>} A promise that resolves to the response of the upload operation.
679
+ */
277
680
  static async upload(key, body, metadata, bucketName) {
278
681
  return _ObjectStorageService.getObjectStorageServiceInstance(
279
682
  bucketName
280
683
  ).then((instance) => instance.upload(key, body, metadata));
281
684
  }
685
+ /**
686
+ * Deletes an object from the object storage service.
687
+ *
688
+ * @param {string} key - The key of the object to delete.
689
+ * @param {string} [bucketName] - The name of the bucket where the object is stored. If not provided, the default bucket name will be used.
690
+ * @return {Promise<boolean>} A promise that resolves to true if the object was successfully deleted, or false otherwise.
691
+ */
282
692
  static async delete(key, bucketName) {
283
693
  return _ObjectStorageService.getObjectStorageServiceInstance(
284
694
  bucketName