Skip to content

Commit 1755075

Browse files
committed
fix: incorrect config load timing
1 parent d5341e0 commit 1755075

File tree

1 file changed

+64
-69
lines changed

1 file changed

+64
-69
lines changed

lib/s3/index.ts

Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,54 @@ import {
66
type IAwsS3CompatibleStorageOptions,
77
type ICosStorageOptions,
88
type IOssStorageOptions,
9-
type IStorage
9+
type IStorage,
10+
type IStorageOptions
1011
} from '@fastgpt-sdk/storage';
1112

12-
let config: any = {};
13-
let externalConfig: any = {};
13+
type StorageConfigWithoutBucket = Omit<IStorageOptions, 'bucket'>;
1414

15-
const { vendor, publicBucket, privateBucket, externalBaseUrl, credentials, region, ...options } =
16-
createDefaultStorageOptions();
15+
const getConfig = () => {
16+
const { vendor, externalBaseUrl, publicBucket, privateBucket, credentials, region, ...options } =
17+
createDefaultStorageOptions();
1718

18-
const setConfig = () => {
19-
if (vendor === 'minio') {
20-
config = {
19+
const buildResult = <T extends StorageConfigWithoutBucket>(config: T, externalConfig?: T) => ({
20+
vendor,
21+
config,
22+
externalConfig,
23+
externalBaseUrl,
24+
privateBucket,
25+
publicBucket
26+
});
27+
28+
if (vendor === 'minio' || vendor === 'aws-s3') {
29+
const config: Omit<IAwsS3CompatibleStorageOptions, 'bucket'> = {
2130
region,
2231
vendor,
2332
credentials,
24-
forcePathStyle: true,
33+
forcePathStyle: vendor === 'minio' ? true : options.forcePathStyle,
2534
endpoint: options.endpoint!,
2635
maxRetries: options.maxRetries!
27-
} as Omit<IAwsS3CompatibleStorageOptions, 'bucket'>;
28-
externalConfig = {
29-
...config,
30-
endpoint: externalBaseUrl
3136
};
32-
} else if (vendor === 'aws-s3') {
33-
config = {
34-
region,
35-
vendor,
36-
credentials,
37-
endpoint: options.endpoint!,
38-
maxRetries: options.maxRetries!,
39-
forcePathStyle: options.forcePathStyle
40-
} as Omit<IAwsS3CompatibleStorageOptions, 'bucket'>;
41-
externalConfig = {
42-
...config,
43-
endpoint: externalBaseUrl
44-
};
45-
} else if (vendor === 'cos') {
46-
config = {
37+
38+
return buildResult(config, { ...config, endpoint: externalBaseUrl });
39+
}
40+
41+
if (vendor === 'cos') {
42+
const config: Omit<ICosStorageOptions, 'bucket'> = {
4743
region,
4844
vendor,
4945
credentials,
5046
proxy: options.proxy,
5147
domain: options.domain,
5248
protocol: options.protocol,
5349
useAccelerate: options.useAccelerate
54-
} as Omit<ICosStorageOptions, 'bucket'>;
55-
} else if (vendor === 'oss') {
56-
config = {
50+
};
51+
52+
return buildResult(config);
53+
}
54+
55+
if (vendor === 'oss') {
56+
const config: Omit<IOssStorageOptions, 'bucket'> = {
5757
region,
5858
vendor,
5959
credentials,
@@ -62,60 +62,55 @@ const setConfig = () => {
6262
internal: options.internal,
6363
secure: options.secure,
6464
enableProxy: options.enableProxy
65-
} as Omit<IOssStorageOptions, 'bucket'>;
65+
};
66+
67+
return buildResult(config);
6668
}
67-
};
6869

69-
export const publicS3Server = (() => {
70-
if (!global._publicS3Server) {
71-
setConfig();
70+
throw new Error(`Not supported vendor: ${vendor}`);
71+
};
7272

73-
const client = createStorage({ bucket: publicBucket, ...config });
73+
const createS3Service = (bucket: string, isPublic: boolean) => {
74+
const { config, externalConfig, externalBaseUrl } = getConfig();
7475

75-
let externalClient: IStorage | undefined = undefined;
76-
if (externalBaseUrl) {
77-
externalClient = createStorage({ bucket: publicBucket, ...externalConfig });
78-
}
76+
const client = createStorage({ bucket, ...config } as IStorageOptions);
7977

80-
global._publicS3Server = new S3Service(client, externalClient);
78+
let externalClient: IStorage | undefined;
79+
if (externalBaseUrl && externalConfig) {
80+
externalClient = createStorage({ bucket, ...externalConfig } as IStorageOptions);
81+
}
8182

82-
client.ensureBucket();
83-
if (externalClient) {
84-
externalClient.ensureBucket();
83+
const ensurePublicPolicy = (storage: IStorage) => {
84+
if (storage instanceof MinioStorageAdapter) {
85+
storage.ensurePublicBucketPolicy();
8586
}
87+
};
88+
89+
client.ensureBucket().then(() => {
90+
if (isPublic) ensurePublicPolicy(client);
91+
});
8692

87-
client.ensureBucket().then(() => {
88-
if (vendor !== 'minio') return;
89-
(client as MinioStorageAdapter).ensurePublicBucketPolicy();
93+
if (externalClient) {
94+
externalClient.ensureBucket().then(() => {
95+
if (isPublic) ensurePublicPolicy(externalClient);
9096
});
97+
}
9198

92-
if (externalClient) {
93-
externalClient.ensureBucket().then(() => {
94-
if (vendor !== 'minio') return;
95-
(externalClient as MinioStorageAdapter).ensurePublicBucketPolicy();
96-
});
97-
}
99+
return new S3Service(client, externalClient);
100+
};
101+
102+
export const publicS3Server = (() => {
103+
if (!global._publicS3Server) {
104+
const { publicBucket } = getConfig();
105+
global._publicS3Server = createS3Service(publicBucket, true);
98106
}
99107
return global._publicS3Server;
100108
})();
101109

102110
export const privateS3Server = (() => {
103111
if (!global._privateS3Server) {
104-
setConfig();
105-
106-
const client = createStorage({ bucket: privateBucket, ...config });
107-
108-
let externalClient: IStorage | undefined = undefined;
109-
if (externalBaseUrl) {
110-
externalClient = createStorage({ bucket: privateBucket, ...externalConfig });
111-
}
112-
113-
global._privateS3Server = new S3Service(client, externalClient);
114-
115-
client.ensureBucket();
116-
if (externalClient) {
117-
externalClient.ensureBucket();
118-
}
112+
const { privateBucket } = getConfig();
113+
global._privateS3Server = createS3Service(privateBucket, false);
119114
}
120115
return global._privateS3Server;
121116
})();

0 commit comments

Comments
 (0)