Skip to content

Commit db356d6

Browse files
authored
remove auto remove policy (#246)
* add log * fix: upload file * remove auto remove policy
1 parent ab2d212 commit db356d6

File tree

4 files changed

+33
-59
lines changed

4 files changed

+33
-59
lines changed

.env.template

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ S3_ACCESS_KEY=minioadmin
2121
S3_SECRET_KEY=minioadmin
2222
S3_PUBLIC_BUCKET=fastgpt-public # 公开读私有写桶。用于系统工具,创建的临时文件,存储的桶。
2323
S3_PRIVATE_BUCKET=fastgpt-private # 私有读写桶。用于系统插件热安装文件的桶。
24-
RETENTION_DAYS=15 # 系统工具临时文件保存天数
2524

2625
# Signoz
2726
SIGNOZ_BASE_URL=

src/s3/config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { HttpsProxyAgent } from 'https-proxy-agent';
55

66
export type S3ConfigType = {
77
maxFileSize?: number; // 文件大小限制(字节)
8-
retentionDays?: number; // 保留天数(由 S3 生命周期策略自动管理)
9-
externalBaseUrl?: string; // 自定义域名
8+
externalBaseURL?: string; // 自定义域名
109
bucket: string; // 存储桶名称
1110
isPublicRead: boolean;
1211
} & ClientOptions;

src/s3/controller.ts

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,30 @@ export class S3Service {
3434
transportAgent: this.config.transportAgent
3535
});
3636

37-
this.externalClient = this.config.externalBaseUrl
38-
? (() => {
39-
const urlObj = new URL(this.config.externalBaseUrl);
40-
const endPoint = urlObj.hostname;
41-
const useSSL = urlObj.protocol === 'https';
42-
return new Minio.Client({
43-
endPoint,
44-
port: urlObj.port ? parseInt(urlObj.port) : useSSL ? 443 : 80,
45-
useSSL,
46-
accessKey: this.config.accessKey,
47-
secretKey: this.config.secretKey,
48-
transportAgent: this.config.transportAgent
49-
});
50-
})()
51-
: undefined;
37+
if (this.config.externalBaseURL) {
38+
const externalBaseURL = new URL(this.config.externalBaseURL);
39+
const endpoint = externalBaseURL.hostname;
40+
const useSSL = externalBaseURL.protocol === 'https:';
41+
42+
const externalPort = externalBaseURL.port
43+
? parseInt(externalBaseURL.port)
44+
: useSSL
45+
? 443
46+
: undefined; // https 默认 443,其他情况让 MinIO 客户端使用默认端口
47+
48+
this.externalClient = new Minio.Client({
49+
useSSL: useSSL,
50+
endPoint: endpoint,
51+
port: externalPort,
52+
accessKey: config.accessKey,
53+
secretKey: config.secretKey,
54+
transportAgent: config.transportAgent
55+
});
56+
}
5257
}
5358

5459
async initialize(policy: 'public' | 'private') {
60+
// Create bucket
5561
const [, err] = await catchError(async () => {
5662
addLog.info(`Checking bucket: ${this.config.bucket}`);
5763
const bucketExists = await this.client.bucketExists(this.config.bucket);
@@ -65,6 +71,7 @@ export class S3Service {
6571
}
6672
}
6773

74+
// Set bucket policy
6875
const [_, err] = await catchError(async () => {
6976
if (policy === 'public') {
7077
return this.client.setBucketPolicy(
@@ -96,37 +103,6 @@ export class S3Service {
96103
addLog.warn(`Failed to set bucket policy: ${this.config.bucket}`);
97104
}
98105

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)
124-
);
125-
if (err) {
126-
addLog.warn(`Failed to remove bucket lifecycle: ${this.config.bucket}`);
127-
}
128-
}
129-
130106
addLog.info(`Bucket initialized, ${this.config.bucket} configured successfully.`);
131107
});
132108
if (err) {
@@ -158,15 +134,15 @@ export class S3Service {
158134
* Get public readable URL
159135
*/
160136
async generateExternalUrl(objectName: string, expiry: number = 3600): Promise<string> {
161-
const externalBaseUrl = this.config.externalBaseUrl;
137+
const externalBaseURL = this.config.externalBaseURL;
162138

163139
// Private
164140
if (!this.config.isPublicRead) {
165141
const url = await this.client.presignedGetObject(this.config.bucket, objectName, expiry);
166142
// 如果有 externalBaseUrl,需要把域名进行替换
167-
if (this.config.externalBaseUrl) {
143+
if (this.config.externalBaseURL) {
168144
const urlObj = new URL(url);
169-
const externalUrlObj = new URL(this.config.externalBaseUrl);
145+
const externalUrlObj = new URL(this.config.externalBaseURL);
170146

171147
// 替换协议和域名,保留路径和查询参数
172148
urlObj.protocol = externalUrlObj.protocol;
@@ -179,8 +155,9 @@ export class S3Service {
179155
return url;
180156
}
181157

182-
if (externalBaseUrl) {
183-
return `${externalBaseUrl}/${this.config.bucket}/${objectName}`;
158+
// Public
159+
if (externalBaseURL) {
160+
return `${externalBaseURL}/${this.config.bucket}/${objectName}`;
184161
}
185162

186163
// Default url
@@ -343,8 +320,8 @@ export class S3Service {
343320

344321
const res = await client.presignedPostPolicy(policy);
345322
const postURL = (() => {
346-
if (this.config.externalBaseUrl) {
347-
return `${this.config.externalBaseUrl}/${this.config.bucket}`;
323+
if (this.config.externalBaseURL) {
324+
return `${this.config.externalBaseURL}/${this.config.bucket}`;
348325
} else {
349326
return res.postURL;
350327
}

src/s3/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ export const fileUploadS3Server = (() => {
66
maxFileSize: process.env.MAX_FILE_SIZE
77
? parseInt(process.env.MAX_FILE_SIZE)
88
: 20 * 1024 * 1024, // 默认 20MB
9-
retentionDays: process.env.RETENTION_DAYS ? parseInt(process.env.RETENTION_DAYS) : 15, // 默认保留15天
109
bucket: process.env.S3_PUBLIC_BUCKET || 'fastgpt-public',
11-
externalBaseUrl: process.env.S3_EXTERNAL_BASE_URL,
10+
externalBaseURL: process.env.S3_EXTERNAL_BASE_URL,
1211
isPublicRead: true
1312
});
1413
global._fileUploadS3Server.initialize('public');
@@ -21,7 +20,7 @@ export const pluginFileS3Server = (() => {
2120
global._pluginFileS3Server = new S3Service({
2221
maxFileSize: 50 * 1024 * 1024, // 默认 50MB
2322
bucket: process.env.S3_PRIVATE_BUCKET || 'fastgpt-private',
24-
externalBaseUrl: process.env.S3_EXTERNAL_BASE_URL,
23+
externalBaseURL: process.env.S3_EXTERNAL_BASE_URL,
2524
isPublicRead: false
2625
});
2726
global._pluginFileS3Server.initialize('private');

0 commit comments

Comments
 (0)