From efd61632383da3d67dfa8c3ebb94f7ddb62f6336 Mon Sep 17 00:00:00 2001 From: yiling Date: Thu, 25 Jun 2026 18:46:14 +0800 Subject: [PATCH] refactor(s3): remove automatic port appending to AWS_ENDPOINT_URL_S3 --- README.md | 7 +++-- docker-compose/local/docker-compose.yaml | 2 +- docs/configuration/env-variables.mdx | 20 +++++++------- docs/getting-started/quickstart.mdx | 2 +- packages/core/src/s3/s3.test.ts | 35 ++++++++++++++++++++++++ packages/core/src/s3/s3.ts | 4 +-- 6 files changed, 53 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 931f96ba..47e68389 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Docker Compose is the fastest way to get Shumai running. You do not need to clon curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose.yaml ``` 3. **Configure Environment Variables (Remote Deployments only):** - If deploying Shumai to a remote server (e.g., AWS EC2, VPS), edit docker-compose.yaml and add AWS_ENDPOINT_URL_S3 under the environment section, setting it to your server's public IP address or domain name (e.g., http://12.34.56.78). + If deploying Shumai to a remote server (e.g., AWS EC2, VPS), edit docker-compose.yaml and set `AWS_ENDPOINT_URL_S3` under the environment section to your server's public IP address or domain name along with the mapped port (e.g., `http://12.34.56.78:3000`). 4. Start the services in detached mode: ```bash docker compose up -d @@ -126,11 +126,12 @@ DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema BETTER_AUTH_SECRET=ySxs7DxzHDZBbeeHNPEwBuspYwipBqz5Gk5XdBjNhWw= STORAGE_BACKEND=local SHUMAI_SERVER_PORT=3000 -AWS_ENDPOINT_URL_S3=http://localhost +AWS_ENDPOINT_URL_S3=http://localhost:3000 ``` > [!NOTE] -> If deploying on a remote server, update `AWS_ENDPOINT_URL_S3` from `http://localhost` to your server's public IP address or domain name. +> `SHUMAI_SERVER_PORT` sets the port the server starts on, while `AWS_ENDPOINT_URL_S3` is used by the browser to build file upload URLs. +> If deploying on a remote server (e.g. `http://123.456.7.8`) with a mapped port (e.g. `12345:3000` in docker-compose), set `AWS_ENDPOINT_URL_S3` to `http://123.456.7.8:12345`. #### Step 6: Run Shumai diff --git a/docker-compose/local/docker-compose.yaml b/docker-compose/local/docker-compose.yaml index c1fb901e..e96a232e 100644 --- a/docker-compose/local/docker-compose.yaml +++ b/docker-compose/local/docker-compose.yaml @@ -29,7 +29,7 @@ services: # S3 Configuration STORAGE_BACKEND: local - AWS_ENDPOINT_URL_S3: http://localhost + AWS_ENDPOINT_URL_S3: http://localhost:3000 # Better Auth BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw= diff --git a/docs/configuration/env-variables.mdx b/docs/configuration/env-variables.mdx index 705e60b2..ae40b1a8 100644 --- a/docs/configuration/env-variables.mdx +++ b/docs/configuration/env-variables.mdx @@ -37,16 +37,16 @@ Shumai is configured via environment variables. You can define these variables i Shumai supports storing files locally or on S3-compatible endpoints. -| Variable | Description | Example / Default | -| :------------------------- | :------------------------------------------------------------------------------------------------ | :--------------------------- | -| `STORAGE_BACKEND` | Storage type. Use `local` for local file system storage, or `s3` for S3-compatible cloud storage. | `local` (default) or `s3` | -| `AWS_ENDPOINT_URL_S3` | Custom endpoint for S3 storage. Must change to public domain/IP if deploying remotely. | `http://localhost` (default) | -| `S3_BUCKET` | S3 bucket name. | `shumai` (default) | -| `S3_ACCESS_KEY_ID` | Access key credentials for S3. | `minioadmin` (default) | -| `S3_SECRET_ACCESS_KEY` | Secret key credentials for S3. | `minioadmin` (default) | -| `S3_REGION` | S3 storage region. | `auto` (default) | -| `S3_USE_SSL` | Set to `true` to use HTTPS for S3 endpoints. | `false` (default) | -| `PRESIGNED_URL_EXPIRES_IN` | Number of hours for which generated presigned download/upload links are valid. | `5` (default) | +| Variable | Description | Example / Default | +| :------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------ | +| `STORAGE_BACKEND` | Storage type. Use `local` for local file system storage, or `s3` for S3-compatible cloud storage. | `local` (default) or `s3` | +| `AWS_ENDPOINT_URL_S3` | Endpoint for S3 storage or local storage. Used by the browser to build file upload URLs. Must include port if deploying remotely or using custom ports. | `http://localhost:3000` (respects `SHUMAI_SERVER_PORT` fallback if unset) | +| `S3_BUCKET` | S3 bucket name. | `shumai` (default) | +| `S3_ACCESS_KEY_ID` | Access key credentials for S3. | `minioadmin` (default) | +| `S3_SECRET_ACCESS_KEY` | Secret key credentials for S3. | `minioadmin` (default) | +| `S3_REGION` | S3 storage region. | `auto` (default) | +| `S3_USE_SSL` | Set to `true` to use HTTPS for S3 endpoints. | `false` (default) | +| `PRESIGNED_URL_EXPIRES_IN` | Number of hours for which generated presigned download/upload links are valid. | `5` (default) | --- diff --git a/docs/getting-started/quickstart.mdx b/docs/getting-started/quickstart.mdx index a15e548d..cbfac153 100644 --- a/docs/getting-started/quickstart.mdx +++ b/docs/getting-started/quickstart.mdx @@ -69,7 +69,7 @@ To handle media processing and security isolation, the host machine must have th DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public BETTER_AUTH_SECRET=your-random-32-character-secret-key STORAGE_BACKEND=local - AWS_ENDPOINT_URL_S3=http://localhost + AWS_ENDPOINT_URL_S3=http://localhost:3000 ``` 4. **Start Shumai**: diff --git a/packages/core/src/s3/s3.test.ts b/packages/core/src/s3/s3.test.ts index 5d1255df..cd6404ff 100644 --- a/packages/core/src/s3/s3.test.ts +++ b/packages/core/src/s3/s3.test.ts @@ -191,4 +191,39 @@ describe('S3Service implementations', () => { await expect(localS3.presign('b', 'k', 'POST')).rejects.toThrow() }) }) + + describe('s3Service initialization', () => { + let originalEnv: NodeJS.ProcessEnv + + beforeEach(() => { + originalEnv = { ...process.env } + }) + + afterEach(() => { + process.env = originalEnv + }) + + it('should initialize LocalStorageService with default localhost and SHUMAI_SERVER_PORT when AWS_ENDPOINT_URL_S3 is not set', async () => { + process.env.STORAGE_BACKEND = 'local' + delete process.env.AWS_ENDPOINT_URL_S3 + process.env.SHUMAI_SERVER_PORT = '4567' + + vi.resetModules() + const { s3Service } = await import('./s3') + const url = await s3Service.presign('bucket', 'key', 'GET') + expect(url).toContain('http://localhost:4567') + }) + + it('should initialize LocalStorageService with exact AWS_ENDPOINT_URL_S3 when it is set', async () => { + process.env.STORAGE_BACKEND = 'local' + process.env.AWS_ENDPOINT_URL_S3 = 'http://123.456.7.8:12345' + process.env.SHUMAI_SERVER_PORT = '4567' + + vi.resetModules() + const { s3Service } = await import('./s3') + const url = await s3Service.presign('bucket', 'key', 'GET') + expect(url).toContain('http://123.456.7.8:12345') + expect(url).not.toContain('4567') + }) + }) }) diff --git a/packages/core/src/s3/s3.ts b/packages/core/src/s3/s3.ts index f1081b08..669d4ff7 100644 --- a/packages/core/src/s3/s3.ts +++ b/packages/core/src/s3/s3.ts @@ -452,7 +452,7 @@ export class LocalStorageService implements S3Service { const storageBackend = process.env.STORAGE_BACKEND || 'local' const port = process.env.SHUMAI_SERVER_PORT || '3000' -const s3Endpoint = process.env.AWS_ENDPOINT_URL_S3 || 'http://localhost' +const s3Endpoint = process.env.AWS_ENDPOINT_URL_S3 || `http://localhost:${port}` export const s3Service: S3Service = storageBackend === 's3' @@ -464,4 +464,4 @@ export const s3Service: S3Service = process.env.S3_USE_SSL === 'true', process.env.S3_REGION || 'auto', ) - : new LocalStorageService(`${s3Endpoint}:${port}`) + : new LocalStorageService(s3Endpoint)