@pi-r/aws 0.3.6 → 0.3.7
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/client/index.js +39 -45
- package/download/index.js +1 -1
- package/package.json +1 -1
- package/upload/index.js +1 -1
package/client/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isNoSuchBucket = exports.getBucketKey = 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;
|
|
4
|
+
const aws = require("aws-sdk");
|
|
4
5
|
const types_1 = require("@e-mc/types");
|
|
5
6
|
const util_1 = require("@e-mc/cloud/util");
|
|
6
7
|
const Module = require("@e-mc/module");
|
|
@@ -123,20 +124,18 @@ exports.validateDatabase = validateDatabase;
|
|
|
123
124
|
function createStorageClient(credential, service = 'aws', sdk = 'aws-sdk/clients/s3') {
|
|
124
125
|
try {
|
|
125
126
|
if (service === 'aws') {
|
|
126
|
-
const
|
|
127
|
-
if (
|
|
128
|
-
const client = new S3();
|
|
129
|
-
client.config.loadFromPath(
|
|
127
|
+
const { profile, fromPath } = credential;
|
|
128
|
+
if (fromPath) {
|
|
129
|
+
const client = new aws.S3();
|
|
130
|
+
client.config.loadFromPath(fromPath);
|
|
130
131
|
return client;
|
|
131
132
|
}
|
|
132
|
-
if (
|
|
133
|
-
|
|
134
|
-
credential = new SharedIniFileCredentials({ profile: credential.profile });
|
|
133
|
+
if (profile || process.env.AWS_SDK_LOAD_CONFIG === '1' && !isAccessDefined(credential) && !isEnvDefined()) {
|
|
134
|
+
credential = new aws.SharedIniFileCredentials({ profile });
|
|
135
135
|
}
|
|
136
|
-
return new S3(credential);
|
|
136
|
+
return new aws.S3(credential);
|
|
137
137
|
}
|
|
138
|
-
|
|
139
|
-
return new S3(credential);
|
|
138
|
+
return new (require(sdk))(credential);
|
|
140
139
|
}
|
|
141
140
|
catch (err) {
|
|
142
141
|
this.checkPackage(err, sdk.split('/')[0], { passThrough: true });
|
|
@@ -145,26 +144,18 @@ function createStorageClient(credential, service = 'aws', sdk = 'aws-sdk/clients
|
|
|
145
144
|
}
|
|
146
145
|
exports.createStorageClient = createStorageClient;
|
|
147
146
|
function createDatabaseClient(credential) {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
AWS.config.loadFromPath(credential.fromPath);
|
|
153
|
-
}
|
|
154
|
-
else if (credential.profile || process.env.AWS_SDK_LOAD_CONFIG === '1' && !isAccessDefined(credential) && !isEnvDefined()) {
|
|
155
|
-
const { SharedIniFileCredentials } = require('aws-sdk');
|
|
156
|
-
options = new SharedIniFileCredentials({ profile: credential.profile });
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
options = credential;
|
|
160
|
-
}
|
|
161
|
-
const DynamoDB = require('aws-sdk/clients/dynamodb');
|
|
162
|
-
return new DynamoDB.DocumentClient(options);
|
|
147
|
+
const { profile, fromPath } = credential;
|
|
148
|
+
let options;
|
|
149
|
+
if (fromPath) {
|
|
150
|
+
aws.config.loadFromPath(fromPath);
|
|
163
151
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
152
|
+
else if (profile || process.env.AWS_SDK_LOAD_CONFIG === '1' && !isAccessDefined(credential) && !isEnvDefined()) {
|
|
153
|
+
options = new aws.SharedIniFileCredentials({ profile });
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
options = credential;
|
|
167
157
|
}
|
|
158
|
+
return new aws.DynamoDB.DocumentClient(options);
|
|
168
159
|
}
|
|
169
160
|
exports.createDatabaseClient = createDatabaseClient;
|
|
170
161
|
function createBucket(credential, Bucket, publicRead, service = 'aws', sdk = 'aws-sdk/clients/s3') {
|
|
@@ -184,8 +175,8 @@ function createBucketV2(credential, Bucket, ACL, options, service = 'aws', sdk =
|
|
|
184
175
|
.catch(() => {
|
|
185
176
|
const input = { ...options, Bucket };
|
|
186
177
|
const region = credential.region;
|
|
187
|
-
if (
|
|
188
|
-
input.CreateBucketConfiguration = { LocationConstraint: region };
|
|
178
|
+
if (region && (region !== 'us-east-1' || service !== 'aws')) {
|
|
179
|
+
input.CreateBucketConfiguration || (input.CreateBucketConfiguration = { LocationConstraint: region });
|
|
189
180
|
}
|
|
190
181
|
return S3.createBucket(input).promise()
|
|
191
182
|
.then(async () => {
|
|
@@ -235,11 +226,12 @@ exports.setBucketPolicy = setBucketPolicy;
|
|
|
235
226
|
function setBucketWebsite(credential, Bucket, options, service = 'aws', sdk = 'aws-sdk/clients/s3') {
|
|
236
227
|
const S3 = createStorageClient.call(this, credential, service, sdk);
|
|
237
228
|
const WebsiteConfiguration = {};
|
|
238
|
-
|
|
239
|
-
|
|
229
|
+
const { indexPage: Suffix, errorPage: Key } = options;
|
|
230
|
+
if ((0, types_1.isString)(Suffix)) {
|
|
231
|
+
WebsiteConfiguration.IndexDocument = { Suffix };
|
|
240
232
|
}
|
|
241
|
-
if ((0, types_1.isString)(
|
|
242
|
-
WebsiteConfiguration.ErrorDocument = { Key
|
|
233
|
+
if ((0, types_1.isString)(Key)) {
|
|
234
|
+
WebsiteConfiguration.ErrorDocument = { Key };
|
|
243
235
|
}
|
|
244
236
|
return S3.putBucketWebsite({ Bucket, WebsiteConfiguration }).promise()
|
|
245
237
|
.then(() => {
|
|
@@ -269,8 +261,9 @@ async function deleteObjectsV2(credential, Bucket, recursive = true, service = '
|
|
|
269
261
|
}
|
|
270
262
|
return S3.deleteObjects({ Bucket, Delete: { Objects } }).promise()
|
|
271
263
|
.then(data => {
|
|
272
|
-
|
|
273
|
-
|
|
264
|
+
const Deleted = data.Deleted;
|
|
265
|
+
if ((0, types_1.isArray)(Deleted)) {
|
|
266
|
+
const files = Deleted.length + ' files';
|
|
274
267
|
this.formatMessage(64, service, ["Bucket emptied" + ` (${recursive ? 'recursive' : files})`, Bucket], recursive ? files : '', { ...Cloud.LOG_CLOUD_COMMAND });
|
|
275
268
|
}
|
|
276
269
|
})
|
|
@@ -329,9 +322,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
329
322
|
if (update) {
|
|
330
323
|
await client.update({ ...command, ...update }).promise();
|
|
331
324
|
}
|
|
332
|
-
const
|
|
333
|
-
if (
|
|
334
|
-
rows = [
|
|
325
|
+
const Item = (await client.get(command).promise()).Item;
|
|
326
|
+
if (Item) {
|
|
327
|
+
rows = [Item];
|
|
335
328
|
}
|
|
336
329
|
}
|
|
337
330
|
else if ((0, types_1.isPlainObject)(query)) {
|
|
@@ -343,9 +336,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
343
336
|
if (limit > 0) {
|
|
344
337
|
query.Limit = limit;
|
|
345
338
|
}
|
|
346
|
-
const
|
|
347
|
-
if (
|
|
348
|
-
rows =
|
|
339
|
+
const { Count, Items } = await createClient().query(query).promise();
|
|
340
|
+
if (Count && Items) {
|
|
341
|
+
rows = Items;
|
|
349
342
|
}
|
|
350
343
|
}
|
|
351
344
|
else {
|
|
@@ -358,9 +351,10 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
358
351
|
exports.executeBatchQuery = executeBatchQuery;
|
|
359
352
|
function setDatabaseEndpoint(config) {
|
|
360
353
|
var _a, _b;
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
354
|
+
let region = config.region;
|
|
355
|
+
config.endpoint || (config.endpoint = `https://dynamodb.${(0, types_1.isString)(region) ? region : process.env.AWS_DEFAULT_REGION || 'us-east-1'}.amazonaws.com`);
|
|
356
|
+
if (!region && (0, types_1.isString)(config.endpoint)) {
|
|
357
|
+
region = (_b = (((_a = /dynamodb\.([^.]+)\./i.exec(config.endpoint)) === null || _a === void 0 ? void 0 : _a[1]) || process.env.AWS_DEFAULT_REGION)) === null || _b === void 0 ? void 0 : _b.toLowerCase();
|
|
364
358
|
if (region && region !== 'us-east-1') {
|
|
365
359
|
config.region = region;
|
|
366
360
|
}
|
package/download/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
const client_1 = require("@pi-r/aws");
|
|
3
2
|
const types_1 = require("@e-mc/types");
|
|
4
3
|
const Module = require("@e-mc/module");
|
|
5
4
|
const Cloud = require("@e-mc/cloud");
|
|
5
|
+
const client_1 = require("@pi-r/aws");
|
|
6
6
|
function download(credential, service = 'aws', sdk = 'aws-sdk/clients/s3') {
|
|
7
7
|
const s3 = client_1.createStorageClient.call(this, credential, service, sdk);
|
|
8
8
|
return (data, callback) => {
|
package/package.json
CHANGED
package/upload/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const path = require("path");
|
|
3
3
|
const fs = require("fs");
|
|
4
|
-
const client_1 = require("@pi-r/aws");
|
|
5
4
|
const util_1 = require("@e-mc/cloud/util");
|
|
6
5
|
const types_1 = require("@e-mc/types");
|
|
7
6
|
const Module = require("@e-mc/module");
|
|
8
7
|
const Cloud = require("@e-mc/cloud");
|
|
8
|
+
const client_1 = require("@pi-r/aws");
|
|
9
9
|
const BUCKET_SESSION = new Set();
|
|
10
10
|
const BUCKET_RESPONSE = {};
|
|
11
11
|
function upload(credential, service = 'aws', sdk = 'aws-sdk/clients/s3') {
|