zyket 1.2.15 → 1.2.16
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/package.json +1 -1
- package/src/services/index.js +1 -1
- package/src/services/s3/index.js +52 -1
- package/src/utils/EnvManager.js +2 -0
package/package.json
CHANGED
package/src/services/index.js
CHANGED
|
@@ -22,7 +22,7 @@ module.exports = [
|
|
|
22
22
|
eventsActivated ? ["events", EventService, ["@service_container"]] : null,
|
|
23
23
|
databaseActivated ? ["database", Database, ["@service_container", process.env.DATABASE_URL]] : null,
|
|
24
24
|
["cache", Cache, ["@service_container", process.env.CACHE_URL || '']],
|
|
25
|
-
s3Activated ? ["s3", S3, ["@service_container", process.env.S3_ENDPOINT, process.env.S3_PORT, process.env.S3_USE_SSL === "true", process.env.S3_ACCESS_KEY, process.env.S3_SECRET_KEY]] : null,
|
|
25
|
+
s3Activated ? ["s3", S3, ["@service_container", process.env.S3_ENDPOINT, process.env.S3_PORT, process.env.S3_USE_SSL === "true", process.env.S3_ACCESS_KEY, process.env.S3_SECRET_KEY, process.env.S3_PUBLIC_BUCKETS, process.env.S3_PRIVATE_BUCKETS]] : null,
|
|
26
26
|
schedulerActivated ? ["scheduler", Scheduler, ["@service_container"]] : null,
|
|
27
27
|
bullmqActivated ? ["bullmq", require("./bullmq"), ["@service_container"]] : null,
|
|
28
28
|
socketActivated ? ["socketio", SocketIO, ["@service_container"]] : null,
|
package/src/services/s3/index.js
CHANGED
|
@@ -8,8 +8,10 @@ module.exports = class S3 extends Service {
|
|
|
8
8
|
#useSSL
|
|
9
9
|
#accessKey
|
|
10
10
|
#secretKey
|
|
11
|
+
#publicBuckets
|
|
12
|
+
#privateBuckets
|
|
11
13
|
|
|
12
|
-
constructor(container, endPoint, port, useSSL, accessKey, secretKey) {
|
|
14
|
+
constructor(container, endPoint, port, useSSL, accessKey, secretKey, publicBuckets, privateBuckets) {
|
|
13
15
|
super('s3')
|
|
14
16
|
this.#container = container
|
|
15
17
|
this.#endPoint = endPoint
|
|
@@ -17,6 +19,8 @@ module.exports = class S3 extends Service {
|
|
|
17
19
|
this.#useSSL = useSSL
|
|
18
20
|
this.#accessKey = accessKey
|
|
19
21
|
this.#secretKey = secretKey
|
|
22
|
+
this.#publicBuckets = this.#parseBuckets(publicBuckets)
|
|
23
|
+
this.#privateBuckets = this.#parseBuckets(privateBuckets)
|
|
20
24
|
}
|
|
21
25
|
|
|
22
26
|
async boot() {
|
|
@@ -27,6 +31,53 @@ module.exports = class S3 extends Service {
|
|
|
27
31
|
accessKey: this.#accessKey,
|
|
28
32
|
secretKey: this.#secretKey
|
|
29
33
|
})
|
|
34
|
+
|
|
35
|
+
await this.initBuckets()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
#parseBuckets(value) {
|
|
39
|
+
if (!value) return []
|
|
40
|
+
return value
|
|
41
|
+
.split(',')
|
|
42
|
+
.map((name) => name.trim())
|
|
43
|
+
.filter(Boolean)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async initBuckets() {
|
|
47
|
+
for (const bucketName of this.#privateBuckets) {
|
|
48
|
+
await this.ensureBucket(bucketName, false)
|
|
49
|
+
}
|
|
50
|
+
for (const bucketName of this.#publicBuckets) {
|
|
51
|
+
await this.ensureBucket(bucketName, true)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async ensureBucket(bucketName, isPublic = false) {
|
|
56
|
+
const exists = await this.client.bucketExists(bucketName)
|
|
57
|
+
if (!exists) {
|
|
58
|
+
this.#container.get('logger').info(`Creating ${isPublic ? 'public' : 'private'} bucket ${bucketName}`)
|
|
59
|
+
await this.client.makeBucket(bucketName, 'us-east-1')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (isPublic) {
|
|
63
|
+
await this.setBucketPublic(bucketName)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async setBucketPublic(bucketName) {
|
|
68
|
+
const policy = {
|
|
69
|
+
Version: '2012-10-17',
|
|
70
|
+
Statement: [
|
|
71
|
+
{
|
|
72
|
+
Effect: 'Allow',
|
|
73
|
+
Principal: { AWS: ['*'] },
|
|
74
|
+
Action: ['s3:GetObject'],
|
|
75
|
+
Resource: [`arn:aws:s3:::${bucketName}/*`],
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
}
|
|
79
|
+
this.#container.get('logger').debug(`Setting public read policy on bucket ${bucketName}`)
|
|
80
|
+
return this.client.setBucketPolicy(bucketName, JSON.stringify(policy))
|
|
30
81
|
}
|
|
31
82
|
|
|
32
83
|
async saveFile(bucketName, fileName, file, contentType = 'binary/octet-stream') {
|
package/src/utils/EnvManager.js
CHANGED