Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GCP support for object storage #140

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ HUGGINGFACE_TOKEN="hf_xxx"
LLAMA_CLOUD_API_KEY="llx-xxx"

GOOGLE_GENAI_API_KEY="XXX"

GCP_PROJECT_ID="your-gcp-project-id"
GCP_BUCKET_NAME="your-gcp-bucket-name"
GCP_KEY_FILENAME="path-to-your-gcp-keyfile.json"
46 changes: 46 additions & 0 deletions src/lib/server/object-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { env } from '$env/dynamic/private';
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
import rfc2047 from 'rfc2047';
import { v4 as uuidv4 } from 'uuid';
import { Storage } from '@google-cloud/storage';

const CLOUDFLARE_ACCOUNT_ID = env.CLOUDFLARE_ACCOUNT_ID;
const CLOUDFLARE_R2_BUCKET = env.CLOUDFLARE_R2_BUCKET;
Expand All @@ -19,6 +20,12 @@ const client = new S3Client({
}
});

const storage = new Storage({
projectId: env.GCP_PROJECT_ID,
keyFilename: env.GCP_KEY_FILENAME
});
const bucket = storage.bucket(env.GCP_BUCKET_NAME);

const EXT = {
'audio/wav': 'wav',
'audio/mpeg': 'mp3',
Expand Down Expand Up @@ -49,6 +56,10 @@ export async function upload_object(
metadata[k] = rfc2047.encode(v);
}

if (env.USE_GCP === 'true') {
return upload_object_gcp(object, type, metadata);
}

const command = new PutObjectCommand({
Bucket: CLOUDFLARE_R2_BUCKET,
Key: key,
Expand All @@ -62,3 +73,38 @@ export async function upload_object(
console.log(`Uploaded object to ${url}`);
return url;
}

export async function upload_object_gcp(
object: Buffer,
type: keyof typeof EXT,
metadata: Record<string, string> = {}
): Promise<string> {
const ext = EXT[type];
if (!ext) {
throw new Error('Invalid file type');
}

const key = `${uuidv4()}.${ext}`;

const file = bucket.file(key);
const stream = file.createWriteStream({
metadata: {
contentType: type,
metadata: metadata
}
});

return new Promise((resolve, reject) => {
stream.on('error', (err) => {
reject(err);
});

stream.on('finish', () => {
const url = `https://storage.googleapis.com/${env.GCP_BUCKET_NAME}/${key}`;
console.log(`Uploaded object to ${url}`);
resolve(url);
});

stream.end(object);
});
}
6 changes: 4 additions & 2 deletions src/routes/api/stt/+server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { json, type RequestHandler } from '@sveltejs/kit';

import { upload_object } from '$lib/server/object-storage';
import { upload_object, upload_object_gcp } from '$lib/server/object-storage';
import { transcribe } from '$lib/stt/gemini';

// curl -X POST http://localhost:5173/api/stt -H "Content-Type: multipart/form-data" -H "Origin: http://localhost:5173" -F "file=@test.wav"
Expand All @@ -25,7 +25,9 @@ export const POST: RequestHandler = async ({ request }) => {
}

const transcription = await transcribe(audio_buffer);
const url = await upload_object(audio_buffer, 'audio/mpeg', { transcription });
const url = process.env.USE_GCP === 'true'
? await upload_object_gcp(audio_buffer, 'audio/mpeg', { transcription })
: await upload_object(audio_buffer, 'audio/mpeg', { transcription });
return json({ status: 'success', transcription, url });
} catch (error) {
console.error('Error processing request:', error);
Expand Down
Loading