@iyio/aws-s3 0.0.10

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 ADDED
@@ -0,0 +1,11 @@
1
+ # aws-s3
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build aws-s3` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test aws-s3` to execute the unit tests via [Jest](https://jestjs.io).
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@iyio/aws-s3",
3
+ "version": "0.0.10",
4
+ "type": "commonjs",
5
+ "main": "./src/index.js",
6
+ "types": "./src/index.d.ts",
7
+ "dependencies": {},
8
+ "peerDependencies": {
9
+ "@iyio/aws-credential-providers": "0.0.10",
10
+ "@iyio/aws": "0.0.10",
11
+ "@iyio/common": "0.0.10",
12
+ "rxjs": "^7.5.7",
13
+ "uuid": "^9.0.0",
14
+ "@aws-sdk/credential-providers": "^3.199.0",
15
+ "amazon-cognito-identity-js": "^5.2.12",
16
+ "@aws-sdk/client-s3": "^3.200.0",
17
+ "tslib": "^2.3.0"
18
+ }
19
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './lib/S3Store';
package/src/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./lib/S3Store"), exports);
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/aws-s3/src/index.ts"],"names":[],"mappings":";;;AAAA,wDAA8B"}
@@ -0,0 +1,16 @@
1
+ import { S3Client, S3ClientConfig } from "@aws-sdk/client-s3";
2
+ import { BaseStore, CancelToken, Scope } from "@iyio/common";
3
+ export interface S3StoreConfig {
4
+ bucket: string;
5
+ }
6
+ export declare class S3Store<T = any> extends BaseStore<T> {
7
+ static fromScope<T = any>(scope: Scope, config: S3StoreConfig): S3Store<T>;
8
+ readonly bucket: string;
9
+ readonly clientConfig: Readonly<S3ClientConfig>;
10
+ constructor(clientConfig: S3ClientConfig, { bucket }: S3StoreConfig);
11
+ private client;
12
+ getClient(): S3Client;
13
+ getAsync(key: string, cancel?: CancelToken): Promise<T | undefined>;
14
+ putAsync(key: string, value: T): Promise<void>;
15
+ deleteAsync(key: string): Promise<boolean | undefined>;
16
+ }
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.S3Store = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const client_s3_1 = require("@aws-sdk/client-s3");
6
+ const aws_1 = require("@iyio/aws");
7
+ const common_1 = require("@iyio/common");
8
+ class S3Store extends common_1.BaseStore {
9
+ constructor(clientConfig, { bucket }) {
10
+ super();
11
+ this.client = null;
12
+ this.bucket = bucket;
13
+ this.clientConfig = clientConfig;
14
+ }
15
+ static fromScope(scope, config) {
16
+ var _a;
17
+ return new S3Store({
18
+ region: (0, aws_1.awsRegionParam)(scope),
19
+ credentials: (_a = scope.get(aws_1.IAwsAuthType)) === null || _a === void 0 ? void 0 : _a.getAuthProvider()
20
+ }, config);
21
+ }
22
+ getClient() {
23
+ if (this.client) {
24
+ return this.client;
25
+ }
26
+ this.client = new client_s3_1.S3Client(this.clientConfig);
27
+ return this.client;
28
+ }
29
+ getAsync(key, cancel) {
30
+ var _a, _b, _c, _d;
31
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
32
+ let r;
33
+ try {
34
+ r = yield this.getClient().send(new client_s3_1.GetObjectCommand({
35
+ Key: key,
36
+ Bucket: this.bucket,
37
+ }));
38
+ }
39
+ catch (ex) {
40
+ if ((ex === null || ex === void 0 ? void 0 : ex.Code) === 'NoSuchKey') {
41
+ return undefined;
42
+ }
43
+ throw ex;
44
+ }
45
+ if (cancel === null || cancel === void 0 ? void 0 : cancel.isCanceled) {
46
+ return;
47
+ }
48
+ const contentType = (_b = (_a = r.ContentType) === null || _a === void 0 ? void 0 : _a.toLocaleLowerCase()) !== null && _b !== void 0 ? _b : '';
49
+ if (contentType === 'application/json' || contentType.startsWith('text/')) {
50
+ if (contentType.endsWith('/json')) {
51
+ if (r.Body) {
52
+ return JSON.parse(yield r.Body.transformToString());
53
+ }
54
+ else {
55
+ return undefined;
56
+ }
57
+ }
58
+ else {
59
+ return (_c = r.Body) === null || _c === void 0 ? void 0 : _c.toString();
60
+ }
61
+ }
62
+ else {
63
+ if (r.Body) {
64
+ return new common_1.BinaryStoreValue((_d = r.ContentType) !== null && _d !== void 0 ? _d : 'application/octet-stream', yield r.Body.transformToByteArray());
65
+ }
66
+ else {
67
+ return undefined;
68
+ }
69
+ }
70
+ });
71
+ }
72
+ putAsync(key, value) {
73
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
74
+ yield this.getClient().send((value instanceof common_1.BinaryStoreValue) ?
75
+ new client_s3_1.PutObjectCommand({
76
+ Key: key,
77
+ Bucket: this.bucket,
78
+ Body: value.content,
79
+ ContentType: value.contentType,
80
+ })
81
+ :
82
+ new client_s3_1.PutObjectCommand({
83
+ Key: key,
84
+ Bucket: this.bucket,
85
+ Body: JSON.stringify(value),
86
+ ContentType: 'application/json',
87
+ }));
88
+ });
89
+ }
90
+ deleteAsync(key) {
91
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
92
+ yield this.getClient().send(new client_s3_1.DeleteObjectCommand({
93
+ Key: key,
94
+ Bucket: this.bucket,
95
+ }));
96
+ return true;
97
+ });
98
+ }
99
+ }
100
+ exports.S3Store = S3Store;
101
+ //# sourceMappingURL=S3Store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"S3Store.js","sourceRoot":"","sources":["../../../../../packages/aws-s3/src/lib/S3Store.ts"],"names":[],"mappings":";;;;AAAA,kDAA+I;AAC/I,mCAAyD;AACzD,yCAA+E;AAO/E,MAAa,OAAe,SAAQ,kBAAY;IA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