Skip to content

Commit

Permalink
feat: added support for gcp presigned url (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
abskrj authored Sep 17, 2023
1 parent a9c4e97 commit 3e49d27
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 42 deletions.
2 changes: 1 addition & 1 deletion constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const CLOUD_CONTAINER_NAME = "zestream-dash"
const DOWNLOAD_FOLDER_PERM = 0666

const AWS_ENDPOINT = "http://localhost:4566"
const PRESIGNED_URL_EXPIRATION = 60 * time.Minute
const PRESIGNED_URL_EXPIRATION = 1 * time.Hour

// event queue
const RABBIT_MQ_CHANNEL = "VideoProcessing"
Expand Down
26 changes: 5 additions & 21 deletions controllers/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,12 @@ package controllers
import (
"net/http"
"path/filepath"
"zestream-server/configs"
"zestream-server/constants"
"zestream-server/utils"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/gin-gonic/gin"
)

func GeneratePresignedAWSURL(c *gin.Context, s3Client s3iface.S3API) {

// Obtain the file name and extension from query params
func GetPresignedURL(c *gin.Context) {
fileName := c.Query("fileName")
extension := filepath.Ext(fileName)

Expand All @@ -24,22 +17,13 @@ func GeneratePresignedAWSURL(c *gin.Context, s3Client s3iface.S3API) {
return
}

// Generate a New Video ID
videoID := utils.VideoIDGen(extension)

// Create a PutObjectRequest with the necessary parameters
req, _ := s3Client.PutObjectRequest(&s3.PutObjectInput{
Bucket: aws.String(configs.EnvVar[configs.AWS_S3_BUCKET_NAME]),
Key: aws.String(videoID),
})

// Sign the request and generate a presigned URL
urlStr, err := req.Presign(constants.PRESIGNED_URL_EXPIRATION)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error generating presigned URL", "details": err.Error()})
url := utils.GetSignedURL(videoID)
if url == "" {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error generating presigned URL", "details": ""})
return
}

c.JSON(http.StatusOK, gin.H{"preSignedURL": urlStr, "videoID": videoID})
c.JSON(http.StatusOK, gin.H{"preSignedURL": url, "videoID": videoID})
}
25 changes: 5 additions & 20 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func Init() *gin.Engine {
r := gin.Default()

r.Use(func(c *gin.Context) {
// add header Access-Control-Allow-Origin
c.Writer.Header().Set("Content-Type", "application/json")
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, UPDATE")
Expand All @@ -25,27 +24,13 @@ func Init() *gin.Engine {
}
})

// TODO: write a functin to return session of AWS/GCP/Azure
// Create a new session
// sess, err := session.NewSession(&aws.Config{
// Region: aws.String(constants.S3_REGION),
// })
// if err != nil {
// return nil
// }
apiV1 := r.Group("/api/v1")

// // Create a new S3 client
// s3Client := s3.New(sess)
r.GET("health", controllers.Ping)

v1 := r.Group("/api/v1")

v1.GET("ping", controllers.Ping)

v1.POST("process-video", controllers.ProcessVideo)

// v1.GET("generate-presigned-url", func(c *gin.Context) {
// controllers.GeneratePresignedURL(c, s3Client)
// })
// /api/v1
apiV1.POST("video/process", controllers.ProcessVideo)
apiV1.GET("url/presigned", controllers.GetPresignedURL)

return r
}
53 changes: 53 additions & 0 deletions utils/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import (
"net/url"
"os"
"path/filepath"
"time"
"zestream-server/configs"
"zestream-server/constants"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"

"cloud.google.com/go/storage"
Expand Down Expand Up @@ -223,3 +226,53 @@ func (a AzureUploader) Upload(walker fileWalk) {
}

}

func GetSignedURL(videoId string) string {
if configs.EnvVar[configs.AWS_ACCESS_KEY_ID] != "" {
return generateAWSSignedURL(videoId)
}

if configs.EnvVar[configs.GCP_BUCKET_NAME] != "" {
return generateGCPSignedURL(videoId)
}

return ""
}

func generateGCPSignedURL(videoId string) string {
session := configs.GetCloudSession()
bucket := session.GCPSession.Bucket(configs.EnvVar[configs.GCP_BUCKET_NAME])

expirationTime := time.Now().Add(constants.PRESIGNED_URL_EXPIRATION)

url, err := bucket.SignedURL(videoId, &storage.SignedURLOptions{
Method: "GET",
Expires: expirationTime,
})
if err != nil {
log.Println(err)
}

return url
}

func generateAWSSignedURL(videoId string) string {
session := configs.GetCloudSession()

client := s3.New(session.AWSSession)

req, err := client.PutObjectRequest(&s3.PutObjectInput{
Bucket: aws.String(configs.EnvVar[configs.AWS_S3_BUCKET_NAME]),
Key: aws.String(videoId),
})
if err != nil {
log.Println(err)
}

url, er := req.Presign(constants.PRESIGNED_URL_EXPIRATION)
if er != nil {
log.Println(err)
}

return url
}

0 comments on commit 3e49d27

Please sign in to comment.