diff --git a/CHANGELOG.md b/CHANGELOG.md index 04a4e329..62df5979 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Added * **deploy**: Add dedicated `shumaione/shumai-transcode` Docker image and a complete Kubernetes example featuring horizontal pod autoscaling (1 to 5 replicas) for media processing with in-cluster Garage S3 storage +* **storage**: Support configuring a separate public endpoint (`AWS_PUBLIC_ENDPOINT_URL_S3`) for browser-facing presigned URLs while preserving internal cluster endpoints for backend workers ### Fixed diff --git a/docs/configuration/distributed.mdx b/docs/configuration/distributed.mdx index 5f3f940c..b3d50f28 100644 --- a/docs/configuration/distributed.mdx +++ b/docs/configuration/distributed.mdx @@ -197,15 +197,14 @@ In Kubernetes, services are created as internal `ClusterIP` services by default. Open **`http://localhost:3000`** in your browser. -2. **Enable Browser Uploads to Garage S3**: - Shumai uses direct client-to-S3 presigned URLs for uploading media. To allow your local browser to upload directly to the in-cluster Garage S3 service: +2. **Forward In-Cluster S3 Storage (Garage)**: + Shumai uses direct client-to-S3 presigned URLs for uploading and streaming media. `shumai-server` is configured with `AWS_PUBLIC_ENDPOINT_URL_S3: http://localhost:3900`. Forward port 3900 to allow your local browser to upload directly to Garage S3: ```bash kubectl port-forward -n shumai svc/garage 3900:3900 - echo "127.0.0.1 garage.shumai.svc.cluster.local" | sudo tee -a /etc/hosts ``` > [!NOTE] -> In production environments, configure an Ingress controller (e.g. `ingress-nginx` or AWS ALB) with a public domain or set `AWS_ENDPOINT_URL_S3` to an external cloud S3/R2 bucket so port-forwarding and `/etc/hosts` mappings are not required. +> In production environments, configure an Ingress controller (e.g. `ingress-nginx` or AWS ALB) with a public domain or set `AWS_ENDPOINT_URL_S3` to an external cloud S3/R2 bucket. ### How Autoscaling Operates diff --git a/k8s/README.md b/k8s/README.md index f48059f1..7c7e1db2 100644 --- a/k8s/README.md +++ b/k8s/README.md @@ -101,20 +101,15 @@ kubectl port-forward -n shumai svc/shumai-server 3000:3000 ``` Open your browser to **`http://localhost:3000`**. -#### Step B: Enable In-Cluster S3 Media Uploads -Shumai uses direct client-to-S3 pre-signed URLs for high-throughput uploads. For your local browser to upload directly to the in-cluster Garage S3 service: +#### Step B: Port-forward In-Cluster S3 Storage (Garage) +Shumai generates direct client-to-S3 presigned URLs configured via `AWS_PUBLIC_ENDPOINT_URL_S3` (`http://localhost:3900`). To allow your browser to upload and stream media files directly: -1. Forward the Garage S3 API port (3900): - ```bash - kubectl port-forward -n shumai svc/garage 3900:3900 - ``` -2. Map the cluster S3 domain name to `127.0.0.1` in your local `/etc/hosts`: - ```bash - echo "127.0.0.1 garage.shumai.svc.cluster.local" | sudo tee -a /etc/hosts - ``` +```bash +kubectl port-forward -n shumai svc/garage 3900:3900 +``` > [!NOTE] -> **Production Environments**: In production cloud clusters (AWS EKS, GKE, self-hosted), you would configure an Ingress controller (e.g. `ingress-nginx` or AWS ALB) with a public domain or point `AWS_ENDPOINT_URL_S3` to an external cloud S3/R2 bucket so port-forwarding and `/etc/hosts` mappings are not required. +> **Production Environments**: In production cloud clusters (AWS EKS, GKE, self-hosted), you would configure an Ingress controller (e.g. `ingress-nginx` or AWS ALB) with a public domain or point `AWS_ENDPOINT_URL_S3` to an external cloud S3/R2 bucket. --- diff --git a/k8s/shumai-server.yaml b/k8s/shumai-server.yaml index 8ce90fdd..6780872e 100644 --- a/k8s/shumai-server.yaml +++ b/k8s/shumai-server.yaml @@ -38,6 +38,8 @@ spec: env: - name: SHUMAI_WORKERS value: 'agent' + - name: AWS_PUBLIC_ENDPOINT_URL_S3 + value: 'http://localhost:3900' envFrom: - configMapRef: name: shumai-config diff --git a/packages/core/src/s3/s3.test.ts b/packages/core/src/s3/s3.test.ts index aa07762c..165b5d55 100644 --- a/packages/core/src/s3/s3.test.ts +++ b/packages/core/src/s3/s3.test.ts @@ -351,7 +351,35 @@ describe('S3Service implementations', () => { const input = await s3.resolveInput('test-bucket', 'video.mp4') expect(input).toBe('https://presigned.s3.url') - expect(presignSpy).toHaveBeenCalledWith('test-bucket', 'video.mp4', 'GET') + expect(presignSpy).toHaveBeenCalledWith( + 'test-bucket', + 'video.mp4', + 'GET', + false, + undefined, + true, + ) + }) + + it('should support separate publicEndpoint for presigning while resolveInput uses internal client', async () => { + const s3 = new S3StorageService( + 'http://internal-s3:9000', + 'key', + 'secret', + 'test-bucket', + 'us-east-1', + 'https://public.example.com', + ) + + getSignedUrlSpy.mockClear() + await s3.presign('test-bucket', 'asset.mp4', 'GET') + const presignClientCall = getSignedUrlSpy.mock.calls[0][0] + + getSignedUrlSpy.mockClear() + await s3.resolveInput('test-bucket', 'asset.mp4') + const resolveClientCall = getSignedUrlSpy.mock.calls[0][0] + + expect(presignClientCall).not.toBe(resolveClientCall) }) it('should stream in uploadFileToKey', async () => { @@ -642,6 +670,16 @@ describe('S3Service implementations', () => { expect(url).toContain('http://123.456.7.8:12345') expect(url).not.toContain('4567') }) + + it('should initialize S3StorageService with AWS_PUBLIC_ENDPOINT_URL_S3 when set', async () => { + process.env.STORAGE_BACKEND = 's3' + process.env.AWS_ENDPOINT_URL_S3 = 'http://garage.shumai.svc.cluster.local:3900' + process.env.AWS_PUBLIC_ENDPOINT_URL_S3 = 'http://localhost:3900' + + vi.resetModules() + const { s3Service } = await import('./s3') + expect(s3Service).toBeDefined() + }) }) describe('buildContentDisposition', () => { diff --git a/packages/core/src/s3/s3.ts b/packages/core/src/s3/s3.ts index 7a4ae1ad..bdb5ca6a 100644 --- a/packages/core/src/s3/s3.ts +++ b/packages/core/src/s3/s3.ts @@ -111,6 +111,7 @@ export interface S3Service { method: string, download?: boolean, filename?: string, + internal?: boolean, ) => Promise presignMultipart: ( bucket: string, @@ -123,6 +124,7 @@ export interface S3Service { export class S3StorageService implements S3Service { private client: S3Client + private presignClient: S3Client private bucket: string private presignCache = new LruTtlCache(50000) @@ -132,6 +134,7 @@ export class S3StorageService implements S3Service { secretAccessKey: string, bucket: string, region: string = 'auto', + publicEndpoint?: string, ) { this.client = new S3Client({ region, @@ -144,6 +147,20 @@ export class S3StorageService implements S3Service { requestChecksumCalculation: 'WHEN_REQUIRED', responseChecksumValidation: 'WHEN_REQUIRED', }) + this.presignClient = + publicEndpoint && publicEndpoint !== endpoint + ? new S3Client({ + region, + endpoint: publicEndpoint, + credentials: { + accessKeyId, + secretAccessKey, + }, + forcePathStyle: true, + requestChecksumCalculation: 'WHEN_REQUIRED', + responseChecksumValidation: 'WHEN_REQUIRED', + }) + : this.client this.bucket = bucket } @@ -342,12 +359,13 @@ export class S3StorageService implements S3Service { method: string, download?: boolean, filename?: string, + internal?: boolean, ): Promise { const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10) const expiresInSeconds = expireHours * 3600 const cacheTtlMs = expiresInSeconds * 1000 - const cacheKey = `${bucket}/${key}?download=${download || false}&filename=${filename || ''}` + const cacheKey = `${bucket}/${key}?download=${download || false}&filename=${filename || ''}&internal=${internal || false}` if (method === 'GET' && !download) { const cached = this.presignCache.get(cacheKey) if (cached) { @@ -355,6 +373,7 @@ export class S3StorageService implements S3Service { } } + const signingClient = internal ? this.client : this.presignClient let url: string if (method === 'GET') { @@ -363,19 +382,19 @@ export class S3StorageService implements S3Service { Key: key, ResponseContentDisposition: download ? buildContentDisposition(filename) : undefined, }) - url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds }) + url = await getSignedUrl(signingClient, command, { expiresIn: expiresInSeconds }) } else if (method === 'PUT') { const command = new PutObjectCommand({ Bucket: bucket, Key: key, }) - url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds }) + url = await getSignedUrl(signingClient, command, { expiresIn: expiresInSeconds }) } else if (method === 'DELETE') { const command = new DeleteObjectCommand({ Bucket: bucket, Key: key, }) - url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds }) + url = await getSignedUrl(signingClient, command, { expiresIn: expiresInSeconds }) } else { throw new Error(`Unsupported method for presign: ${method}`) } @@ -405,13 +424,13 @@ export class S3StorageService implements S3Service { UploadId: request.uploadId, PartNumber: request.partNumber, }) - url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds }) + url = await getSignedUrl(this.presignClient, command, { expiresIn: expiresInSeconds }) } else { const command = new PutObjectCommand({ Bucket: bucket, Key: key, }) - url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds }) + url = await getSignedUrl(this.presignClient, command, { expiresIn: expiresInSeconds }) } } else if (request.method === 'POST') { if (request.uploadId) { @@ -420,13 +439,13 @@ export class S3StorageService implements S3Service { Key: key, UploadId: request.uploadId, }) - url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds }) + url = await getSignedUrl(this.presignClient, command, { expiresIn: expiresInSeconds }) } else { const command = new CreateMultipartUploadCommand({ Bucket: bucket, Key: key, }) - url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds }) + url = await getSignedUrl(this.presignClient, command, { expiresIn: expiresInSeconds }) } } else if (request.method === 'GET') { if (!request.uploadId) { @@ -437,7 +456,7 @@ export class S3StorageService implements S3Service { Key: key, UploadId: request.uploadId, }) - url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds }) + url = await getSignedUrl(this.presignClient, command, { expiresIn: expiresInSeconds }) } else if (request.method === 'DELETE') { if (!request.uploadId) { throw new Error('Abort multipart upload requires uploadId') @@ -447,7 +466,7 @@ export class S3StorageService implements S3Service { Key: key, UploadId: request.uploadId, }) - url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds }) + url = await getSignedUrl(this.presignClient, command, { expiresIn: expiresInSeconds }) } else { throw new Error(`Unsupported method for presignMultipart: ${request.method}`) } @@ -466,7 +485,7 @@ export class S3StorageService implements S3Service { } async resolveInput(bucket: string, key: string): Promise { - return this.presign(bucket, key, 'GET') + return this.presign(bucket, key, 'GET', false, undefined, true) } } @@ -743,6 +762,8 @@ export function getStorageBackend(): 's3' | 'local' { const storageBackend = getStorageBackend() const port = process.env.SHUMAI_SERVER_PORT || '3000' const s3Endpoint = process.env.AWS_ENDPOINT_URL_S3 || `http://localhost:${port}` +const s3PublicEndpoint = + process.env.AWS_PUBLIC_ENDPOINT_URL_S3 || process.env.S3_PUBLIC_ENDPOINT || undefined export const s3Service: S3Service = storageBackend === 's3' @@ -752,5 +773,6 @@ export const s3Service: S3Service = process.env.S3_SECRET_ACCESS_KEY || 'minioadmin', process.env.S3_BUCKET || 'shumai', process.env.S3_REGION || 'auto', + s3PublicEndpoint, ) : new LocalStorageService(s3Endpoint)