-
-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Endpoint for pre signing url for azure/gcp #64
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -72,6 +75,38 @@ func UploadToCloudStorage(uploader Uploader, path string) { | |
uploader.Upload(walker) | ||
} | ||
|
||
func GetPreSignedURL(videoId string) string { | ||
cloudSession := configs.GetCloudSession() | ||
|
||
containerName := constants.CLOUD_CONTAINER_NAME_TEMP | ||
|
||
if configs.EnvVar[configs.GCP_PROJECT_ID] != "" { | ||
return getPresignedGCPURL(&GcpUploader{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a suggestion, correct me if I am wrong .why are we passing a pointer to the function generating predesigned URL for GCP, shouldn't the code be uniform for every url generating function? |
||
ContainerName: containerName, | ||
VideoId: videoId, | ||
Client: cloudSession.GCPSession, | ||
}) | ||
} | ||
|
||
if configs.EnvVar[configs.AWS_ACCESS_KEY_ID] != "" { | ||
return getPresignedAWSURL(AwsUploader{ | ||
ContainerName: containerName, | ||
VideoId: videoId, | ||
Session: cloudSession.AWSSession, | ||
}) | ||
} | ||
|
||
if configs.EnvVar[configs.AZURE_ACCESS_KEY] != "" { | ||
return getPresignedAzureURL(AzureUploader{ | ||
ContainerName: containerName, | ||
VideoId: videoId, | ||
Comment on lines
+83
to
+102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use switch-case whenever multiple if-else are involved switch configs.EnvVar[configs.GCP_PROJECT_ID] {
case "":
return getPresignedGCPURL(&GcpUploader{
ContainerName: containerName,
VideoId: videoId,
Client: cloudSession.GCPSession,
})
case "":
return getPresignedAWSURL(AwsUploader{
ContainerName: containerName,
VideoId: videoId,
Session: cloudSession.AWSSession,
})
case "":
return getPresignedAzureURL(AzureUploader{
ContainerName: containerName,
VideoId: videoId,
Credential: cloudSession.AzureSession,
})
default:
return ""
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here, we are checking != "", and we can't negate values in switch case, thats why used if |
||
Credential: cloudSession.AzureSession, | ||
}) | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func (f fileWalk) WalkFunc(path string, info os.FileInfo, err error) error { | ||
|
||
if err != nil { | ||
|
@@ -127,6 +162,21 @@ func (a AwsUploader) Upload(walker fileWalk) { | |
} | ||
} | ||
|
||
func getPresignedAWSURL(a AwsUploader) string { | ||
s3Client := s3.New(a.Session) | ||
|
||
req, _ := s3Client.PutObjectRequest(&s3.PutObjectInput{ | ||
Bucket: aws.String(configs.EnvVar[configs.AWS_S3_BUCKET_NAME]), | ||
Key: aws.String(filepath.Join(a.ContainerName, a.VideoId)), | ||
}) | ||
|
||
// Sign the request and generate a presigned URL | ||
urlStr, err := req.Presign(constants.PRESIGNED_URL_EXPIRATION) | ||
LogErr(err) | ||
|
||
return urlStr | ||
} | ||
|
||
type GcpUploader struct { | ||
ContainerName string | ||
VideoId string | ||
|
@@ -175,6 +225,25 @@ func (g *GcpUploader) Upload(walker fileWalk) { | |
|
||
} | ||
|
||
func getPresignedGCPURL(g *GcpUploader) string { | ||
bucketName := configs.EnvVar[configs.GCP_BUCKET_NAME] | ||
if bucketName == "" { | ||
log.Println("GCP Bucketname not available") | ||
} | ||
|
||
now := time.Now() | ||
urlExpiryTime := now.Add(constants.PRESIGNED_URL_EXPIRATION) | ||
|
||
u, err := g.Client.Bucket(bucketName).SignedURL(filepath.Join(g.ContainerName, g.VideoId), &storage.SignedURLOptions{ | ||
Expires: urlExpiryTime, | ||
}) | ||
LogErr(err) | ||
|
||
signedURL, _ := url.Parse(u) | ||
|
||
return signedURL.String() | ||
} | ||
|
||
type AzureUploader struct { | ||
ContainerName string | ||
VideoId string | ||
|
@@ -184,8 +253,6 @@ type AzureUploader struct { | |
func (a AzureUploader) Upload(walker fileWalk) { | ||
azureEndpoint := configs.EnvVar[configs.AZURE_ENDPOINT] | ||
|
||
log.Println(azureEndpoint) | ||
|
||
if azureEndpoint == "" { | ||
log.Println("Azure endpoint not available") | ||
} | ||
|
@@ -221,5 +288,15 @@ func (a AzureUploader) Upload(walker fileWalk) { | |
log.Println("Unable to close the file ", path) | ||
} | ||
} | ||
} | ||
|
||
func getPresignedAzureURL(a AzureUploader) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to add expiry time for the azure presigned URL |
||
azureEndpoint := configs.EnvVar[configs.AZURE_ENDPOINT] | ||
|
||
url, _ := url.Parse(azureEndpoint) | ||
url = url.JoinPath(a.ContainerName, a.VideoId) | ||
blockBlobUrl := azblob.NewBlockBlobURL(*url, azblob.NewPipeline(a.Credential, azblob.PipelineOptions{})) | ||
|
||
urlSigned := blockBlobUrl.URL() | ||
return urlSigned.String() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better if we can Extract the logic for getting the presigned URL into separate functions for each cloud provider, and call the appropriate function based on the cloud provider. For example, you can have a function
getPresignedGCPURL()
that takes the necessary parameters and returns the presigned URL for GCP, and similarly for AWS and Azure. This way, the code for each cloud provider is separated and more readable.We can move these cloud specific function to a different file as well for code modularity and readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, was trying to do the same and wanted to share the sessions to each of functions, got this was only, will try to improve this in next fix