Last active
December 27, 2019 07:49
-
-
Save wingkwong/a7a33fee0b640997991753d9f06ff120 to your computer and use it in GitHub Desktop.
A simple function to generate s3 presigned url in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func GetS3PresignedUrl(bucket string, key string, region string, expiration time.Duration) string { | |
// Initialize a session in the target region that the SDK will use to load | |
// credentials from the shared credentials file ~/.aws/credentials. | |
sess, err := session.NewSession(&aws.Config{ | |
Region: aws.String(region)}, | |
) | |
// Create S3 service client | |
svc := s3.New(sess) | |
// Construct a GetObjectRequest request | |
req, _ := svc.GetObjectRequest(&s3.GetObjectInput{ | |
Bucket: aws.String(bucket), | |
Key: aws.String(key), | |
}) | |
// Presign with expiration time | |
presignedUrl, err := req.Presign(expiration * time.Minute) | |
// Check if it can be signed or not | |
if err != nil { | |
fmt.Println("Failed to sign request", err) | |
} | |
// Return the presigned url | |
return presignedUrl | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment