url signing

This commit is contained in:
2025-03-16 11:24:00 +02:00
parent 34d50fde30
commit 07d574acd9
3 changed files with 64 additions and 4 deletions

View File

@@ -163,7 +163,7 @@ func (cfg *apiConfig) handlerUploadVideo(w http.ResponseWriter, r *http.Request)
return
}
url := fmt.Sprintf("http://%s.s3.%s.amazonaws.com/%s", cfg.s3Bucket, cfg.s3Region, fileName)
url := fmt.Sprintf("%s,%s", cfg.s3Bucket, fileName)
video.VideoURL = &url
@@ -179,5 +179,11 @@ func (cfg *apiConfig) handlerUploadVideo(w http.ResponseWriter, r *http.Request)
return
}
respondWithJSON(w, http.StatusOK, video)
vid, err := cfg.dbVideoToSignedVideo(video)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't convert video to signed video", err)
return
}
respondWithJSON(w, http.StatusOK, vid)
}

View File

@@ -95,7 +95,12 @@ func (cfg *apiConfig) handlerVideoGet(w http.ResponseWriter, r *http.Request) {
return
}
respondWithJSON(w, http.StatusOK, video)
vid, err := cfg.dbVideoToSignedVideo(video)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't convert video to signed video", err)
return
}
respondWithJSON(w, http.StatusOK, vid)
}
func (cfg *apiConfig) handlerVideosRetrieve(w http.ResponseWriter, r *http.Request) {
@@ -116,5 +121,19 @@ func (cfg *apiConfig) handlerVideosRetrieve(w http.ResponseWriter, r *http.Reque
return
}
respondWithJSON(w, http.StatusOK, videos)
var vids []database.Video
if len(videos) > 0 {
for _, video := range videos {
vid, err := cfg.dbVideoToSignedVideo(video)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't convert video to signed video", err)
return
}
vids = append(vids, vid)
}
} else {
vids = make([]database.Video, 0)
}
respondWithJSON(w, http.StatusOK, vids)
}

35
main.go
View File

@@ -4,13 +4,17 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"log"
"net/http"
"os"
"os/exec"
"strings"
"time"
"github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/database"
@@ -80,6 +84,37 @@ func processVideoForFastStart(filePath string) (string, error) {
return newName, nil
}
func generatePresignedURL(s3Client *s3.Client, bucket, key string, expireTime time.Duration) (string, error) {
presignedClient := s3.NewPresignClient(s3Client)
obj, err := presignedClient.PresignGetObject(context.Background(), &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}, s3.WithPresignExpires(expireTime))
if err != nil {
return "", err
}
return obj.URL, nil
}
func (cfg *apiConfig) dbVideoToSignedVideo(video database.Video) (database.Video, error) {
if video.VideoURL == nil {
return video, nil
}
pieces := strings.Split(*video.VideoURL, ",")
if len(pieces) < 2 {
return database.Video{}, errors.New("Invalid video URL")
}
bucket, key := pieces[0], pieces[1]
url, err := generatePresignedURL(cfg.s3Client, bucket, key, time.Second*600)
if err != nil {
return database.Video{}, err
}
video.VideoURL = &url
return video, nil
}
func main() {
godotenv.Load(".env")