Skip to content

Commit c0f2525

Browse files
authored
fix: bucket init (#196)
* fix: bucket init * initial bucket policy
1 parent 85a5cf9 commit c0f2525

File tree

2 files changed

+58
-32
lines changed

2 files changed

+58
-32
lines changed

src/s3/controller.ts

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class S3Service {
5151
: undefined;
5252
}
5353

54-
async initialize() {
54+
async initialize(policy: 'public' | 'private') {
5555
const [, err] = await catchError(async () => {
5656
addLog.info(`Checking bucket: ${this.config.bucket}`);
5757
const bucketExists = await this.client.bucketExists(this.config.bucket);
@@ -60,46 +60,70 @@ export class S3Service {
6060
addLog.info(`Creating bucket: ${this.config.bucket}`);
6161
const [, err] = await catchError(() => this.client.makeBucket(this.config.bucket));
6262
if (err) {
63-
addLog.warn(`Failed to create bucket: ${this.config.bucket}`);
64-
return Promise.reject(err);
63+
addLog.error(`Failed to create bucket: ${this.config.bucket}`);
64+
return;
6565
}
6666
}
6767

68-
if (this.config.retentionDays && this.config.retentionDays > 0) {
69-
const Days = this.config.retentionDays;
70-
const [, err] = await catchError(() =>
71-
Promise.all([
72-
this.client.setBucketPolicy(
73-
this.config.bucket,
74-
JSON.stringify({
75-
Version: '2012-10-17',
76-
Statement: [
77-
{
78-
Effect: 'Allow',
79-
Principal: '*',
80-
Action: ['s3:GetObject'],
81-
Resource: [`arn:aws:s3:::${this.config.bucket}/*`]
82-
}
83-
]
84-
})
85-
),
86-
this.client.setBucketLifecycle(this.config.bucket, {
87-
Rule: [
68+
const [_, err] = await catchError(async () => {
69+
if (policy === 'public') {
70+
return this.client.setBucketPolicy(
71+
this.config.bucket,
72+
JSON.stringify({
73+
Version: '2012-10-17',
74+
Statement: [
8875
{
89-
ID: 'AutoDeleteRule',
90-
Status: 'Enabled',
91-
Expiration: {
92-
Days,
93-
DeleteMarker: false,
94-
DeleteAll: false
95-
}
76+
Effect: 'Allow',
77+
Principal: '*',
78+
Action: ['s3:GetObject'],
79+
Resource: [`arn:aws:s3:::${this.config.bucket}/*`]
9680
}
9781
]
9882
})
99-
])
83+
);
84+
}
85+
if (policy === 'private') {
86+
return this.client.setBucketPolicy(
87+
this.config.bucket,
88+
JSON.stringify({
89+
Version: '2012-10-17',
90+
Statement: []
91+
})
92+
);
93+
}
94+
});
95+
if (err) {
96+
addLog.warn(`Failed to set bucket policy: ${this.config.bucket}`);
97+
}
98+
99+
// Update bucket lifecycle
100+
if (this.config.retentionDays && this.config.retentionDays > 0) {
101+
const Days = this.config.retentionDays;
102+
const [, err] = await catchError(() =>
103+
this.client.setBucketLifecycle(this.config.bucket, {
104+
Rule: [
105+
{
106+
ID: 'AutoDeleteRule',
107+
Status: 'Enabled',
108+
Expiration: {
109+
Days,
110+
DeleteMarker: false,
111+
DeleteAll: false
112+
}
113+
}
114+
]
115+
})
116+
);
117+
if (err) {
118+
addLog.warn(`Failed to set bucket lifecycle: ${this.config.bucket}`);
119+
}
120+
} else {
121+
// Remove bucket policy to make it private
122+
const [, err] = await catchError(() =>
123+
this.client.removeBucketLifecycle(this.config.bucket)
100124
);
101125
if (err) {
102-
addLog.warn(`Failed to set bucket policy: ${this.config.bucket}`);
126+
addLog.warn(`Failed to remove bucket lifecycle: ${this.config.bucket}`);
103127
}
104128
}
105129

src/s3/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const fileUploadS3Server = (() => {
1010
bucket: process.env.S3_TOOL_BUCKET || 'fastgpt-tool',
1111
externalBaseUrl: process.env.S3_EXTERNAL_BASE_URL
1212
});
13+
global._fileUploadS3Server.initialize('public');
1314
}
1415
return global._fileUploadS3Server;
1516
})();
@@ -21,6 +22,7 @@ export const pluginFileS3Server = (() => {
2122
bucket: process.env.S3_PLUGIN_BUCKET || 'fastgpt-plugin',
2223
externalBaseUrl: process.env.S3_EXTERNAL_BASE_URL
2324
});
25+
global._pluginFileS3Server.initialize('private');
2426
}
2527
return global._pluginFileS3Server;
2628
})();

0 commit comments

Comments
 (0)