@pi-r/aws 0.6.1 → 0.6.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 +2 -0
- package/client/index.d.ts +4 -2
- package/client/index.js +22 -14
- package/package.json +6 -6
- package/types/index.d.ts +7 -1
- package/upload/index.js +39 -36
package/README.md
CHANGED
package/client/index.d.ts
CHANGED
|
@@ -8,11 +8,13 @@ import type { ConfigurationOptions } from 'aws-sdk/lib/core';
|
|
|
8
8
|
import type { ServiceConfigurationOptions } from 'aws-sdk/lib/service';
|
|
9
9
|
import type { AttributeValue, DocumentClient } from 'aws-sdk/clients/dynamodb';
|
|
10
10
|
import type { DynamoDBClientConfig } from '@aws-sdk/client-dynamodb';
|
|
11
|
-
import type { CreateBucketRequest } from 'aws-sdk/clients/s3';
|
|
11
|
+
import type { CreateBucketRequest, DefaultRetention } from 'aws-sdk/clients/s3';
|
|
12
12
|
|
|
13
13
|
declare namespace AWS {
|
|
14
14
|
function isAccessDefined(credential: Pick<ConfigurationOptions, "accessKeyId" | "secretAccessKey" | "sessionToken">): boolean;
|
|
15
15
|
function isEnvDefined(): boolean;
|
|
16
|
+
function isSharedCredentialsDefined(): boolean;
|
|
17
|
+
function isProviderChainDefined(): boolean;
|
|
16
18
|
function isDatabaseDefined(credential: AWSDatabaseCredential, data: CloudDatabase): boolean;
|
|
17
19
|
function getPublicReadPolicy(bucket: string, authenticated?: boolean, write?: boolean): string;
|
|
18
20
|
function getBucketPublicReadPolicy(bucket: string): string;
|
|
@@ -31,7 +33,7 @@ declare namespace AWS {
|
|
|
31
33
|
function executeBatchQuery(this: ICloud, credential: AWSDatabaseCredential, batch: AWSDatabaseQuery[], sessionKey?: string): Promise<BatchQueryResult>;
|
|
32
34
|
function setDatabaseEndpoint(config: ServiceConfigurationOptions | DynamoDBClientConfig): void;
|
|
33
35
|
function checkBucketCannedACL(value: unknown): BucketCannedACL | undefined;
|
|
34
|
-
function writeMessageDefaultRetention(this: IModule, bucket: string, retention:
|
|
36
|
+
function writeMessageDefaultRetention(this: IModule, bucket: string, retention: DefaultRetention, service?: string): void;
|
|
35
37
|
function parseAttributeValue(value: unknown): AttributeValue;
|
|
36
38
|
function getBucketKey(credential: unknown, Bucket: string, acl: string | undefined, service: string, sdk: string): string;
|
|
37
39
|
function isNoSuchBucket(err: unknown): boolean;
|
package/client/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isNoSuchBucket = exports.getBucketKey = exports.parseAttributeValue = exports.writeMessageDefaultRetention = exports.checkBucketCannedACL = exports.setDatabaseEndpoint = exports.executeBatchQuery = exports.executeQuery = exports.deleteObjectsV2 = exports.deleteObjects = exports.setBucketWebsite = exports.setBucketPolicy = exports.createBucketV2 = exports.createBucket = exports.createDatabaseClient = exports.createStorageClient = exports.validateDatabase = exports.validateStorage = exports.getPrivatePolicy = exports.getBucketPublicReadPolicy = exports.getPublicReadPolicy = exports.isDatabaseDefined = exports.isEnvDefined = exports.isAccessDefined = void 0;
|
|
3
|
+
exports.isNoSuchBucket = exports.getBucketKey = exports.parseAttributeValue = exports.writeMessageDefaultRetention = exports.checkBucketCannedACL = exports.setDatabaseEndpoint = exports.executeBatchQuery = exports.executeQuery = exports.deleteObjectsV2 = exports.deleteObjects = exports.setBucketWebsite = exports.setBucketPolicy = exports.createBucketV2 = exports.createBucket = exports.createDatabaseClient = exports.createStorageClient = exports.validateDatabase = exports.validateStorage = exports.getPrivatePolicy = exports.getBucketPublicReadPolicy = exports.getPublicReadPolicy = exports.isDatabaseDefined = exports.isProviderChainDefined = exports.isEnvDefined = exports.isSharedCredentialsDefined = exports.isAccessDefined = void 0;
|
|
4
4
|
const aws = require("aws-sdk");
|
|
5
5
|
const types_1 = require("@e-mc/types");
|
|
6
6
|
const util_1 = require("@e-mc/cloud/util");
|
|
@@ -62,12 +62,20 @@ function isAccessDefined(credential) {
|
|
|
62
62
|
return !!(credential.accessKeyId && credential.secretAccessKey || credential.sessionToken);
|
|
63
63
|
}
|
|
64
64
|
exports.isAccessDefined = isAccessDefined;
|
|
65
|
+
function isSharedCredentialsDefined() {
|
|
66
|
+
return !!process.env.AWS_SDK_LOAD_CONFIG;
|
|
67
|
+
}
|
|
68
|
+
exports.isSharedCredentialsDefined = isSharedCredentialsDefined;
|
|
65
69
|
function isEnvDefined() {
|
|
66
|
-
return !!(process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY
|
|
70
|
+
return !!(process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY);
|
|
67
71
|
}
|
|
68
72
|
exports.isEnvDefined = isEnvDefined;
|
|
73
|
+
function isProviderChainDefined() {
|
|
74
|
+
return isSharedCredentialsDefined() || !!(process.env.AWS_WEB_IDENTITY_TOKEN_FILE || process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI || process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI);
|
|
75
|
+
}
|
|
76
|
+
exports.isProviderChainDefined = isProviderChainDefined;
|
|
69
77
|
function isDatabaseDefined(credential, data) {
|
|
70
|
-
return !!(data.table && (credential.region || credential.endpoint || process.env.AWS_DEFAULT_REGION));
|
|
78
|
+
return !!(data.table && (credential.region || credential.endpoint || process.env.AWS_DEFAULT_REGION || process.env.AWS_REGION));
|
|
71
79
|
}
|
|
72
80
|
exports.isDatabaseDefined = isDatabaseDefined;
|
|
73
81
|
function getPublicReadPolicy(bucket, authenticated, write) {
|
|
@@ -114,7 +122,7 @@ function getPrivatePolicy(bucket) {
|
|
|
114
122
|
}
|
|
115
123
|
exports.getPrivatePolicy = getPrivatePolicy;
|
|
116
124
|
function validateStorage(credential) {
|
|
117
|
-
return !!(isAccessDefined(credential) || isEnvDefined() || credential.fromPath || credential.profile ||
|
|
125
|
+
return !!(isAccessDefined(credential) || isEnvDefined() || credential.fromPath || credential.profile || isProviderChainDefined());
|
|
118
126
|
}
|
|
119
127
|
exports.validateStorage = validateStorage;
|
|
120
128
|
function validateDatabase(credential, data) {
|
|
@@ -130,7 +138,7 @@ function createStorageClient(credential, service = 'aws', sdk = 'aws-sdk/clients
|
|
|
130
138
|
client.config.loadFromPath(fromPath);
|
|
131
139
|
return client;
|
|
132
140
|
}
|
|
133
|
-
if (profile ||
|
|
141
|
+
if (profile || isSharedCredentialsDefined() && !isAccessDefined(credential) && !isEnvDefined()) {
|
|
134
142
|
credential = new aws.SharedIniFileCredentials({ profile });
|
|
135
143
|
}
|
|
136
144
|
return new aws.S3(credential);
|
|
@@ -149,7 +157,7 @@ function createDatabaseClient(credential) {
|
|
|
149
157
|
if (fromPath) {
|
|
150
158
|
aws.config.loadFromPath(fromPath);
|
|
151
159
|
}
|
|
152
|
-
else if (profile ||
|
|
160
|
+
else if (profile || isSharedCredentialsDefined() && !isAccessDefined(credential) && !isEnvDefined()) {
|
|
153
161
|
options = new aws.SharedIniFileCredentials({ profile });
|
|
154
162
|
}
|
|
155
163
|
else {
|
|
@@ -315,7 +323,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
315
323
|
continue;
|
|
316
324
|
}
|
|
317
325
|
}
|
|
318
|
-
const Key = (0, types_1.isPlainObject)(key) ? key : { [key]:
|
|
326
|
+
const Key = (0, types_1.isPlainObject)(key) ? key : { [key]: id };
|
|
319
327
|
const command = { TableName, Key };
|
|
320
328
|
client = createClient();
|
|
321
329
|
if (update) {
|
|
@@ -330,12 +338,12 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
330
338
|
if (TableName) {
|
|
331
339
|
query.TableName = TableName;
|
|
332
340
|
}
|
|
333
|
-
if (limit > 0) {
|
|
334
|
-
query.Limit = limit;
|
|
335
|
-
}
|
|
336
341
|
if (!TableName) {
|
|
337
342
|
throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
|
|
338
343
|
}
|
|
344
|
+
if (limit > 0) {
|
|
345
|
+
query.Limit = limit;
|
|
346
|
+
}
|
|
339
347
|
if (useCache && (rows = getCache(queryString = Module.asString(query, true)))) {
|
|
340
348
|
result[i] = rows;
|
|
341
349
|
continue;
|
|
@@ -394,9 +402,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
394
402
|
exports.executeBatchQuery = executeBatchQuery;
|
|
395
403
|
function setDatabaseEndpoint(config) {
|
|
396
404
|
let region = config.region;
|
|
397
|
-
config.endpoint || (config.endpoint = `https://dynamodb.${(0, types_1.isString)(region) ? region : process.env.AWS_DEFAULT_REGION || 'us-east-1'}.amazonaws.com`);
|
|
398
|
-
if (!region
|
|
399
|
-
region = (
|
|
405
|
+
const endpoint = config.endpoint || (config.endpoint = `https://dynamodb.${(0, types_1.isString)(region) ? region : process.env.AWS_DEFAULT_REGION || process.env.AWS_REGION || 'us-east-1'}.amazonaws.com`);
|
|
406
|
+
if (!region) {
|
|
407
|
+
region = ((0, types_1.isString)(endpoint) && /\bdynamodb\.([^.]+)\./i.exec(endpoint)?.[1] || process.env.AWS_DEFAULT_REGION || process.env.AWS_REGION)?.toLowerCase();
|
|
400
408
|
if (region && region !== 'us-east-1') {
|
|
401
409
|
config.region = region;
|
|
402
410
|
}
|
|
@@ -425,7 +433,7 @@ function writeMessageDefaultRetention(bucket, retention, service = 'aws') {
|
|
|
425
433
|
if (Mode) {
|
|
426
434
|
status.push(status.length ? `(${Mode})` : Mode);
|
|
427
435
|
}
|
|
428
|
-
this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket configured" /* VAL_CLOUD.CONFIGURE_BUCKET */ + ' (
|
|
436
|
+
this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket configured" /* VAL_CLOUD.CONFIGURE_BUCKET */ + ' (Retention Policy)', bucket], status.join(' '), { ...Cloud.LOG_CLOUD_COMMAND });
|
|
429
437
|
}
|
|
430
438
|
exports.writeMessageDefaultRetention = writeMessageDefaultRetention;
|
|
431
439
|
function parseAttributeValue(value) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/aws",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.3",
|
|
4
4
|
"description": "AWS V2 cloud functions for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"types": "client/index.d.ts",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "https://github.com/anpham6/pi-r.git",
|
|
12
|
+
"url": "git+https://github.com/anpham6/pi-r.git",
|
|
13
13
|
"directory": "src/cloud/aws"
|
|
14
14
|
},
|
|
15
15
|
"keywords": [
|
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@e-mc/cloud": "^0.8.
|
|
25
|
-
"@e-mc/module": "^0.8.
|
|
26
|
-
"@e-mc/types": "^0.8.
|
|
27
|
-
"aws-sdk": "^2.
|
|
24
|
+
"@e-mc/cloud": "^0.8.3",
|
|
25
|
+
"@e-mc/module": "^0.8.3",
|
|
26
|
+
"@e-mc/types": "^0.8.3",
|
|
27
|
+
"aws-sdk": "^2.1547.0"
|
|
28
28
|
}
|
|
29
29
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -3,12 +3,18 @@ import type { CloudDatabase } from '@e-mc/types/lib/cloud';
|
|
|
3
3
|
|
|
4
4
|
import type { ConfigurationOptions } from 'aws-sdk/lib/core';
|
|
5
5
|
import type { ServiceConfigurationOptions } from 'aws-sdk/lib/service';
|
|
6
|
-
import type {
|
|
6
|
+
import type { DocumentClient } from 'aws-sdk/clients/dynamodb';
|
|
7
7
|
import type { PutBucketAclRequest, PutBucketPolicyRequest, PutPublicAccessBlockRequest } from 'aws-sdk/clients/s3';
|
|
8
8
|
|
|
9
9
|
export type BucketCannedACL = "authenticated-read" | "private" | "public-read" | "public-read-write";
|
|
10
10
|
export type ObjectCannedACL = BucketCannedACL | "aws-exec-read" | "bucket-owner-full-control" | "bucket-owner-read";
|
|
11
11
|
export type ConfigureBucketOptions = PutBucketAclRequest | PutBucketPolicyRequest | PutPublicAccessBlockRequest;
|
|
12
|
+
export type Key = DocumentClient.Key;
|
|
13
|
+
export type GetItemInput = DocumentClient.GetItemInput;
|
|
14
|
+
export type QueryInput = DocumentClient.QueryInput;
|
|
15
|
+
export type UpdateItemInput = DocumentClient.UpdateItemInput;
|
|
16
|
+
export type ScanInput = DocumentClient.ScanInput;
|
|
17
|
+
export type BatchGetItemInput = DocumentClient.BatchGetItemInput;
|
|
12
18
|
|
|
13
19
|
export interface AWSStorageCredential extends ConfigurationOptions {
|
|
14
20
|
profile?: string;
|
package/upload/index.js
CHANGED
|
@@ -36,7 +36,7 @@ function upload(credential, service = 'aws', sdk = 'aws-sdk/clients/s3') {
|
|
|
36
36
|
}
|
|
37
37
|
BUCKET_SESSION.add(service + Bucket);
|
|
38
38
|
}
|
|
39
|
-
if (service !== 'oci') {
|
|
39
|
+
if (service !== 'ibm' && service !== 'oci') {
|
|
40
40
|
const DefaultRetention = configBucket?.retentionPolicy;
|
|
41
41
|
if ((0, types_1.isPlainObject)(DefaultRetention)) {
|
|
42
42
|
s3.putObjectLockConfiguration({ Bucket, ObjectLockConfiguration: { ObjectLockEnabled: 'Enabled', Rule: { DefaultRetention } }, ExpectedBucketOwner: options?.ExpectedBucketOwner, RequestPayer: options?.RequestPayer }, err => {
|
|
@@ -119,46 +119,49 @@ function upload(credential, service = 'aws', sdk = 'aws-sdk/clients/s3') {
|
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
s3.upload(params, (err, result) => {
|
|
122
|
-
if (
|
|
123
|
-
const url = endpoint ? Module.joinPath(endpoint, result.Key) : result.Location;
|
|
124
|
-
this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Upload success" /* VAL_CLOUD.UPLOAD_FILE */, url, { ...Cloud.LOG_CLOUD_UPLOAD });
|
|
122
|
+
if (err) {
|
|
125
123
|
if (first) {
|
|
126
|
-
|
|
127
|
-
if ((0, types_1.isPlainObject)(tags)) {
|
|
128
|
-
const TagSet = [];
|
|
129
|
-
for (const name in tags) {
|
|
130
|
-
TagSet.push({ Key: name, Value: tags[name] });
|
|
131
|
-
}
|
|
132
|
-
s3.putObjectTagging({ Bucket, Key: result.Key, Tagging: { TagSet }, ExpectedBucketOwner: params.ExpectedBucketOwner, RequestPayer: params.RequestPayer }, error => {
|
|
133
|
-
if (!error) {
|
|
134
|
-
this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags created" /* VAL_CLOUD.CREATE_TAG */, Bucket], result.Key, { ...Cloud.LOG_CLOUD_COMMAND });
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
addLog(error);
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
else if (tags === false) {
|
|
142
|
-
s3.deleteObjectTagging({ Bucket, Key: result.Key, ExpectedBucketOwner: params.ExpectedBucketOwner }, error => {
|
|
143
|
-
if (!error) {
|
|
144
|
-
this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags deleted" /* VAL_CLOUD.DELETE_TAG */, Bucket], result.Key, { ...Cloud.LOG_CLOUD_COMMAND });
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
addLog(error);
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
cleanup();
|
|
153
|
-
callback(null, url);
|
|
124
|
+
errorResponse(err);
|
|
154
125
|
}
|
|
126
|
+
else {
|
|
127
|
+
addLog(err);
|
|
128
|
+
}
|
|
129
|
+
return;
|
|
155
130
|
}
|
|
156
|
-
|
|
157
|
-
|
|
131
|
+
const url = endpoint ? Module.joinPath(endpoint, result.Key) : result.Location;
|
|
132
|
+
this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Upload success" /* VAL_CLOUD.UPLOAD_FILE */, url, { ...Cloud.LOG_CLOUD_UPLOAD });
|
|
133
|
+
if (!first) {
|
|
134
|
+
return;
|
|
158
135
|
}
|
|
159
|
-
|
|
160
|
-
|
|
136
|
+
if (service !== 'oci') {
|
|
137
|
+
let length = -1;
|
|
138
|
+
if ((0, types_1.isPlainObject)(tags) && (length = Object.keys(tags).length) > 0) {
|
|
139
|
+
const TagSet = [];
|
|
140
|
+
for (const name in tags) {
|
|
141
|
+
TagSet.push({ Key: name, Value: tags[name] });
|
|
142
|
+
}
|
|
143
|
+
s3.putObjectTagging({ Bucket, Key: result.Key, Tagging: { TagSet }, ExpectedBucketOwner: params.ExpectedBucketOwner, RequestPayer: params.RequestPayer }, error => {
|
|
144
|
+
if (!error) {
|
|
145
|
+
this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags created" /* VAL_CLOUD.CREATE_TAG */, Bucket], result.Key, { ...Cloud.LOG_CLOUD_COMMAND });
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
addLog(error);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
else if (tags === false || length === 0) {
|
|
153
|
+
s3.deleteObjectTagging({ Bucket, Key: result.Key, ExpectedBucketOwner: params.ExpectedBucketOwner }, error => {
|
|
154
|
+
if (!error) {
|
|
155
|
+
this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags deleted" /* VAL_CLOUD.DELETE_TAG */, Bucket], result.Key, { ...Cloud.LOG_CLOUD_COMMAND });
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
addLog(error);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
161
162
|
}
|
|
163
|
+
cleanup();
|
|
164
|
+
callback(null, url);
|
|
162
165
|
});
|
|
163
166
|
}
|
|
164
167
|
};
|