Skip to content

Commit 8fcd0f1

Browse files
Implement DownloadFile function for S3 client and update VideoHandler to use it
1 parent 2db317e commit 8fcd0f1

3 files changed

Lines changed: 104 additions & 3 deletions

File tree

cmd/worker/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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()

internal/handlers/video.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff 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

1318
type VideoHandler struct {
14-
s3Client *s3.Client
19+
s3Client *awss3.Client
20+
cfg *config.Config
1521
}
1622

1723
func 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)

internal/s3/download.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

0 commit comments

Comments
 (0)