@@ -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 }
0 commit comments