@twin.org/blob-storage-connector-aws-s3 0.0.1-next.10 → 0.0.1-next.13

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.
@@ -3,6 +3,7 @@
3
3
  var clientS3 = require('@aws-sdk/client-s3');
4
4
  var core = require('@twin.org/core');
5
5
  var crypto = require('@twin.org/crypto');
6
+ var loggingModels = require('@twin.org/logging-models');
6
7
 
7
8
  // Copyright 2024 IOTA Stiftung.
8
9
  // SPDX-License-Identifier: Apache-2.0.
@@ -52,6 +53,61 @@ class S3BlobStorageConnector {
52
53
  forcePathStyle: true
53
54
  });
54
55
  }
56
+ /**
57
+ * Bootstrap the component by creating and initializing any resources it needs.
58
+ * @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
59
+ * @returns True if the bootstrapping process was successful.
60
+ */
61
+ async bootstrap(nodeLoggingConnectorType) {
62
+ const nodeLogging = loggingModels.LoggingConnectorFactory.getIfExists(nodeLoggingConnectorType ?? "node-logging");
63
+ try {
64
+ await nodeLogging?.log({
65
+ level: "info",
66
+ source: this.CLASS_NAME,
67
+ message: "bucketCreating",
68
+ data: {
69
+ bucket: this._config.bucketName
70
+ }
71
+ });
72
+ const listBucketsCommand = new clientS3.ListBucketsCommand({});
73
+ const bucketsList = await this._s3Client.send(listBucketsCommand);
74
+ const bucketExists = bucketsList.Buckets?.some(bucket => bucket.Name === this._config.bucketName);
75
+ if (bucketExists) {
76
+ await nodeLogging?.log({
77
+ level: "info",
78
+ source: this.CLASS_NAME,
79
+ message: "bucketExists",
80
+ data: {
81
+ bucket: this._config.bucketName
82
+ }
83
+ });
84
+ }
85
+ else {
86
+ await this._s3Client.send(new clientS3.CreateBucketCommand({ Bucket: this._config.bucketName }));
87
+ await nodeLogging?.log({
88
+ level: "info",
89
+ source: this.CLASS_NAME,
90
+ message: "bucketCreated",
91
+ data: {
92
+ bucket: this._config.bucketName
93
+ }
94
+ });
95
+ }
96
+ }
97
+ catch (err) {
98
+ await nodeLogging?.log({
99
+ level: "error",
100
+ source: this.CLASS_NAME,
101
+ message: "bucketCreateFailed",
102
+ data: {
103
+ bucket: this._config.bucketName
104
+ },
105
+ error: core.BaseError.fromError(err)
106
+ });
107
+ return false;
108
+ }
109
+ return true;
110
+ }
55
111
  /**
56
112
  * Set the blob.
57
113
  * @param blob The data for the blob.
@@ -1,6 +1,7 @@
1
- import { S3Client, PutObjectCommand, GetObjectCommand, HeadObjectCommand, DeleteObjectCommand } from '@aws-sdk/client-s3';
2
- import { Guards, Converter, Urn, GeneralError, BaseError } from '@twin.org/core';
1
+ import { S3Client, ListBucketsCommand, CreateBucketCommand, PutObjectCommand, GetObjectCommand, HeadObjectCommand, DeleteObjectCommand } from '@aws-sdk/client-s3';
2
+ import { Guards, BaseError, Converter, Urn, GeneralError } from '@twin.org/core';
3
3
  import { Sha256 } from '@twin.org/crypto';
4
+ import { LoggingConnectorFactory } from '@twin.org/logging-models';
4
5
 
5
6
  // Copyright 2024 IOTA Stiftung.
6
7
  // SPDX-License-Identifier: Apache-2.0.
@@ -50,6 +51,61 @@ class S3BlobStorageConnector {
50
51
  forcePathStyle: true
51
52
  });
52
53
  }
54
+ /**
55
+ * Bootstrap the component by creating and initializing any resources it needs.
56
+ * @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
57
+ * @returns True if the bootstrapping process was successful.
58
+ */
59
+ async bootstrap(nodeLoggingConnectorType) {
60
+ const nodeLogging = LoggingConnectorFactory.getIfExists(nodeLoggingConnectorType ?? "node-logging");
61
+ try {
62
+ await nodeLogging?.log({
63
+ level: "info",
64
+ source: this.CLASS_NAME,
65
+ message: "bucketCreating",
66
+ data: {
67
+ bucket: this._config.bucketName
68
+ }
69
+ });
70
+ const listBucketsCommand = new ListBucketsCommand({});
71
+ const bucketsList = await this._s3Client.send(listBucketsCommand);
72
+ const bucketExists = bucketsList.Buckets?.some(bucket => bucket.Name === this._config.bucketName);
73
+ if (bucketExists) {
74
+ await nodeLogging?.log({
75
+ level: "info",
76
+ source: this.CLASS_NAME,
77
+ message: "bucketExists",
78
+ data: {
79
+ bucket: this._config.bucketName
80
+ }
81
+ });
82
+ }
83
+ else {
84
+ await this._s3Client.send(new CreateBucketCommand({ Bucket: this._config.bucketName }));
85
+ await nodeLogging?.log({
86
+ level: "info",
87
+ source: this.CLASS_NAME,
88
+ message: "bucketCreated",
89
+ data: {
90
+ bucket: this._config.bucketName
91
+ }
92
+ });
93
+ }
94
+ }
95
+ catch (err) {
96
+ await nodeLogging?.log({
97
+ level: "error",
98
+ source: this.CLASS_NAME,
99
+ message: "bucketCreateFailed",
100
+ data: {
101
+ bucket: this._config.bucketName
102
+ },
103
+ error: BaseError.fromError(err)
104
+ });
105
+ return false;
106
+ }
107
+ return true;
108
+ }
53
109
  /**
54
110
  * Set the blob.
55
111
  * @param blob The data for the blob.
@@ -21,6 +21,12 @@ export declare class S3BlobStorageConnector implements IBlobStorageConnector {
21
21
  constructor(options: {
22
22
  config: IS3BlobStorageConnectorConfig;
23
23
  });
24
+ /**
25
+ * Bootstrap the component by creating and initializing any resources it needs.
26
+ * @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
27
+ * @returns True if the bootstrapping process was successful.
28
+ */
29
+ bootstrap(nodeLoggingConnectorType?: string): Promise<boolean>;
24
30
  /**
25
31
  * Set the blob.
26
32
  * @param blob The data for the blob.
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/blob-storage-connector-aws-s3 - Changelog
2
2
 
3
- ## v0.0.1-next.10
3
+ ## v0.0.1-next.13
4
4
 
5
5
  - Initial Release
@@ -51,6 +51,30 @@ Runtime name for the class.
51
51
 
52
52
  ## Methods
53
53
 
54
+ ### bootstrap()
55
+
56
+ > **bootstrap**(`nodeLoggingConnectorType`?): `Promise`\<`boolean`\>
57
+
58
+ Bootstrap the component by creating and initializing any resources it needs.
59
+
60
+ #### Parameters
61
+
62
+ • **nodeLoggingConnectorType?**: `string`
63
+
64
+ The node logging connector type, defaults to "node-logging".
65
+
66
+ #### Returns
67
+
68
+ `Promise`\<`boolean`\>
69
+
70
+ True if the bootstrapping process was successful.
71
+
72
+ #### Implementation of
73
+
74
+ `IBlobStorageConnector.bootstrap`
75
+
76
+ ***
77
+
54
78
  ### set()
55
79
 
56
80
  > **set**(`blob`): `Promise`\<`string`\>
package/locales/en.json CHANGED
@@ -1,6 +1,14 @@
1
1
  {
2
+ "info": {
3
+ "s3BlobStorageConnector": {
4
+ "bucketCreating": "Creating bucket \"{bucket}\"",
5
+ "bucketCreated": "Created bucket \"{bucket}\"",
6
+ "bucketExists": "Skipping create bucket \"{bucket}\" as it already exists"
7
+ }
8
+ },
2
9
  "error": {
3
10
  "s3BlobStorageConnector": {
11
+ "bucketCreateFailed": "Creating bucket \"{bucket}\" failed",
4
12
  "namespaceMismatch": "The namespace in the urn \"{id}\" does not match the namespace of the blob storage \"{namespace}\"",
5
13
  "getBlobFailed": "Failed to get blob in S3",
6
14
  "setBlobFailed": "Failed to set blob in S3",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/blob-storage-connector-aws-s3",
3
- "version": "0.0.1-next.10",
3
+ "version": "0.0.1-next.13",
4
4
  "description": "Blob Storage connector implementation using AWS S3",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,9 +15,10 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@aws-sdk/client-s3": "^3.667.0",
18
- "@twin.org/blob-storage-models": "0.0.1-next.10",
18
+ "@twin.org/blob-storage-models": "0.0.1-next.13",
19
19
  "@twin.org/core": "next",
20
20
  "@twin.org/crypto": "next",
21
+ "@twin.org/logging-models": "next",
21
22
  "@twin.org/nameof": "next"
22
23
  },
23
24
  "main": "./dist/cjs/index.cjs",