Skip to content

Instantly share code, notes, and snippets.

@wingkwong
Last active December 27, 2019 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wingkwong/a7a33fee0b640997991753d9f06ff120 to your computer and use it in GitHub Desktop.
Save wingkwong/a7a33fee0b640997991753d9f06ff120 to your computer and use it in GitHub Desktop.
A simple function to generate s3 presigned url in Go
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