@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/README.md +1 -1
- package/dist/cjs/index.js +624 -214
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.mts +129 -0
- package/dist/esm/index.mjs +651 -11
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/index.d.ts +94 -10
- package/package.json +4 -15
- package/dist/esm/blobStorage.service-Q2W6LO3S.mjs +0 -105
- package/dist/esm/blobStorage.service-Q2W6LO3S.mjs.map +0 -1
- package/dist/esm/chunk-JHXQKOVY.mjs +0 -10
- package/dist/esm/chunk-JHXQKOVY.mjs.map +0 -1
- package/dist/esm/s3Storage.service-OZGBAWBM.mjs +0 -100
- package/dist/esm/s3Storage.service-OZGBAWBM.mjs.map +0 -1
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
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
|
15
|
-
|
|
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
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
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
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
39
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
-
|
|
243
|
-
ObjectStorageService2 = module.BlobStorageService;
|
|
592
|
+
return new BlobStorageService(bucketName);
|
|
244
593
|
} else {
|
|
245
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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
|