Skip to content

Commit d90c94e

Browse files
committed
Feature remove port dependancy
1 parent f876301 commit d90c94e

File tree

3 files changed

+53
-27
lines changed

3 files changed

+53
-27
lines changed

README.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,11 @@ Create a `config/minio.config.ts` file:
7474
import { registerAs } from '@nestjs/config';
7575

7676
export default registerAs('minio', () => ({
77-
endPoint: process.env.MINIO_ENDPOINT || 'localhost',
77+
endPoint: process.env.MINIO_ENDPOINT || 'localhost:9000', // Format: "host" or "host:port"
7878
externalEndPoint:
7979
process.env.MINIO_EXTERNAL_ENDPOINT ||
8080
process.env.MINIO_ENDPOINT ||
81-
'localhost',
82-
port: parseInt(process.env.MINIO_PORT || '9000', 10),
81+
'localhost:9000', // Format: "host" or "host:port"
8382
useSSL: process.env.MINIO_USE_HTTPS === 'true',
8483
externalUseSSL: process.env.MINIO_EXTERNAL_ENDPOINT_USE_HTTPS === 'true',
8584
accessKey: process.env.MINIO_ACCESS_KEY || 'minioadmin',
@@ -98,8 +97,8 @@ export default registerAs('minio', () => ({
9897

9998
Create a `.env` file in your project root:
10099
```env
101-
MINIO_ENDPOINT=your-minio-endpoint
102-
MINIO_PORT=9000
100+
MINIO_ENDPOINT=minio:9000
101+
MINIO_EXTERNAL_ENDPOINT=minio.example.com:9000
103102
MINIO_USE_HTTPS=false
104103
MINIO_ACCESS_KEY=your-access-key
105104
MINIO_SECRET_KEY=your-secret-key
@@ -229,16 +228,15 @@ The module accepts the following configuration options:
229228
```typescript
230229
interface IMinioModuleOptions {
231230
// Required options
232-
endPoint: string; // MinIO server endpoint
233-
port: number; // MinIO server port
231+
endPoint: string; // MinIO server endpoint (format: "host" or "host:port", e.g., "minio:9000")
234232
useSSL: boolean; // Whether to use SSL for connection
235233
accessKey: string; // MinIO access key
236234
secretKey: string; // MinIO secret key
237235
urlExpiryHours: number; // Expiry time for signed URLs in hours
238236

239237
// Optional options
240238
region?: string; // MinIO region
241-
externalEndPoint?: string; // External endpoint for public access
239+
externalEndPoint?: string; // External endpoint for public access (format: "host" or "host:port")
242240
externalUseSSL?: boolean; // Whether to use SSL for external endpoint
243241

244242
// Bucket configuration
@@ -253,16 +251,15 @@ interface IMinioModuleOptions {
253251

254252
```typescript
255253
const minioConfig: IMinioModuleOptions = {
256-
endPoint: 'minio.example.com',
257-
port: 9000,
254+
endPoint: 'minio.example.com:9000', // Port included in endpoint
258255
useSSL: false,
259256
accessKey: 'your-access-key',
260257
secretKey: 'your-secret-key',
261258
urlExpiryHours: 2,
262259

263260
// Optional settings
264261
region: 'us-east-1',
265-
externalEndPoint: 'public.minio.example.com',
262+
externalEndPoint: 'public.minio.example.com:9000', // Port included in endpoint
266263
externalUseSSL: true,
267264

268265
// Bucket configuration
@@ -283,16 +280,15 @@ const minioConfig: IMinioModuleOptions = {
283280

284281
```env
285282
# Required settings
286-
MINIO_ENDPOINT=minio.example.com
287-
MINIO_PORT=9000
283+
MINIO_ENDPOINT=minio:9000
288284
MINIO_USE_HTTPS=false
289285
MINIO_ACCESS_KEY=your-access-key
290286
MINIO_SECRET_KEY=your-secret-key
291287
MINIO_URL_EXPIRY_HOURS=2
292288
293289
# Optional settings
294290
MINIO_REGION=us-east-1
295-
MINIO_EXTERNAL_ENDPOINT=public.minio.example.com
291+
MINIO_EXTERNAL_ENDPOINT=minio.example.com:9000
296292
MINIO_EXTERNAL_ENDPOINT_USE_HTTPS=true
297293
298294
# Bucket configuration

src/interfaces/minio-options.interface.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
export interface IMinioModuleOptions {
2-
endPoint: string;
3-
port: number;
2+
endPoint: string; // Format: "host" or "host:port" (e.g., "minio:9000")
43
useSSL: boolean;
54
accessKey: string;
65
secretKey: string;
76
region?: string;
8-
externalEndPoint?: string;
7+
externalEndPoint?: string; // Format: "host" or "host:port" (e.g., "minio.example.com:9000")
98
externalUseSSL?: boolean;
109
urlExpiryHours: number;
1110
buckets: {

src/minio.service.ts

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,40 @@ export class MinioService implements OnModuleInit {
1212
private bucketInitialized = false;
1313
constructor(@Inject(MINIO_CONFIG) private readonly config: IMinioModuleOptions) {}
1414

15+
/**
16+
* Parses an endpoint string to extract host and port
17+
* Supports formats: "host", "host:port", "host:port/path"
18+
*/
19+
private parseEndpoint(endpoint: string): { host: string; port?: number } {
20+
// Remove protocol if present
21+
const cleanEndpoint = endpoint.replace(/^https?:\/\//, '');
22+
23+
// Split by colon to get host and potential port
24+
const parts = cleanEndpoint.split(':');
25+
26+
if (parts.length === 1) {
27+
// No port specified, use default based on SSL
28+
return { host: parts[0] };
29+
}
30+
31+
// Extract port (may have path after it)
32+
const portPart = parts[1].split('/')[0];
33+
const port = parseInt(portPart, 10);
34+
35+
if (isNaN(port)) {
36+
// Invalid port, treat as hostname with colon
37+
return { host: cleanEndpoint };
38+
}
39+
40+
return { host: parts[0], port };
41+
}
42+
1543
async onModuleInit(): Promise<void> {
44+
const { host, port } = this.parseEndpoint(this.config.endPoint);
45+
1646
this.minioClient = new Minio.Client({
17-
endPoint: this.config.endPoint,
18-
port: this.config.port,
47+
endPoint: host,
48+
port: port,
1949
useSSL: this.config.useSSL,
2050
accessKey: this.config.accessKey,
2151
secretKey: this.config.secretKey,
@@ -130,11 +160,12 @@ export class MinioService implements OnModuleInit {
130160
const accessKey = this.config.accessKey;
131161
const secretKey = this.config.secretKey;
132162
const region = this.config.region || 'us-east-1';
133-
const externalHost = endPoint;
134-
const port = this.config.port;
135-
136-
// Use host without port for signing
137-
const signingHost = port ? `${externalHost}:${port}` : externalHost;
163+
164+
// Parse endpoint to get host and port
165+
const { host, port } = this.parseEndpoint(endPoint);
166+
167+
// Use host with port for signing (if port is specified)
168+
const signingHost = port ? `${host}:${port}` : host;
138169

139170
const currentDate = new Date();
140171
const amzDate = this.getAmzDate(currentDate);
@@ -215,11 +246,11 @@ export class MinioService implements OnModuleInit {
215246

216247
async getPresignedUrl(bucketName: string, objectName: string): Promise<string> {
217248
if (!this.config.buckets.private.includes(bucketName)) {
249+
// For public buckets, return direct URL
218250
const endpoint = this.config.externalEndPoint || this.config.endPoint;
219251
const protocol = (this.config.externalUseSSL ?? this.config.useSSL) ? 'https' : 'http';
220-
return `${protocol}://${endpoint}${
221-
this.config.port ? `:${this.config.port}` : ''
222-
}/${bucketName}/${objectName}`;
252+
// Endpoint already includes port if specified
253+
return `${protocol}://${endpoint}/${bucketName}/${objectName}`;
223254
}
224255

225256
return await this.calculatePresignedGetUrl(

0 commit comments

Comments
 (0)