@pi-r/aws-v3 0.7.3 → 0.8.1
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/LICENSE +6 -6
- package/README.md +6 -6
- package/client/index.js +54 -28
- package/download/index.js +18 -7
- package/package.json +33 -33
- package/types/index.d.ts +39 -40
- package/upload/index.js +41 -15
package/LICENSE
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
Copyright 2024 An Pham
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
-
|
|
5
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
-
|
|
1
|
+
Copyright 2024 An Pham
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
7
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
### @pi-r/aws-v3
|
|
2
|
-
|
|
3
|
-
https://e-mc.readthedocs.io/en/latest/cloud/aws-v3.html
|
|
4
|
-
|
|
5
|
-
### LICENSE
|
|
6
|
-
|
|
1
|
+
### @pi-r/aws-v3
|
|
2
|
+
|
|
3
|
+
https://e-mc.readthedocs.io/en/latest/cloud/aws-v3.html
|
|
4
|
+
|
|
5
|
+
### LICENSE
|
|
6
|
+
|
|
7
7
|
MIT
|
package/client/index.js
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
exports.CLOUD_UPLOAD_CHUNK = exports.CLOUD_UPLOAD_STREAM =
|
|
2
|
+
exports.CLOUD_UPLOAD_CHUNK = exports.CLOUD_UPLOAD_STREAM = void 0;
|
|
3
|
+
exports.createStorageClient = createStorageClient;
|
|
4
|
+
exports.createDatabaseClient = createDatabaseClient;
|
|
5
|
+
exports.validateStorage = validateStorage;
|
|
6
|
+
exports.validateDatabase = validateDatabase;
|
|
7
|
+
exports.createBucket = createBucket;
|
|
8
|
+
exports.createBucketV2 = createBucketV2;
|
|
9
|
+
exports.setBucketPolicy = setBucketPolicy;
|
|
10
|
+
exports.setBucketTagging = setBucketTagging;
|
|
11
|
+
exports.setBucketWebsite = setBucketWebsite;
|
|
12
|
+
exports.deleteObjects = deleteObjects;
|
|
13
|
+
exports.deleteObjectsV2 = deleteObjectsV2;
|
|
14
|
+
exports.executeQuery = executeQuery;
|
|
15
|
+
exports.executeBatchQuery = executeBatchQuery;
|
|
3
16
|
const Lib = require("@aws-sdk/lib-dynamodb");
|
|
4
17
|
const Client = require("@aws-sdk/client-dynamodb");
|
|
5
18
|
const Providers = require("@aws-sdk/credential-providers");
|
|
@@ -115,11 +128,9 @@ function createStorageClient(credential, service = "aws-v3", sdk = "@aws-sdk/cli
|
|
|
115
128
|
throw err;
|
|
116
129
|
}
|
|
117
130
|
}
|
|
118
|
-
exports.createStorageClient = createStorageClient;
|
|
119
131
|
function createDatabaseClient(credential) {
|
|
120
132
|
return [Lib.DynamoDBDocumentClient.from(new Client.DynamoDBClient(fromProvider(credential)), credential.translateConfig), Lib];
|
|
121
133
|
}
|
|
122
|
-
exports.createDatabaseClient = createDatabaseClient;
|
|
123
134
|
function validateStorage(credential) {
|
|
124
135
|
const credentials = credential.credentials;
|
|
125
136
|
if (!credentials && !credential.provider && (0, aws_lib_1.isSharedCredentialsDefined)()) {
|
|
@@ -128,15 +139,12 @@ function validateStorage(credential) {
|
|
|
128
139
|
}
|
|
129
140
|
return !!credentials && ((0, aws_lib_1.isAccessDefined)(credentials) || typeof credentials === 'function') || (0, types_1.isPlainObject)(credential.provider) || (0, aws_lib_1.isEnvDefined)();
|
|
130
141
|
}
|
|
131
|
-
exports.validateStorage = validateStorage;
|
|
132
142
|
function validateDatabase(credential, data) {
|
|
133
143
|
return (0, aws_lib_1.isDatabaseDefined)(credential, data) && validateStorage(credential);
|
|
134
144
|
}
|
|
135
|
-
exports.validateDatabase = validateDatabase;
|
|
136
145
|
async function createBucket(credential, Bucket, publicRead, service = "aws-v3", sdk = "@aws-sdk/client-s3") {
|
|
137
146
|
return createBucketV2.call(this, credential, Bucket, publicRead ? "public-read" : undefined, undefined, service, sdk);
|
|
138
147
|
}
|
|
139
|
-
exports.createBucket = createBucket;
|
|
140
148
|
async function createBucketV2(credential, Bucket, ACL, options, service = "aws-v3", sdk = "@aws-sdk/client-s3") {
|
|
141
149
|
ACL = ACL === 1 ? 1 : (0, aws_lib_1.checkBucketCannedACL)(ACL);
|
|
142
150
|
const [client, AWS] = createStorageClient.call(this, credential, service, sdk);
|
|
@@ -176,7 +184,6 @@ async function createBucketV2(credential, Bucket, ACL, options, service = "aws-v
|
|
|
176
184
|
});
|
|
177
185
|
});
|
|
178
186
|
}
|
|
179
|
-
exports.createBucketV2 = createBucketV2;
|
|
180
187
|
async function setBucketPolicy(credential, Bucket, options, service = "aws-v3", sdk = "@aws-sdk/client-s3") {
|
|
181
188
|
const [client, AWS] = createStorageClient.call(this, credential, service, sdk);
|
|
182
189
|
const block = 'PublicAccessBlockConfiguration' in options;
|
|
@@ -194,7 +201,6 @@ async function setBucketPolicy(credential, Bucket, options, service = "aws-v3",
|
|
|
194
201
|
return false;
|
|
195
202
|
});
|
|
196
203
|
}
|
|
197
|
-
exports.setBucketPolicy = setBucketPolicy;
|
|
198
204
|
async function setBucketTagging(credential, Bucket, options, service = "aws-v3", sdk = "@aws-sdk/client-s3") {
|
|
199
205
|
if (!(0, types_1.isPlainObject)(options) || !Array.isArray(options.Tagging?.TagSet)) {
|
|
200
206
|
return false;
|
|
@@ -217,7 +223,6 @@ async function setBucketTagging(credential, Bucket, options, service = "aws-v3",
|
|
|
217
223
|
options.Bucket = Bucket;
|
|
218
224
|
return client.send(new AWS.PutBucketTaggingCommand(options)).then(command).catch((err) => error(err));
|
|
219
225
|
}
|
|
220
|
-
exports.setBucketTagging = setBucketTagging;
|
|
221
226
|
async function setBucketWebsite(credential, Bucket, options, service = "aws-v3", sdk = "@aws-sdk/client-s3") {
|
|
222
227
|
const [client, AWS] = createStorageClient.call(this, credential, service, sdk);
|
|
223
228
|
const WebsiteConfiguration = {};
|
|
@@ -240,11 +245,9 @@ async function setBucketWebsite(credential, Bucket, options, service = "aws-v3",
|
|
|
240
245
|
return false;
|
|
241
246
|
});
|
|
242
247
|
}
|
|
243
|
-
exports.setBucketWebsite = setBucketWebsite;
|
|
244
248
|
async function deleteObjects(credential, Bucket, service = "aws-v3", sdk = "@aws-sdk/client-s3") {
|
|
245
249
|
return deleteObjectsV2.call(this, credential, Bucket, true, service, sdk);
|
|
246
250
|
}
|
|
247
|
-
exports.deleteObjects = deleteObjects;
|
|
248
251
|
async function deleteObjectsV2(credential, Bucket, recursive = true, service = "aws-v3", sdk = "@aws-sdk/client-s3") {
|
|
249
252
|
const [client, AWS] = createStorageClient.call(this, credential, service, sdk);
|
|
250
253
|
return client.send(new AWS.ListObjectsCommand({ Bucket }))
|
|
@@ -256,9 +259,8 @@ async function deleteObjectsV2(credential, Bucket, recursive = true, service = "
|
|
|
256
259
|
}
|
|
257
260
|
return client.send(new AWS.DeleteObjectsCommand({ Bucket, Delete: { Objects } }))
|
|
258
261
|
.then(data => {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const files = Deleted.length + ' files';
|
|
262
|
+
if ((0, types_1.isArray)(data.Deleted)) {
|
|
263
|
+
const files = data.Deleted.length + ' files';
|
|
262
264
|
this.formatMessage(64, service, ["Bucket emptied" + ` (${recursive ? 'recursive' : files})`, Bucket], recursive ? files : '', { ...Cloud.LOG_CLOUD_COMMAND });
|
|
263
265
|
}
|
|
264
266
|
})
|
|
@@ -273,11 +275,9 @@ async function deleteObjectsV2(credential, Bucket, recursive = true, service = "
|
|
|
273
275
|
}
|
|
274
276
|
});
|
|
275
277
|
}
|
|
276
|
-
exports.deleteObjectsV2 = deleteObjectsV2;
|
|
277
278
|
async function executeQuery(credential, data, sessionKey) {
|
|
278
279
|
return (await executeBatchQuery.call(this, credential, [data], sessionKey))[0] || [];
|
|
279
280
|
}
|
|
280
|
-
exports.executeQuery = executeQuery;
|
|
281
281
|
async function executeBatchQuery(credential, batch, sessionKey) {
|
|
282
282
|
const length = batch.length;
|
|
283
283
|
const result = new Array(length);
|
|
@@ -291,12 +291,6 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
291
291
|
const item = batch[i];
|
|
292
292
|
let { service, table: TableName, id, query, key, limit = 0, update, ignoreCache } = item;
|
|
293
293
|
const useCache = caching && ignoreCache !== true;
|
|
294
|
-
const getCache = (value) => {
|
|
295
|
-
if (ignoreCache !== 1) {
|
|
296
|
-
cacheValue.renewCache = ignoreCache === 0;
|
|
297
|
-
return this.getQueryResult(service, credential, value, cacheValue);
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
294
|
cacheValue.exclusiveOf = Array.isArray(ignoreCache) ? ignoreCache : undefined;
|
|
301
295
|
let rows, queryString = '';
|
|
302
296
|
if (key && (id || (0, types_1.isPlainObject)(key))) {
|
|
@@ -306,7 +300,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
306
300
|
}
|
|
307
301
|
if (useCache) {
|
|
308
302
|
queryString = TableName + '_' + Cloud.asString(key, true) + (id !== undefined ? '_' + Cloud.asString(id, true) : '');
|
|
309
|
-
if (!update && (rows =
|
|
303
|
+
if (!update && (rows = this.getCacheResult(service, credential, queryString, cacheValue, ignoreCache))) {
|
|
310
304
|
result[i] = rows;
|
|
311
305
|
continue;
|
|
312
306
|
}
|
|
@@ -314,7 +308,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
314
308
|
const [db, AWS] = createClient();
|
|
315
309
|
const Key = (0, types_1.isPlainObject)(key) ? key : { [key]: id };
|
|
316
310
|
const command = { TableName, Key };
|
|
317
|
-
if (update) {
|
|
311
|
+
if ((0, types_1.isPlainObject)(update)) {
|
|
318
312
|
await db.send(new AWS.UpdateCommand({ ...command, ...update }));
|
|
319
313
|
}
|
|
320
314
|
const Item = (await db.send(new AWS.GetCommand(command))).Item;
|
|
@@ -333,7 +327,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
333
327
|
if (limit > 0) {
|
|
334
328
|
query.Limit = limit;
|
|
335
329
|
}
|
|
336
|
-
if (useCache && (rows =
|
|
330
|
+
if (useCache && (rows = this.getCacheResult(service, credential, queryString = Cloud.asString(query, true), cacheValue, ignoreCache))) {
|
|
337
331
|
result[i] = rows;
|
|
338
332
|
continue;
|
|
339
333
|
}
|
|
@@ -350,12 +344,13 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
350
344
|
}
|
|
351
345
|
TableName ||= Object.keys(params.RequestItems)[0];
|
|
352
346
|
if (!TableName) {
|
|
347
|
+
closeClient();
|
|
353
348
|
throw (0, util_1.formatError)(item, "Missing database table");
|
|
354
349
|
}
|
|
355
350
|
const Item = params.RequestItems[TableName] ||= {};
|
|
356
351
|
Item.Keys = query;
|
|
357
352
|
params = { RequestItems: { [TableName]: Item } };
|
|
358
|
-
if (useCache && (rows =
|
|
353
|
+
if (useCache && (rows = this.getCacheResult(service, credential, queryString = Cloud.asString(params, true), cacheValue, ignoreCache))) {
|
|
359
354
|
result[i] = rows;
|
|
360
355
|
continue;
|
|
361
356
|
}
|
|
@@ -365,6 +360,38 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
365
360
|
rows = Responses[TableName];
|
|
366
361
|
}
|
|
367
362
|
}
|
|
363
|
+
else if ((0, types_1.isString)(query)) {
|
|
364
|
+
const Statement = query.trim();
|
|
365
|
+
const Parameters = (0, types_1.isArray)(item.params) ? item.params : undefined;
|
|
366
|
+
const Limit = limit > 0 ? limit : undefined;
|
|
367
|
+
if (!/^select\s/i.test(Statement)) {
|
|
368
|
+
closeClient();
|
|
369
|
+
throw (0, util_1.formatError)(item, "Missing database query");
|
|
370
|
+
}
|
|
371
|
+
const db = createClient()[0];
|
|
372
|
+
if ((0, types_1.isString)(update) && /^(?:insert|delete|update)\s/i.test(update) && !update.includes('?')) {
|
|
373
|
+
await db.send(new Lib.ExecuteStatementCommand({ Statement: update })).catch((err) => {
|
|
374
|
+
this.formatFail(64, service, "Unable to execute query", err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
update = '';
|
|
379
|
+
}
|
|
380
|
+
if (useCache && !update && (rows = this.getCacheResult(service, credential, queryString = Statement + '_' + Cloud.asString(Parameters, true) + (Limit ? '_' + Limit : ''), cacheValue, ignoreCache))) {
|
|
381
|
+
result[i] = rows;
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
const { Items } = await db.send(new Lib.ExecuteStatementCommand({ Statement, Parameters, Limit }));
|
|
385
|
+
if (Items) {
|
|
386
|
+
rows = Items;
|
|
387
|
+
if (rows.length === 0) {
|
|
388
|
+
queryString = '';
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
queryString = '';
|
|
393
|
+
}
|
|
394
|
+
}
|
|
368
395
|
else if (TableName) {
|
|
369
396
|
let params = item.params;
|
|
370
397
|
if ((0, types_1.isPlainObject)(params)) {
|
|
@@ -373,7 +400,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
373
400
|
else {
|
|
374
401
|
params = { TableName };
|
|
375
402
|
}
|
|
376
|
-
if (useCache && (rows =
|
|
403
|
+
if (useCache && (rows = this.getCacheResult(service, credential, queryString = Cloud.asString(params, true), cacheValue, ignoreCache))) {
|
|
377
404
|
result[i] = rows;
|
|
378
405
|
continue;
|
|
379
406
|
}
|
|
@@ -392,6 +419,5 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
392
419
|
closeClient();
|
|
393
420
|
return result;
|
|
394
421
|
}
|
|
395
|
-
exports.executeBatchQuery = executeBatchQuery;
|
|
396
422
|
exports.CLOUD_UPLOAD_STREAM = true;
|
|
397
423
|
exports.CLOUD_UPLOAD_CHUNK = true;
|
package/download/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
const Cloud = require("@e-mc/cloud");
|
|
2
3
|
const util_1 = require("@e-mc/cloud/util");
|
|
3
4
|
const types_1 = require("@e-mc/types");
|
|
4
|
-
const Cloud = require("@e-mc/cloud");
|
|
5
5
|
const client_1 = require("@pi-r/aws-v3");
|
|
6
|
-
|
|
6
|
+
function download(config, service = "aws-v3", sdk = "@aws-sdk/client-s3") {
|
|
7
7
|
const [client, AWS] = client_1.createStorageClient.call(this, config, service, sdk);
|
|
8
8
|
return (data, callback) => {
|
|
9
9
|
const { bucket: Bucket, download: target } = data;
|
|
@@ -15,17 +15,28 @@ module.exports = function download(config, service = "aws-v3", sdk = "@aws-sdk/c
|
|
|
15
15
|
const location = Cloud.joinPath(Bucket, Key);
|
|
16
16
|
client.send(new AWS.GetObjectCommand({ ...target.options, Bucket, Key, VersionId: target.versionId }), { abortSignal: this.signal })
|
|
17
17
|
.then(result => {
|
|
18
|
-
(0, util_1.readableAsBuffer)(result.Body).then(buffer =>
|
|
18
|
+
(0, util_1.readableAsBuffer)(result.Body).then(buffer => {
|
|
19
|
+
callback(null, buffer);
|
|
20
|
+
}).catch((err) => {
|
|
21
|
+
callback(err);
|
|
22
|
+
});
|
|
19
23
|
let deleteObject = target.deleteObject;
|
|
20
24
|
if (deleteObject) {
|
|
21
25
|
if (!(0, types_1.isPlainObject)(deleteObject)) {
|
|
22
26
|
deleteObject = undefined;
|
|
23
27
|
}
|
|
24
28
|
client.send(new AWS.DeleteObjectCommand({ ...deleteObject, Bucket, Key, VersionId: target.versionId }))
|
|
25
|
-
.then(() =>
|
|
26
|
-
|
|
29
|
+
.then(() => {
|
|
30
|
+
this.formatMessage(64, service, "Delete success", location, { ...Cloud.LOG_CLOUD_DELETE });
|
|
31
|
+
})
|
|
32
|
+
.catch((err) => {
|
|
33
|
+
this.formatFail(64, service, ["Delete failed", location], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: !!target.active });
|
|
34
|
+
});
|
|
27
35
|
}
|
|
28
36
|
})
|
|
29
|
-
.catch((err) =>
|
|
37
|
+
.catch((err) => {
|
|
38
|
+
callback(err);
|
|
39
|
+
});
|
|
30
40
|
};
|
|
31
|
-
}
|
|
41
|
+
}
|
|
42
|
+
module.exports = download;
|
package/package.json
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@pi-r/aws-v3",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "AWS
|
|
5
|
-
"main": "client/index.js",
|
|
6
|
-
"publishConfig": {
|
|
7
|
-
"access": "public"
|
|
8
|
-
},
|
|
9
|
-
"repository": {
|
|
10
|
-
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/anpham6/pi-r.git",
|
|
12
|
-
"directory": "src/cloud/aws-v3"
|
|
13
|
-
},
|
|
14
|
-
"keywords": [
|
|
15
|
-
"squared",
|
|
16
|
-
"e-mc",
|
|
17
|
-
"squared-functions"
|
|
18
|
-
],
|
|
19
|
-
"author": "An Pham <anpham6@gmail.com>",
|
|
20
|
-
"license": "MIT",
|
|
21
|
-
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
22
|
-
"dependencies": {
|
|
23
|
-
"@e-mc/cloud": "^0.
|
|
24
|
-
"@e-mc/module": "^0.
|
|
25
|
-
"@e-mc/types": "^0.
|
|
26
|
-
"@pi-r/aws-lib": "^0.
|
|
27
|
-
"@aws-sdk/client-dynamodb": "^3.
|
|
28
|
-
"@aws-sdk/client-s3": "^3.
|
|
29
|
-
"@aws-sdk/credential-providers": "^3.
|
|
30
|
-
"@aws-sdk/lib-dynamodb": "^3.
|
|
31
|
-
"@aws-sdk/lib-storage": "^3.
|
|
32
|
-
}
|
|
33
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@pi-r/aws-v3",
|
|
3
|
+
"version": "0.8.1",
|
|
4
|
+
"description": "AWS cloud functions for E-mc.",
|
|
5
|
+
"main": "client/index.js",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/anpham6/pi-r.git",
|
|
12
|
+
"directory": "src/cloud/aws-v3"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"squared",
|
|
16
|
+
"e-mc",
|
|
17
|
+
"squared-functions"
|
|
18
|
+
],
|
|
19
|
+
"author": "An Pham <anpham6@gmail.com>",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@e-mc/cloud": "^0.10.0",
|
|
24
|
+
"@e-mc/module": "^0.10.0",
|
|
25
|
+
"@e-mc/types": "^0.10.0",
|
|
26
|
+
"@pi-r/aws-lib": "^0.8.0",
|
|
27
|
+
"@aws-sdk/client-dynamodb": "^3.624.0",
|
|
28
|
+
"@aws-sdk/client-s3": "^3.624.0",
|
|
29
|
+
"@aws-sdk/credential-providers": "^3.624.0",
|
|
30
|
+
"@aws-sdk/lib-dynamodb": "^3.624.0",
|
|
31
|
+
"@aws-sdk/lib-storage": "^3.624.0"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,41 +1,40 @@
|
|
|
1
|
-
import type { CloudDatabase, CloudStorage } from '@e-mc/types/lib/cloud';
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
import type { DynamoDBClientConfig, QueryCommandInput, ScanCommandInput } from '@aws-sdk/client-dynamodb';
|
|
5
|
-
import type { BatchGetCommandInput, TranslateConfig, UpdateCommandInput } from '@aws-sdk/lib-dynamodb';
|
|
6
|
-
import type {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
1
|
+
import type { CloudDatabase, CloudStorage } from '@e-mc/types/lib/cloud';
|
|
2
|
+
|
|
3
|
+
import type { AwsSdkSigV4AuthInputConfig } from '@aws-sdk/core';
|
|
4
|
+
import type { DynamoDBClientConfig, QueryCommandInput, ScanCommandInput } from '@aws-sdk/client-dynamodb';
|
|
5
|
+
import type { BatchGetCommandInput, NativeAttributeValue, TranslateConfig, UpdateCommandInput } from '@aws-sdk/lib-dynamodb';
|
|
6
|
+
import type { S3ClientConfig } from '@aws-sdk/client-s3';
|
|
7
|
+
|
|
8
|
+
export interface AWSBaseCredential extends AwsSdkSigV4AuthInputConfig {
|
|
9
|
+
provider?: {
|
|
10
|
+
http?: unknown;
|
|
11
|
+
ini?: unknown;
|
|
12
|
+
cognitoIdentity?: unknown;
|
|
13
|
+
cognitoIdentityPool?: unknown;
|
|
14
|
+
temporaryCredentials?: unknown;
|
|
15
|
+
webToken?: unknown;
|
|
16
|
+
containerMetadata?: unknown;
|
|
17
|
+
instanceMetadata?: unknown;
|
|
18
|
+
process?: unknown;
|
|
19
|
+
tokenFile?: unknown;
|
|
20
|
+
sso?: unknown;
|
|
21
|
+
env?: unknown;
|
|
22
|
+
nodeProviderChain?: unknown;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type AWSStorage = CloudStorage<AWSStorageCredential, "aws-v3">;
|
|
27
|
+
|
|
28
|
+
export interface AWSStorageCredential extends S3ClientConfig, AWSBaseCredential {}
|
|
29
|
+
|
|
30
|
+
export interface AWSDatabaseQuery extends CloudDatabase<QueryCommandInput | ObjectMap<NativeAttributeValue>[] | string, PlainObject, UpdateCommandInput | string, BatchGetCommandInput | ScanCommandInput | NativeAttributeValue[]> {
|
|
31
|
+
source: "cloud";
|
|
32
|
+
service: "aws-v3";
|
|
33
|
+
key?: string | AttributeKey;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface AWSDatabaseCredential extends Omit<DynamoDBClientConfig, "credentials">, AWSBaseCredential {
|
|
37
|
+
translateConfig?: TranslateConfig;
|
|
38
|
+
}
|
|
39
|
+
|
|
41
40
|
export type AttributeKey = Record<string, NativeAttributeValue>;
|
package/upload/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
const path = require("path");
|
|
3
3
|
const fs = require("fs");
|
|
4
4
|
const stream = require("stream");
|
|
5
|
+
const crypto_1 = require("crypto");
|
|
5
6
|
const lib_storage_1 = require("@aws-sdk/lib-storage");
|
|
6
7
|
const Cloud = require("@e-mc/cloud");
|
|
7
8
|
const util_1 = require("@e-mc/cloud/util");
|
|
@@ -10,7 +11,7 @@ const aws_lib_1 = require("@pi-r/aws-lib");
|
|
|
10
11
|
const client_1 = require("@pi-r/aws-v3");
|
|
11
12
|
const BUCKET_SESSION = new Set();
|
|
12
13
|
const BUCKET_RESPONSE = {};
|
|
13
|
-
|
|
14
|
+
function upload(credential, service = "aws-v3", sdk = "@aws-sdk/client-s3") {
|
|
14
15
|
const [client, AWS] = client_1.createStorageClient.call(this, credential, service, sdk);
|
|
15
16
|
return async (data, callback) => {
|
|
16
17
|
const { bucket: Bucket, localUri } = data;
|
|
@@ -27,7 +28,11 @@ module.exports = function upload(credential, service = "aws-v3", sdk = "@aws-sdk
|
|
|
27
28
|
callback(err);
|
|
28
29
|
return false;
|
|
29
30
|
};
|
|
30
|
-
const addLog = (err) =>
|
|
31
|
+
const addLog = (err) => {
|
|
32
|
+
if (err instanceof Error) {
|
|
33
|
+
this.addLog(this.statusType.WARN, err, service, Bucket);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
31
36
|
const configBucket = admin.configBucket;
|
|
32
37
|
if (!BUCKET_SESSION.has(service + Bucket)) {
|
|
33
38
|
const bucketAcl = admin.publicRead ? "public-read" : admin.acl;
|
|
@@ -41,36 +46,56 @@ module.exports = function upload(credential, service = "aws-v3", sdk = "@aws-sdk
|
|
|
41
46
|
if (configBucket) {
|
|
42
47
|
const { cors: CORSConfiguration, lifecycle: LifecycleConfiguration, retentionPolicy: DefaultRetention } = configBucket;
|
|
43
48
|
const ExpectedBucketOwner = options?.ExpectedBucketOwner;
|
|
44
|
-
const commandMessage = (feature, message) =>
|
|
49
|
+
const commandMessage = (feature, message) => {
|
|
50
|
+
this.formatMessage(64, service, ["Bucket configured" + ` (${feature})`, message || Bucket], ExpectedBucketOwner, { ...Cloud[message === 'delete' ? 'LOG_CLOUD_WARN' : 'LOG_CLOUD_COMMAND'] });
|
|
51
|
+
};
|
|
45
52
|
if ((0, types_1.isPlainObject)(DefaultRetention)) {
|
|
46
53
|
client.send(new AWS.PutObjectLockConfigurationCommand({ Bucket, ObjectLockConfiguration: { ObjectLockEnabled: 'Enabled', Rule: { DefaultRetention } }, ExpectedBucketOwner, RequestPayer: options?.RequestPayer }))
|
|
47
54
|
.then(() => {
|
|
48
55
|
this.formatMessage(64, service, ["Bucket configured" + ' (Retention Policy)', Bucket], (0, aws_lib_1.formatDefaultRetention)(DefaultRetention), { ...Cloud.LOG_CLOUD_COMMAND });
|
|
49
56
|
})
|
|
50
|
-
.catch((err) =>
|
|
57
|
+
.catch((err) => {
|
|
58
|
+
addLog(err);
|
|
59
|
+
});
|
|
51
60
|
}
|
|
52
61
|
if (CORSConfiguration && Array.isArray(CORSConfiguration.CORSRules)) {
|
|
53
62
|
if (CORSConfiguration.CORSRules.length === 0) {
|
|
54
63
|
client.send(new AWS.DeleteBucketCorsCommand({ Bucket, ExpectedBucketOwner }))
|
|
55
|
-
.then(() =>
|
|
56
|
-
|
|
64
|
+
.then(() => {
|
|
65
|
+
commandMessage('CORS', 'delete');
|
|
66
|
+
})
|
|
67
|
+
.catch((err) => {
|
|
68
|
+
addLog(err);
|
|
69
|
+
});
|
|
57
70
|
}
|
|
58
71
|
else {
|
|
59
72
|
client.send(new AWS.PutBucketCorsCommand({ Bucket, CORSConfiguration, ExpectedBucketOwner }))
|
|
60
|
-
.then(() =>
|
|
61
|
-
|
|
73
|
+
.then(() => {
|
|
74
|
+
commandMessage('CORS');
|
|
75
|
+
})
|
|
76
|
+
.catch((err) => {
|
|
77
|
+
addLog(err);
|
|
78
|
+
});
|
|
62
79
|
}
|
|
63
80
|
}
|
|
64
81
|
if (LifecycleConfiguration && Array.isArray(LifecycleConfiguration.Rules)) {
|
|
65
82
|
if (LifecycleConfiguration.Rules.length === 0) {
|
|
66
83
|
client.send(new AWS.DeleteBucketLifecycleCommand({ Bucket, ExpectedBucketOwner }))
|
|
67
|
-
.then(() =>
|
|
68
|
-
|
|
84
|
+
.then(() => {
|
|
85
|
+
commandMessage('Lifecycle', 'delete');
|
|
86
|
+
})
|
|
87
|
+
.catch((err) => {
|
|
88
|
+
addLog(err);
|
|
89
|
+
});
|
|
69
90
|
}
|
|
70
91
|
else {
|
|
71
92
|
client.send(new AWS.PutBucketLifecycleConfigurationCommand({ Bucket, LifecycleConfiguration, ExpectedBucketOwner }))
|
|
72
|
-
.then(() =>
|
|
73
|
-
|
|
93
|
+
.then(() => {
|
|
94
|
+
commandMessage('Lifecycle');
|
|
95
|
+
})
|
|
96
|
+
.catch((err) => {
|
|
97
|
+
addLog(err);
|
|
98
|
+
});
|
|
74
99
|
}
|
|
75
100
|
}
|
|
76
101
|
}
|
|
@@ -89,7 +114,7 @@ module.exports = function upload(credential, service = "aws-v3", sdk = "@aws-sdk
|
|
|
89
114
|
.then(() => true)
|
|
90
115
|
.catch((err) => {
|
|
91
116
|
if (err instanceof Error && err.name !== 'NotFound') {
|
|
92
|
-
filename = (0,
|
|
117
|
+
filename = (0, crypto_1.randomUUID)() + path.extname(current);
|
|
93
118
|
return true;
|
|
94
119
|
}
|
|
95
120
|
return false;
|
|
@@ -113,7 +138,7 @@ module.exports = function upload(credential, service = "aws-v3", sdk = "@aws-sdk
|
|
|
113
138
|
partSize = (0, types_1.alignSize)(data.upload.chunkSize, 1024);
|
|
114
139
|
}
|
|
115
140
|
try {
|
|
116
|
-
Stream.push(data.buffer.length ? stream.Readable.from(data.buffer, { highWaterMark: getPartSize() }) : fs.createReadStream(localUri, { highWaterMark: getPartSize(), signal: this.signal }));
|
|
141
|
+
Stream.push(data.buffer.length > 0 ? stream.Readable.from(data.buffer, { highWaterMark: getPartSize() }) : fs.createReadStream(localUri, { highWaterMark: getPartSize(), signal: this.signal }));
|
|
117
142
|
if (fileGroup) {
|
|
118
143
|
const [key, body, type] = (0, util_1.createKeyAndBody)(filename, fileGroup, 0, addLog, 2);
|
|
119
144
|
Key.push(...key);
|
|
@@ -208,4 +233,5 @@ module.exports = function upload(credential, service = "aws-v3", sdk = "@aws-sdk
|
|
|
208
233
|
});
|
|
209
234
|
}
|
|
210
235
|
};
|
|
211
|
-
}
|
|
236
|
+
}
|
|
237
|
+
module.exports = upload;
|