cloudfront

This commit is contained in:
2025-03-16 11:32:00 +02:00
parent 07d574acd9
commit 3acebd4bc9
3 changed files with 5 additions and 66 deletions

View File

@@ -163,7 +163,7 @@ func (cfg *apiConfig) handlerUploadVideo(w http.ResponseWriter, r *http.Request)
return return
} }
url := fmt.Sprintf("%s,%s", cfg.s3Bucket, fileName) url := fmt.Sprintf("%s/%s", cfg.s3CfDistribution, fileName)
video.VideoURL = &url video.VideoURL = &url
@@ -179,11 +179,5 @@ func (cfg *apiConfig) handlerUploadVideo(w http.ResponseWriter, r *http.Request)
return return
} }
vid, err := cfg.dbVideoToSignedVideo(video) respondWithJSON(w, http.StatusOK, 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,12 +95,7 @@ func (cfg *apiConfig) handlerVideoGet(w http.ResponseWriter, r *http.Request) {
return return
} }
vid, err := cfg.dbVideoToSignedVideo(video) respondWithJSON(w, http.StatusOK, 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) { func (cfg *apiConfig) handlerVideosRetrieve(w http.ResponseWriter, r *http.Request) {
@@ -121,19 +116,5 @@ func (cfg *apiConfig) handlerVideosRetrieve(w http.ResponseWriter, r *http.Reque
return return
} }
var vids []database.Video respondWithJSON(w, http.StatusOK, videos)
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)
} }

38
main.go
View File

@@ -4,19 +4,14 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/database"
"log" "log"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"strings"
"time"
"github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/database"
"github.com/joho/godotenv" "github.com/joho/godotenv"
_ "github.com/lib/pq" _ "github.com/lib/pq"
@@ -84,37 +79,6 @@ func processVideoForFastStart(filePath string) (string, error) {
return newName, nil 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() { func main() {
godotenv.Load(".env") godotenv.Load(".env")