@@ -18,20 +18,16 @@ This task downloads a video from a provided URL, saves it to a temporary file, a
18
18
``` ts trigger/supabase-storage-upload.ts
19
19
import { PutObjectCommand , S3Client } from " @aws-sdk/client-s3" ;
20
20
import { logger , task } from " @trigger.dev/sdk/v3" ;
21
- import fs from " fs/promises" ;
22
21
import fetch from " node-fetch" ;
23
- import os from " os" ;
24
- import path from " path" ;
25
22
26
23
// Initialize S3 client for Supabase Storage
27
- // Update your .env file, and environment variables in the dashboard as instructed below
28
24
const s3Client = new S3Client ({
29
25
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 ` ,
31
27
credentials: {
32
28
// 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 ?? " " ,
35
31
},
36
32
});
37
33
@@ -40,32 +36,24 @@ export const supabaseStorageUpload = task({
40
36
run : async (payload : { videoUrl: string }) => {
41
37
const { videoUrl } = payload ;
42
38
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
48
40
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 );
51
43
52
- const videoFile = await fs .readFile (outputPath ); // Read the video file
53
44
const bucket = " my_bucket" ; // Replace "my_bucket" with your bucket name
45
+ const objectKey = ` video_${Date .now ()}.mp4 ` ;
54
46
55
- // Upload the video to Supabase Storage
56
- const objectKey = path .basename (outputPath );
47
+ // Upload the video directly to Supabase Storage
57
48
await s3Client .send (
58
49
new PutObjectCommand ({
59
50
Bucket: bucket ,
60
51
Key: objectKey ,
61
- Body: videoFile ,
52
+ Body: videoBuffer ,
62
53
})
63
54
);
64
55
logger .log (` Video uploaded to Supabase Storage bucket ` , { objectKey });
65
56
66
- // Delete the temporary video file
67
- await fs .unlink (outputPath );
68
-
69
57
// Return the video object key
70
58
return {
71
59
objectKey ,
0 commit comments