@qrvey/object-storage 0.0.2 → 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
@@ -4,8 +4,698 @@ var storageBlob = require('@azure/storage-blob');
4
4
  var clientS3 = require('@aws-sdk/client-s3');
5
5
  var s3RequestPresigner = require('@aws-sdk/s3-request-presigner');
6
6
 
7
- var C=(r,e)=>{if(e=Symbol[r])return e;throw Error("Symbol."+r+" is not defined")};var u=(r,e,t)=>(e=r[C("asyncIterator")])?e.call(r):(r=r[C("iterator")](),e={},t=(n,o)=>(o=r[n])&&(e[n]=s=>new Promise((a,i,c)=>(s=o.call(r,s),c=s.done,Promise.resolve(s.value).then(p=>a({value:p,done:c}),i)))),t("next"),t("return"),e);var l=class{constructor(e){this.containerName=e,this.blobServiceClient=storageBlob.BlobServiceClient.fromConnectionString(process.env.AZURE_STORAGE_CONNECTION_STRING);}async list(){let e=[],t=this.blobServiceClient.getContainerClient(this.containerName);try{for(var n=u(t.listBlobsFlat()),o,s,a;o=!(s=await n.next()).done;o=!1){let i=s.value;e.push(i.name);}}catch(s){a=[s];}finally{try{o&&(s=n.return)&&await s.call(n);}finally{if(a)throw a[0]}}return new Promise(i=>i({keys:e}))}getObject(e){return new Promise((t,n)=>{try{this.blobServiceClient.getContainerClient(this.containerName).getBlockBlobClient(e).download(0).then(a=>t({body:a.readableStreamBody,metadata:{},contentType:"",contentLength:0}));}catch(o){return n(o)}})}async getSignedUrl(e,t){return new Promise((n,o)=>{try{let a=this.blobServiceClient.getContainerClient(this.containerName).getBlockBlobClient(e),i=new Date,c=new Date(i);c.setMinutes(i.getMinutes()+t),a.generateSasUrl({permissions:storageBlob.BlobSASPermissions.parse("r"),startsOn:i,expiresOn:c}).then(p=>n(p));}catch(s){return o(s)}})}async upload(e,t,n){return new Promise((o,s)=>{try{this.blobServiceClient.getContainerClient(this.containerName).getBlockBlobClient(e).uploadStream(t).then(()=>o({key:e}));}catch(a){return s(a)}})}delete(e){return new Promise((t,n)=>{try{this.blobServiceClient.getContainerClient(this.containerName).getBlockBlobClient(e).delete().then(()=>t(!0));}catch(o){return n(o)}})}};var m=class{constructor(e){this.bucketName=e,this.s3Client=new clientS3.S3Client({});}async list(){let e=[],t;do{let n=new clientS3.ListObjectsV2Command({Bucket:this.bucketName,ContinuationToken:t}),o=await this.s3Client.send(n);if(o.Contents)for(let s of o.Contents)s.Key&&e.push(s.Key);t=o.NextContinuationToken;}while(t);return {keys:e}}async getObject(e){var o,s;let t=new clientS3.GetObjectCommand({Bucket:this.bucketName,Key:e}),n=await this.s3Client.send(t);return {body:n.Body,metadata:(o=n.Metadata)!=null?o:{},contentType:(s=n.ContentType)!=null?s:"",contentLength:n.ContentLength}}getSignedUrl(e,t=60){let n=t*60,o=new clientS3.GetObjectCommand({Bucket:this.bucketName,Key:e});return s3RequestPresigner.getSignedUrl(this.s3Client,o,{expiresIn:n})}async upload(e,t,n){let o=new clientS3.PutObjectCommand({Bucket:this.bucketName,Key:e,Body:t,Metadata:n});return await this.s3Client.send(o),{key:e}}async delete(e){let t=new clientS3.DeleteObjectCommand({Bucket:this.bucketName,Key:e});return await this.s3Client.send(t),!0}};var b=class{static async instance(e){var n;return ((n=process.env.OBJECT_STORAGE_SERVICE)==null?void 0:n.toLowerCase())==="azure_blob_storage"?new l(e):new m(e)}};var g=class r{constructor(e){r.bucketName=e;}static async getObjectStorageServiceInstance(e=r.bucketName){if(!e)throw new Error("Bucket name not provided or not valid value");return b.instance(e)}static async list(e){return r.getObjectStorageServiceInstance(e).then(t=>t.list())}static async getObject(e,t){return r.getObjectStorageServiceInstance(t).then(n=>n.getObject(e))}static async getSignedUrl(e,t,n){return r.getObjectStorageServiceInstance(n).then(o=>o.getSignedUrl(e,t))}static async upload(e,t,n,o){return r.getObjectStorageServiceInstance(o).then(s=>s.upload(e,t,n))}static async delete(e,t){return r.getObjectStorageServiceInstance(t).then(n=>n.delete(e))}};
7
+ var __defProp = Object.defineProperty;
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;
24
+ };
25
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
8
26
 
