@qrvey/object-storage 0.0.3 → 0.0.5
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 +175 -139
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.mts +17 -2
- package/dist/esm/index.mjs +171 -139
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/index.d.ts +17 -2
- package/package.json +2 -1
package/dist/esm/index.d.mts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Readable } from 'stream';
|
|
1
|
+
import stream, { Readable } from 'stream';
|
|
2
2
|
|
|
3
3
|
type UploadResponse = {
|
|
4
4
|
key: string;
|
|
5
5
|
};
|
|
6
6
|
|
|
7
|
-
type FileContent = Readable | ReadableStream | Blob;
|
|
7
|
+
type FileContent = Readable | ReadableStream | Blob | Buffer;
|
|
8
8
|
|
|
9
9
|
type ListRequestOptions = {
|
|
10
10
|
prefix?: string;
|
|
@@ -45,6 +45,12 @@ type GetUploadUrlResponse = {
|
|
|
45
45
|
signedUrl: string;
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
+
type CreateUploadWriteStreamResponse = {
|
|
49
|
+
key: string;
|
|
50
|
+
stream: stream.PassThrough;
|
|
51
|
+
promise: Promise<unknown>;
|
|
52
|
+
};
|
|
53
|
+
|
|
48
54
|
interface IObjectStorage {
|
|
49
55
|
list(options: ListRequestOptions): Promise<ListResponse>;
|
|
50
56
|
listAll(options: ListRequestOptions): Promise<ListResponse>;
|
|
@@ -55,6 +61,7 @@ interface IObjectStorage {
|
|
|
55
61
|
upload(key: string, body: FileContent, metadata?: {
|
|
56
62
|
[key: string]: string;
|
|
57
63
|
}): Promise<UploadResponse>;
|
|
64
|
+
createUploadWriteStream(key: string): CreateUploadWriteStreamResponse;
|
|
58
65
|
delete(key: string): Promise<boolean>;
|
|
59
66
|
}
|
|
60
67
|
|
|
@@ -116,6 +123,14 @@ declare class ObjectStorageService {
|
|
|
116
123
|
static upload(key: string, body: FileContent, metadata?: {
|
|
117
124
|
[key: string]: string;
|
|
118
125
|
}, bucketName?: string): Promise<UploadResponse>;
|
|
126
|
+
/**
|
|
127
|
+
* Creates an upload write stream for the specified key in the object storage service.
|
|
128
|
+
*
|
|
129
|
+
* @param {string} key - The key of the object to create the upload write stream for.
|
|
130
|
+
* @param {string} [bucketName] - The name of the bucket where the object will be stored. If not provided, the default bucket name will be used.
|
|
131
|
+
* @return {Promise<CreateUploadWriteStreamResponse>} A promise that resolves to the response of the upload operation.
|
|
132
|
+
*/
|
|
133
|
+
static createUploadWriteStream(key: string, bucketName?: string): Promise<CreateUploadWriteStreamResponse>;
|
|
119
134
|
/**
|
|
120
135
|
* Deletes an object from the object storage service.
|
|
121
136
|
*
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { BlobServiceClient, BlobSASPermissions } from '@azure/storage-blob';
|
|
2
2
|
import { S3Client, ListObjectsV2Command, HeadObjectCommand, GetObjectCommand, PutObjectCommand, DeleteObjectCommand } from '@aws-sdk/client-s3';
|
|
3
3
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
4
|
+
import stream from 'stream';
|
|
5
|
+
import { Upload } from '@aws-sdk/lib-storage';
|
|
4
6
|
|
|
5
7
|
var __defProp = Object.defineProperty;
|
|
6
8
|
var __defProps = Object.defineProperties;
|
|
@@ -44,6 +46,7 @@ var errorMessages = {
|
|
|
44
46
|
SignatureDoesNotMatch: "The request signature we calculated does not match the signature you provided",
|
|
45
47
|
SlowDown: "Please reduce your request rate",
|
|
46
48
|
TemporaryRedirect: "Temporary redirect",
|
|
49
|
+
ObjectNotFound: "ObjectNotFound - The specified key does not exist. (404)",
|
|
47
50
|
DEFAULT: "An unknown error occurred"
|
|
48
51
|
};
|
|
49
52
|
var ErrorHandler = class {
|
|
@@ -91,7 +94,8 @@ var BlobStorageService = class {
|
|
|
91
94
|
process.env.AZURE_STORAGE_CONNECTION_STRING
|
|
92
95
|
);
|
|
93
96
|
}
|
|
94
|
-
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
|
|
98
|
+
createUploadWriteStream(key) {
|
|
95
99
|
throw new Error("Method not implemented.");
|
|
96
100
|
}
|
|
97
101
|
/**
|
|
@@ -100,34 +104,17 @@ var BlobStorageService = class {
|
|
|
100
104
|
* @param {string} blobName - The name of the blob.
|
|
101
105
|
* @return {Promise<GetObjectResponse>} A promise that resolves to the blob properties.
|
|
102
106
|
*/
|
|
103
|
-
getHeadObject(blobName) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
)
|
|
115
|
-
);
|
|
116
|
-
}).catch(
|
|
117
|
-
(error) => reject(
|
|
118
|
-
ErrorHandler.handleError(
|
|
119
|
-
"DEFAULT",
|
|
120
|
-
"",
|
|
121
|
-
error
|
|
122
|
-
)
|
|
123
|
-
)
|
|
124
|
-
);
|
|
125
|
-
} catch (error) {
|
|
126
|
-
return reject(
|
|
127
|
-
ErrorHandler.handleError("DEFAULT", "", error)
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
});
|
|
107
|
+
async getHeadObject(blobName) {
|
|
108
|
+
try {
|
|
109
|
+
const containerClient = this.blobServiceClient.getContainerClient(
|
|
110
|
+
this.containerName
|
|
111
|
+
);
|
|
112
|
+
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
|
|
113
|
+
const blobProperties = await blockBlobClient.getProperties();
|
|
114
|
+
return BlobPropertiesResponseToObjectResponse(blobProperties);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
throw ErrorHandler.handleError("DEFAULT", "", error);
|
|
117
|
+
}
|
|
131
118
|
}
|
|
132
119
|
/**
|
|
133
120
|
* Retrieves an object from the blob storage service.
|
|
@@ -135,35 +122,57 @@ var BlobStorageService = class {
|
|
|
135
122
|
* @param {string} blobName - The name of the blob to retrieve.
|
|
136
123
|
* @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.
|
|
137
124
|
*/
|
|
138
|
-
getObject(blobName) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
"",
|
|
157
|
-
error
|
|
158
|
-
)
|
|
159
|
-
)
|
|
160
|
-
);
|
|
161
|
-
} catch (error) {
|
|
162
|
-
return reject(
|
|
163
|
-
ErrorHandler.handleError("DEFAULT", "", error)
|
|
164
|
-
);
|
|
125
|
+
async getObject(blobName) {
|
|
126
|
+
var _a;
|
|
127
|
+
try {
|
|
128
|
+
const containerClient = this.blobServiceClient.getContainerClient(
|
|
129
|
+
this.containerName
|
|
130
|
+
);
|
|
131
|
+
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
|
|
132
|
+
const downloadBlockBlobResponse = await blockBlobClient.download(0);
|
|
133
|
+
return {
|
|
134
|
+
body: downloadBlockBlobResponse.readableStreamBody,
|
|
135
|
+
metadata: downloadBlockBlobResponse.metadata,
|
|
136
|
+
contentType: downloadBlockBlobResponse.contentType,
|
|
137
|
+
contentLength: downloadBlockBlobResponse.contentLength
|
|
138
|
+
};
|
|
139
|
+
} catch (error) {
|
|
140
|
+
let errorKey = "DEFAULT";
|
|
141
|
+
if (((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.status) === 404) {
|
|
142
|
+
errorKey = "ObjectNotFound";
|
|
165
143
|
}
|
|
166
|
-
|
|
144
|
+
throw ErrorHandler.handleError(errorKey, "", error);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
async getSignatureUrl(blobName, expiresInMinutes, permissions) {
|
|
148
|
+
try {
|
|
149
|
+
const containerClient = this.blobServiceClient.getContainerClient(
|
|
150
|
+
this.containerName
|
|
151
|
+
);
|
|
152
|
+
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
|
|
153
|
+
const startDate = /* @__PURE__ */ new Date();
|
|
154
|
+
const expiryDate = new Date(startDate);
|
|
155
|
+
expiryDate.setMinutes(startDate.getMinutes() + expiresInMinutes);
|
|
156
|
+
return blockBlobClient.generateSasUrl({
|
|
157
|
+
permissions: BlobSASPermissions.parse(permissions),
|
|
158
|
+
startsOn: startDate,
|
|
159
|
+
expiresOn: expiryDate
|
|
160
|
+
});
|
|
161
|
+
} catch (error) {
|
|
162
|
+
throw ErrorHandler.handleError("DEFAULT", "", error);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
async getUploadUrl(blobName, expiresInMinutes) {
|
|
166
|
+
try {
|
|
167
|
+
const sasUrl = await this.getSignatureUrl(
|
|
168
|
+
blobName,
|
|
169
|
+
expiresInMinutes,
|
|
170
|
+
"w"
|
|
171
|
+
);
|
|
172
|
+
return { key: blobName, signedUrl: sasUrl };
|
|
173
|
+
} catch (error) {
|
|
174
|
+
throw ErrorHandler.handleError("DEFAULT", "", error);
|
|
175
|
+
}
|
|
167
176
|
}
|
|
168
177
|
/**
|
|
169
178
|
* Retrieves a signed URL for the blob with the specified name that expires after a certain period.
|
|
@@ -172,37 +181,17 @@ var BlobStorageService = class {
|
|
|
172
181
|
* @param {number} expiresInMinutes - The duration in minutes until the signed URL expires.
|
|
173
182
|
* @return {Promise<string>} A Promise that resolves with the signed URL.
|
|
174
183
|
*/
|
|
175
|
-
getDownloadUrl(blobName, expiresInMinutes) {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
);
|
|
187
|
-
blockBlobClient.generateSasUrl({
|
|
188
|
-
permissions: BlobSASPermissions.parse("r"),
|
|
189
|
-
startsOn: startDate,
|
|
190
|
-
expiresOn: expiryDate
|
|
191
|
-
}).then((sasUrl) => resolve(sasUrl)).catch(
|
|
192
|
-
(error) => reject(
|
|
193
|
-
ErrorHandler.handleError(
|
|
194
|
-
"DEFAULT",
|
|
195
|
-
"",
|
|
196
|
-
error
|
|
197
|
-
)
|
|
198
|
-
)
|
|
199
|
-
);
|
|
200
|
-
} catch (error) {
|
|
201
|
-
return reject(
|
|
202
|
-
ErrorHandler.handleError("DEFAULT", "", error)
|
|
203
|
-
);
|
|
204
|
-
}
|
|
205
|
-
});
|
|
184
|
+
async getDownloadUrl(blobName, expiresInMinutes) {
|
|
185
|
+
try {
|
|
186
|
+
const sasUrl = await this.getSignatureUrl(
|
|
187
|
+
blobName,
|
|
188
|
+
expiresInMinutes,
|
|
189
|
+
"r"
|
|
190
|
+
);
|
|
191
|
+
return sasUrl;
|
|
192
|
+
} catch (error) {
|
|
193
|
+
throw ErrorHandler.handleError("DEFAULT", "", error);
|
|
194
|
+
}
|
|
206
195
|
}
|
|
207
196
|
/**
|
|
208
197
|
* Uploads a file to the blob storage service.
|
|
@@ -212,33 +201,22 @@ var BlobStorageService = class {
|
|
|
212
201
|
* @param {{ [key: string]: string } | undefined} [metadata] - Optional metadata to associate with the blob.
|
|
213
202
|
* @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.
|
|
214
203
|
*/
|
|
215
|
-
upload(blobName, body, metadata) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
"",
|
|
232
|
-
error
|
|
233
|
-
)
|
|
234
|
-
)
|
|
235
|
-
);
|
|
236
|
-
} catch (error) {
|
|
237
|
-
return reject(
|
|
238
|
-
ErrorHandler.handleError("DEFAULT", "", error)
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
});
|
|
204
|
+
async upload(blobName, body, metadata = {}) {
|
|
205
|
+
try {
|
|
206
|
+
const containerClient = this.blobServiceClient.getContainerClient(
|
|
207
|
+
this.containerName
|
|
208
|
+
);
|
|
209
|
+
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
|
|
210
|
+
Buffer.isBuffer(body) ? await blockBlobClient.upload(body, body.length, { metadata }) : await blockBlobClient.uploadStream(
|
|
211
|
+
body,
|
|
212
|
+
void 0,
|
|
213
|
+
void 0,
|
|
214
|
+
{ metadata }
|
|
215
|
+
);
|
|
216
|
+
return { key: blobName };
|
|
217
|
+
} catch (error) {
|
|
218
|
+
throw ErrorHandler.handleError("DEFAULT", "", error);
|
|
219
|
+
}
|
|
242
220
|
}
|
|
243
221
|
/**
|
|
244
222
|
* Deletes a blob from the blob storage service.
|
|
@@ -246,28 +224,15 @@ var BlobStorageService = class {
|
|
|
246
224
|
* @param {string} blobName - The name of the blob to be deleted.
|
|
247
225
|
* @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.
|
|
248
226
|
*/
|
|
249
|
-
delete(
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
ErrorHandler.handleError(
|
|
259
|
-
"DEFAULT",
|
|
260
|
-
"",
|
|
261
|
-
error
|
|
262
|
-
)
|
|
263
|
-
)
|
|
264
|
-
);
|
|
265
|
-
} catch (error) {
|
|
266
|
-
return reject(
|
|
267
|
-
ErrorHandler.handleError("DEFAULT", "", error)
|
|
268
|
-
);
|
|
269
|
-
}
|
|
270
|
-
});
|
|
227
|
+
async delete(data) {
|
|
228
|
+
try {
|
|
229
|
+
const containerClient = this.blobServiceClient.getContainerClient(
|
|
230
|
+
this.containerName
|
|
231
|
+
);
|
|
232
|
+
return this.deleteObject(containerClient, data);
|
|
233
|
+
} catch (error) {
|
|
234
|
+
throw ErrorHandler.handleError("DEFAULT", "", error);
|
|
235
|
+
}
|
|
271
236
|
}
|
|
272
237
|
/**
|
|
273
238
|
* Lists a chunk of blobs from the specified container client based on the provided options and continuation token.
|
|
@@ -368,6 +333,40 @@ var BlobStorageService = class {
|
|
|
368
333
|
} while (pagination);
|
|
369
334
|
return { items: allItems, count: allItems.length };
|
|
370
335
|
}
|
|
336
|
+
/**
|
|
337
|
+
* Deletes a blob from the blob storage service.
|
|
338
|
+
*
|
|
339
|
+
* @param {string} blobName - The name of the blob to be deleted.
|
|
340
|
+
* @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.
|
|
341
|
+
*/
|
|
342
|
+
async deleteObject(containerClient, blobName) {
|
|
343
|
+
try {
|
|
344
|
+
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
|
|
345
|
+
await blockBlobClient.delete();
|
|
346
|
+
return true;
|
|
347
|
+
} catch (error) {
|
|
348
|
+
throw ErrorHandler.handleError("DEFAULT", "", error);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
async deleteObjects(blobNames) {
|
|
352
|
+
const containerClient = this.blobServiceClient.getContainerClient(
|
|
353
|
+
this.containerName
|
|
354
|
+
);
|
|
355
|
+
const deleteBlobPromises = blobNames.map(async (blobName) => {
|
|
356
|
+
var _a;
|
|
357
|
+
try {
|
|
358
|
+
await this.deleteObject(containerClient, blobName);
|
|
359
|
+
return { key: blobName, deleted: true };
|
|
360
|
+
} catch (error) {
|
|
361
|
+
return {
|
|
362
|
+
key: blobName,
|
|
363
|
+
deleted: false,
|
|
364
|
+
error: (_a = error == null ? void 0 : error.message) != null ? _a : ""
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
return Promise.all(deleteBlobPromises);
|
|
369
|
+
}
|
|
371
370
|
};
|
|
372
371
|
|
|
373
372
|
// src/services/storage/s3/s3Helpers.ts
|
|
@@ -395,8 +394,6 @@ function s3ObjectToObjectResponse(s3Object) {
|
|
|
395
394
|
lastModified: s3Object.LastModified
|
|
396
395
|
};
|
|
397
396
|
}
|
|
398
|
-
|
|
399
|
-
// src/services/storage/s3/s3Storage.service.ts
|
|
400
397
|
var S3StorageService = class {
|
|
401
398
|
constructor(bucketName) {
|
|
402
399
|
this.s3Client = new S3Client({
|
|
@@ -566,6 +563,29 @@ var S3StorageService = class {
|
|
|
566
563
|
await this.s3Client.send(command);
|
|
567
564
|
return { key };
|
|
568
565
|
}
|
|
566
|
+
/**
|
|
567
|
+
* Creates a writable stream for uploading a file to the S3 bucket.
|
|
568
|
+
*
|
|
569
|
+
* @param {string} key - The key of the object to upload.
|
|
570
|
+
* @return {CreateUploadWriteStreamResponse} An object containing the key of the uploaded object, the writable stream, and a promise that resolves when the upload is complete.
|
|
571
|
+
*/
|
|
572
|
+
createUploadWriteStream(key) {
|
|
573
|
+
const streamPassThrough = new stream.PassThrough();
|
|
574
|
+
const params = {
|
|
575
|
+
Bucket: this.bucketName,
|
|
576
|
+
Key: key,
|
|
577
|
+
Body: streamPassThrough
|
|
578
|
+
};
|
|
579
|
+
const upload = new Upload({
|
|
580
|
+
client: this.s3Client,
|
|
581
|
+
params
|
|
582
|
+
});
|
|
583
|
+
return {
|
|
584
|
+
key,
|
|
585
|
+
stream: streamPassThrough,
|
|
586
|
+
promise: upload.done()
|
|
587
|
+
};
|
|
588
|
+
}
|
|
569
589
|
/**
|
|
570
590
|
* Deletes an object from the S3 bucket.
|
|
571
591
|
* @param key - The key of the object to delete.
|
|
@@ -680,6 +700,18 @@ var ObjectStorageService = class _ObjectStorageService {
|
|
|
680
700
|
bucketName
|
|
681
701
|
).then((instance) => instance.upload(key, body, metadata));
|
|
682
702
|
}
|
|
703
|
+
/**
|
|
704
|
+
* Creates an upload write stream for the specified key in the object storage service.
|
|
705
|
+
*
|
|
706
|
+
* @param {string} key - The key of the object to create the upload write stream for.
|
|
707
|
+
* @param {string} [bucketName] - The name of the bucket where the object will be stored. If not provided, the default bucket name will be used.
|
|
708
|
+
* @return {Promise<CreateUploadWriteStreamResponse>} A promise that resolves to the response of the upload operation.
|
|
709
|
+
*/
|
|
710
|
+
static async createUploadWriteStream(key, bucketName) {
|
|
711
|
+
return _ObjectStorageService.getObjectStorageServiceInstance(
|
|
712
|
+
bucketName
|
|
713
|
+
).then((instance) => instance.createUploadWriteStream(key));
|
|
714
|
+
}
|
|
683
715
|
/**
|
|
684
716
|
* Deletes an object from the object storage service.
|
|
685
717
|
*
|