mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-06 08:04:37 +00:00
feat(storage): support public s3 endpoint for browser uploads and update k8s example
This commit is contained in:
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|||||||
### Added
|
### 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
|
* **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
|
### Fixed
|
||||||
|
|
||||||
|
|||||||
@@ -197,15 +197,14 @@ In Kubernetes, services are created as internal `ClusterIP` services by default.
|
|||||||
|
|
||||||
Open **`http://localhost:3000`** in your browser.
|
Open **`http://localhost:3000`** in your browser.
|
||||||
|
|
||||||
2. **Enable Browser Uploads to Garage S3**:
|
2. **Forward In-Cluster S3 Storage (Garage)**:
|
||||||
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:
|
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
|
```bash
|
||||||
kubectl port-forward -n shumai svc/garage 3900:3900
|
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]
|
> [!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
|
### How Autoscaling Operates
|
||||||
|
|
||||||
|
|||||||
+6
-11
@@ -101,20 +101,15 @@ kubectl port-forward -n shumai svc/shumai-server 3000:3000
|
|||||||
```
|
```
|
||||||
Open your browser to **`http://localhost:3000`**.
|
Open your browser to **`http://localhost:3000`**.
|
||||||
|
|
||||||
#### Step B: Enable In-Cluster S3 Media Uploads
|
#### Step B: Port-forward In-Cluster S3 Storage (Garage)
|
||||||
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:
|
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
|
||||||
```bash
|
kubectl port-forward -n shumai svc/garage 3900:3900
|
||||||
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
|
|
||||||
```
|
|
||||||
|
|
||||||
> [!NOTE]
|
> [!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.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ spec:
|
|||||||
env:
|
env:
|
||||||
- name: SHUMAI_WORKERS
|
- name: SHUMAI_WORKERS
|
||||||
value: 'agent'
|
value: 'agent'
|
||||||
|
- name: AWS_PUBLIC_ENDPOINT_URL_S3
|
||||||
|
value: 'http://localhost:3900'
|
||||||
envFrom:
|
envFrom:
|
||||||
- configMapRef:
|
- configMapRef:
|
||||||
name: shumai-config
|
name: shumai-config
|
||||||
|
|||||||
@@ -351,7 +351,35 @@ describe('S3Service implementations', () => {
|
|||||||
|
|
||||||
const input = await s3.resolveInput('test-bucket', 'video.mp4')
|
const input = await s3.resolveInput('test-bucket', 'video.mp4')
|
||||||
expect(input).toBe('https://presigned.s3.url')
|
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 () => {
|
it('should stream in uploadFileToKey', async () => {
|
||||||
@@ -642,6 +670,16 @@ describe('S3Service implementations', () => {
|
|||||||
expect(url).toContain('http://123.456.7.8:12345')
|
expect(url).toContain('http://123.456.7.8:12345')
|
||||||
expect(url).not.toContain('4567')
|
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', () => {
|
describe('buildContentDisposition', () => {
|
||||||
|
|||||||
+33
-11
@@ -111,6 +111,7 @@ export interface S3Service {
|
|||||||
method: string,
|
method: string,
|
||||||
download?: boolean,
|
download?: boolean,
|
||||||
filename?: string,
|
filename?: string,
|
||||||
|
internal?: boolean,
|
||||||
) => Promise<string>
|
) => Promise<string>
|
||||||
presignMultipart: (
|
presignMultipart: (
|
||||||
bucket: string,
|
bucket: string,
|
||||||
@@ -123,6 +124,7 @@ export interface S3Service {
|
|||||||
|
|
||||||
export class S3StorageService implements S3Service {
|
export class S3StorageService implements S3Service {
|
||||||
private client: S3Client
|
private client: S3Client
|
||||||
|
private presignClient: S3Client
|
||||||
private bucket: string
|
private bucket: string
|
||||||
private presignCache = new LruTtlCache<string, string>(50000)
|
private presignCache = new LruTtlCache<string, string>(50000)
|
||||||
|
|
||||||
@@ -132,6 +134,7 @@ export class S3StorageService implements S3Service {
|
|||||||
secretAccessKey: string,
|
secretAccessKey: string,
|
||||||
bucket: string,
|
bucket: string,
|
||||||
region: string = 'auto',
|
region: string = 'auto',
|
||||||
|
publicEndpoint?: string,
|
||||||
) {
|
) {
|
||||||
this.client = new S3Client({
|
this.client = new S3Client({
|
||||||
region,
|
region,
|
||||||
@@ -144,6 +147,20 @@ export class S3StorageService implements S3Service {
|
|||||||
requestChecksumCalculation: 'WHEN_REQUIRED',
|
requestChecksumCalculation: 'WHEN_REQUIRED',
|
||||||
responseChecksumValidation: '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
|
this.bucket = bucket
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -342,12 +359,13 @@ export class S3StorageService implements S3Service {
|
|||||||
method: string,
|
method: string,
|
||||||
download?: boolean,
|
download?: boolean,
|
||||||
filename?: string,
|
filename?: string,
|
||||||
|
internal?: boolean,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
|
const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
|
||||||
const expiresInSeconds = expireHours * 3600
|
const expiresInSeconds = expireHours * 3600
|
||||||
const cacheTtlMs = expiresInSeconds * 1000
|
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) {
|
if (method === 'GET' && !download) {
|
||||||
const cached = this.presignCache.get(cacheKey)
|
const cached = this.presignCache.get(cacheKey)
|
||||||
if (cached) {
|
if (cached) {
|
||||||
@@ -355,6 +373,7 @@ export class S3StorageService implements S3Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const signingClient = internal ? this.client : this.presignClient
|
||||||
let url: string
|
let url: string
|
||||||
|
|
||||||
if (method === 'GET') {
|
if (method === 'GET') {
|
||||||
@@ -363,19 +382,19 @@ export class S3StorageService implements S3Service {
|
|||||||
Key: key,
|
Key: key,
|
||||||
ResponseContentDisposition: download ? buildContentDisposition(filename) : undefined,
|
ResponseContentDisposition: download ? buildContentDisposition(filename) : undefined,
|
||||||
})
|
})
|
||||||
url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds })
|
url = await getSignedUrl(signingClient, command, { expiresIn: expiresInSeconds })
|
||||||
} else if (method === 'PUT') {
|
} else if (method === 'PUT') {
|
||||||
const command = new PutObjectCommand({
|
const command = new PutObjectCommand({
|
||||||
Bucket: bucket,
|
Bucket: bucket,
|
||||||
Key: key,
|
Key: key,
|
||||||
})
|
})
|
||||||
url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds })
|
url = await getSignedUrl(signingClient, command, { expiresIn: expiresInSeconds })
|
||||||
} else if (method === 'DELETE') {
|
} else if (method === 'DELETE') {
|
||||||
const command = new DeleteObjectCommand({
|
const command = new DeleteObjectCommand({
|
||||||
Bucket: bucket,
|
Bucket: bucket,
|
||||||
Key: key,
|
Key: key,
|
||||||
})
|
})
|
||||||
url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds })
|
url = await getSignedUrl(signingClient, command, { expiresIn: expiresInSeconds })
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Unsupported method for presign: ${method}`)
|
throw new Error(`Unsupported method for presign: ${method}`)
|
||||||
}
|
}
|
||||||
@@ -405,13 +424,13 @@ export class S3StorageService implements S3Service {
|
|||||||
UploadId: request.uploadId,
|
UploadId: request.uploadId,
|
||||||
PartNumber: request.partNumber,
|
PartNumber: request.partNumber,
|
||||||
})
|
})
|
||||||
url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds })
|
url = await getSignedUrl(this.presignClient, command, { expiresIn: expiresInSeconds })
|
||||||
} else {
|
} else {
|
||||||
const command = new PutObjectCommand({
|
const command = new PutObjectCommand({
|
||||||
Bucket: bucket,
|
Bucket: bucket,
|
||||||
Key: key,
|
Key: key,
|
||||||
})
|
})
|
||||||
url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds })
|
url = await getSignedUrl(this.presignClient, command, { expiresIn: expiresInSeconds })
|
||||||
}
|
}
|
||||||
} else if (request.method === 'POST') {
|
} else if (request.method === 'POST') {
|
||||||
if (request.uploadId) {
|
if (request.uploadId) {
|
||||||
@@ -420,13 +439,13 @@ export class S3StorageService implements S3Service {
|
|||||||
Key: key,
|
Key: key,
|
||||||
UploadId: request.uploadId,
|
UploadId: request.uploadId,
|
||||||
})
|
})
|
||||||
url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds })
|
url = await getSignedUrl(this.presignClient, command, { expiresIn: expiresInSeconds })
|
||||||
} else {
|
} else {
|
||||||
const command = new CreateMultipartUploadCommand({
|
const command = new CreateMultipartUploadCommand({
|
||||||
Bucket: bucket,
|
Bucket: bucket,
|
||||||
Key: key,
|
Key: key,
|
||||||
})
|
})
|
||||||
url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds })
|
url = await getSignedUrl(this.presignClient, command, { expiresIn: expiresInSeconds })
|
||||||
}
|
}
|
||||||
} else if (request.method === 'GET') {
|
} else if (request.method === 'GET') {
|
||||||
if (!request.uploadId) {
|
if (!request.uploadId) {
|
||||||
@@ -437,7 +456,7 @@ export class S3StorageService implements S3Service {
|
|||||||
Key: key,
|
Key: key,
|
||||||
UploadId: request.uploadId,
|
UploadId: request.uploadId,
|
||||||
})
|
})
|
||||||
url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds })
|
url = await getSignedUrl(this.presignClient, command, { expiresIn: expiresInSeconds })
|
||||||
} else if (request.method === 'DELETE') {
|
} else if (request.method === 'DELETE') {
|
||||||
if (!request.uploadId) {
|
if (!request.uploadId) {
|
||||||
throw new Error('Abort multipart upload requires uploadId')
|
throw new Error('Abort multipart upload requires uploadId')
|
||||||
@@ -447,7 +466,7 @@ export class S3StorageService implements S3Service {
|
|||||||
Key: key,
|
Key: key,
|
||||||
UploadId: request.uploadId,
|
UploadId: request.uploadId,
|
||||||
})
|
})
|
||||||
url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds })
|
url = await getSignedUrl(this.presignClient, command, { expiresIn: expiresInSeconds })
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Unsupported method for presignMultipart: ${request.method}`)
|
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<string> {
|
async resolveInput(bucket: string, key: string): Promise<string> {
|
||||||
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 storageBackend = getStorageBackend()
|
||||||
const port = process.env.SHUMAI_SERVER_PORT || '3000'
|
const port = process.env.SHUMAI_SERVER_PORT || '3000'
|
||||||
const s3Endpoint = process.env.AWS_ENDPOINT_URL_S3 || `http://localhost:${port}`
|
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 =
|
export const s3Service: S3Service =
|
||||||
storageBackend === 's3'
|
storageBackend === 's3'
|
||||||
@@ -752,5 +773,6 @@ export const s3Service: S3Service =
|
|||||||
process.env.S3_SECRET_ACCESS_KEY || 'minioadmin',
|
process.env.S3_SECRET_ACCESS_KEY || 'minioadmin',
|
||||||
process.env.S3_BUCKET || 'shumai',
|
process.env.S3_BUCKET || 'shumai',
|
||||||
process.env.S3_REGION || 'auto',
|
process.env.S3_REGION || 'auto',
|
||||||
|
s3PublicEndpoint,
|
||||||
)
|
)
|
||||||
: new LocalStorageService(s3Endpoint)
|
: new LocalStorageService(s3Endpoint)
|
||||||
|
|||||||
Reference in New Issue
Block a user