Adding Presign URL Expiration for AWS s3 in aws-sdk-go-v2 for Go

2.4k Views Asked by At

There is a very fine example of using the aws-sdk-go-v2 to create pre-signed URLs.

This works well but I'm stuck on setting the expiration time. I do see the PresignOptions struct has an Expires but I'm not certain how to do that.

I also see WithPresignExpires but alas, I also do not know how to use this given the example.

Can an example be provided here? I'm learning Go but something is just outside my grasp here.

2

There are 2 best solutions below

0
On

You can also pass more options to the PresignGetObject function (aws s3 sdk example)

This example would generate this header X-Amz-Expires=604800 which would make the link available for 7 days:

request, err := presigner.PresignClient.PresignGetObject(context.TODO(), &s3.GetObjectInput{
    Bucket: aws.String(bucketName),
    Key:    aws.String(objectKey),
}, func(opts *s3.PresignOptions) {
    opts.Expires = time.Duration(7 * 24 * time.Hour)
})
0
On

Seems I can do this for 60 minute expiration.

func GetPresignedURL(c context.Context, api S3PresignGetObjectAPI, input *s3.GetObjectInput) (*v4.PresignedHTTPRequest, error) {
    return api.PresignGetObject(c, input, s3.WithPresignExpires(3600*time.Second))
}