File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ func main() {
1919 s3Client := s3 .NewClient (cfg )
2020 videoHandler := handlers .NewVideoHandler (
2121 s3Client ,
22+ cfg ,
2223 )
2324
2425 mux := asynq .NewServeMux ()
Original file line number Diff line number Diff line change @@ -5,20 +5,28 @@ import (
55 "encoding/json"
66 "log"
77
8+ "github.com/Prateet-Github/worker-go/internal/config"
89 "github.com/Prateet-Github/worker-go/internal/queue"
9- "github.com/aws/aws-sdk-go-v2/service/s3"
10+ "github.com/Prateet-Github/worker-go/internal/s3"
11+
12+ "path/filepath"
13+
14+ awss3 "github.com/aws/aws-sdk-go-v2/service/s3"
1015 "github.com/hibiken/asynq"
1116)
1217
1318type VideoHandler struct {
14- s3Client * s3.Client
19+ s3Client * awss3.Client
20+ cfg * config.Config
1521}
1622
1723func NewVideoHandler (
18- s3Client * s3.Client ,
24+ s3Client * awss3.Client ,
25+ cfg * config.Config ,
1926) * VideoHandler {
2027 return & VideoHandler {
2128 s3Client : s3Client ,
29+ cfg : cfg ,
2230 }
2331}
2432
@@ -33,6 +41,24 @@ func (h *VideoHandler) ProcessVideo(
3341 return err
3442 }
3543
44+ localPath := filepath .Join (
45+ "temp" ,
46+ payload .VideoID + ".mp4" ,
47+ )
48+
49+ err := s3 .DownloadFile (
50+ h .s3Client ,
51+ h .cfg .S3RawBucket ,
52+ payload .S3Key ,
53+ localPath ,
54+ )
55+
56+ if err != nil {
57+ return err
58+ }
59+
60+ log .Println ("Download completed:" , localPath )
61+
3662 log .Println ("Processing video..." )
3763 log .Println ("Video ID:" , payload .VideoID )
3864 log .Println ("S3 Key:" , payload .S3Key )
Original file line number Diff line number Diff line change 1+ package s3
2+
3+ import (
4+ "context"
5+ "fmt"
6+ "io"
7+ "os"
8+ "path/filepath"
9+
10+ awss3 "github.com/aws/aws-sdk-go-v2/service/s3"
11+ )
12+
13+ func DownloadFile (
14+ client * awss3.Client ,
15+ bucket string ,
16+ key string ,
17+ outputPath string ,
18+ ) error {
19+
20+ fmt .Printf ("Downloading: %s\n " , key )
21+ fmt .Printf ("Saving to: %s\n " , outputPath )
22+
23+ resp , err := client .GetObject (
24+ context .Background (),
25+ & awss3.GetObjectInput {
26+ Bucket : & bucket ,
27+ Key : & key ,
28+ },
29+ )
30+
31+ if err != nil {
32+ return err
33+ }
34+ defer resp .Body .Close ()
35+
36+ if err := os .MkdirAll (
37+ filepath .Dir (outputPath ),
38+ 0755 ,
39+ ); err != nil {
40+ return err
41+ }
42+
43+ file , err := os .Create (outputPath )
44+
45+ if err != nil {
46+ return err
47+ }
48+ defer file .Close ()
49+
50+ if _ , err := io .Copy (file , resp .Body ); err != nil {
51+ _ = os .Remove (outputPath )
52+ return err
53+ }
54+
55+ info , err := file .Stat ()
56+
57+ if err != nil {
58+ _ = os .Remove (outputPath )
59+ return err
60+ }
61+
62+ if info .Size () == 0 {
63+ _ = os .Remove (outputPath )
64+ return fmt .Errorf ("downloaded file is empty" )
65+ }
66+
67+ fmt .Printf (
68+ "Downloaded: %s -> %s\n " ,
69+ key ,
70+ outputPath ,
71+ )
72+
73+ return nil
74+ }
You can’t perform that action at this time.
0 commit comments