Skip to content

Commit e1ae353

Browse files
committed
Simplified the storage example
1 parent 09b0c9f commit e1ae353

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

docs/examples/supabase-storage-upload.mdx

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,16 @@ This task downloads a video from a provided URL, saves it to a temporary file, a
1818
```ts trigger/supabase-storage-upload.ts
1919
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
2020
import { logger, task } from "@trigger.dev/sdk/v3";
21-
import fs from "fs/promises";
2221
import fetch from "node-fetch";
23-
import os from "os";
24-
import path from "path";
2522

2623
// Initialize S3 client for Supabase Storage
27-
// Update your .env file, and environment variables in the dashboard as instructed below
2824
const s3Client = new S3Client({
2925
region: process.env.SUPABASE_REGION, // Your Supabase project's region e.g. "us-east-1"
30-
endpoint: `https://${process.env.SUPABASE_PROJECT_ID}.supabase.co/storage/v1/s3`, // Your Supabase project ID
26+
endpoint: `https://${process.env.SUPABASE_PROJECT_ID}.supabase.co/storage/v1/s3`,
3127
credentials: {
3228
// These credentials can be found in your supabase storage settings, under 'S3 access keys'
33-
accessKeyId: process.env.SUPABASE_S3_ACCESS_KEY_ID ?? "", // Access key ID
34-
secretAccessKey: process.env.SUPABASE_S3__SECRET_ACCESS_KEY ?? "", // Access key
29+
accessKeyId: process.env.SUPABASE_ACCESS_KEY_ID ?? "",
30+
secretAccessKey: process.env.SUPABASE_SECRET_ACCESS_KEY ?? "",
3531
},
3632
});
3733

@@ -40,32 +36,24 @@ export const supabaseStorageUpload = task({
4036
run: async (payload: { videoUrl: string }) => {
4137
const { videoUrl } = payload;
4238

43-
// Generate temporary file path
44-
const tempDirectory = os.tmpdir();
45-
const outputPath = path.join(tempDirectory, `video_${Date.now()}.mp4`);
46-
47-
// Fetch the video and save it to a temporary file
39+
// Fetch the video as an ArrayBuffer
4840
const response = await fetch(videoUrl);
49-
const videoBuffer = await response.arrayBuffer();
50-
await fs.writeFile(outputPath, Buffer.from(videoBuffer));
41+
const videoArrayBuffer = await response.arrayBuffer();
42+
const videoBuffer = Buffer.from(videoArrayBuffer);
5143

52-
const videoFile = await fs.readFile(outputPath); // Read the video file
5344
const bucket = "my_bucket"; // Replace "my_bucket" with your bucket name
45+
const objectKey = `video_${Date.now()}.mp4`;
5446

55-
// Upload the video to Supabase Storage
56-
const objectKey = path.basename(outputPath);
47+
// Upload the video directly to Supabase Storage
5748
await s3Client.send(
5849
new PutObjectCommand({
5950
Bucket: bucket,
6051
Key: objectKey,
61-
Body: videoFile,
52+
Body: videoBuffer,
6253
})
6354
);
6455
logger.log(`Video uploaded to Supabase Storage bucket`, { objectKey });
6556

66-
// Delete the temporary video file
67-
await fs.unlink(outputPath);
68-
6957
// Return the video object key
7058
return {
7159
objectKey,

0 commit comments

Comments
 (0)