url signing
This commit is contained in:
@@ -163,7 +163,7 @@ func (cfg *apiConfig) handlerUploadVideo(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
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
|
video.VideoURL = &url
|
||||||
|
|
||||||
@@ -179,5 +179,11 @@ func (cfg *apiConfig) handlerUploadVideo(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,7 +95,12 @@ func (cfg *apiConfig) handlerVideoGet(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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) {
|
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
|
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
35
main.go
@@ -4,13 +4,17 @@ 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"
|
||||||
"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/bootdotdev/learn-file-storage-s3-golang-starter/internal/database"
|
||||||
|
|
||||||
@@ -80,6 +84,37 @@ 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")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user