9
- exports.ObjectStorageService = g;
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"
50
+ };
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
+ }
65
+ };
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
+ }
81
+
82
+ // src/services/storage/blob/blobStorage.service.ts
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)
130
+ );
131
+ }
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 {
143
+ const containerClient = this.blobServiceClient.getContainerClient(
144
+ this.containerName
145
+ );
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
+ );
167
+ }
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
+ );
206
+ }
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
+ );
242
+ }
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
+ );
271
+ }
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;
324
+ }
325
+ }
326
+ return {
327
+ contents: responseContents,
328
+ nextContinuationToken: continuationToken
329
+ };
330
+ }
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
+ }
400
+
401
+ // src/services/storage/s3/s3Storage.service.ts
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;
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}`
552
+ };
553
+ }
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
+ };
585
+
586
+ // src/services/objectStorageFactory.service.ts
587
+ var ObjectStorageFactory = class {
588
+ static async instance(bucketName) {
589
+ var _a;
590
+ const isBlobStorageService = ((_a = process.env.OBJECT_STORAGE_SERVICE) == null ? void 0 : _a.toLowerCase()) === "azure_blob_storage";
591
+ if (isBlobStorageService) {
592
+ return new BlobStorageService(bucketName);
593
+ } else {
594
+ return new S3StorageService(bucketName);
595
+ }
596
+ }
597
+ };
598
+
599
+ // src/services/objectStorage.service.ts
600
+ var ObjectStorageService = class _ObjectStorageService {
601
+ constructor(bucketName) {
602
+ _ObjectStorageService.bucketName = bucketName;
603
+ }
604
+ static async getObjectStorageServiceInstance(bucketName = _ObjectStorageService.bucketName) {
605
+ if (!bucketName)
606
+ throw new Error("Bucket name not provided or not valid value");
607
+ return ObjectStorageFactory.instance(bucketName);
608
+ }
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) {
617
+ return _ObjectStorageService.getObjectStorageServiceInstance(
618
+ bucketName
619
+ ).then((instance) => instance.list(options));
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
+ */
640
+ static async getObject(key, bucketName) {
641
+ return _ObjectStorageService.getObjectStorageServiceInstance(
642
+ bucketName
643
+ ).then((instance) => instance.getObject(key));
644
+ }
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) {
667
+ return _ObjectStorageService.getObjectStorageServiceInstance(
668
+ bucketName
669
+ ).then((instance) => instance.getUploadUrl(key, expiresInMinutes));
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
+ */
680
+ static async upload(key, body, metadata, bucketName) {
681
+ return _ObjectStorageService.getObjectStorageServiceInstance(
682
+ bucketName
683
+ ).then((instance) => instance.upload(key, body, metadata));
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
+ */
692
+ static async delete(key, bucketName) {
693
+ return _ObjectStorageService.getObjectStorageServiceInstance(
694
+ bucketName
695
+ ).then((instance) => instance.delete(key));
696
+ }
697
+ };
698
+
699
+ exports.ObjectStorageService = ObjectStorageService;
10
700
  //# sourceMappingURL=out.js.map
11
701
  //# sourceMappingURL=index.js.map