-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresigner.go
53 lines (43 loc) · 2.07 KB
/
presigner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"context"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type Presigner interface {
PresignGetObject(context.Context, *s3.GetObjectInput, ...func(*s3.PresignOptions)) (*v4.PresignedHTTPRequest, error)
PresignPutObject(context.Context, *s3.PutObjectInput, ...func(*s3.PresignOptions)) (*v4.PresignedHTTPRequest, error)
}
// S3Presigner encapsulates the Amazon Simple Storage Service (Amazon S3) presign actions
// used in the examples.
// It contains PresignClient, a client that is used to presign requests to Amazon S3.
// Presigned requests contain temporary credentials and can be made from any HTTP client.
type S3Presigner struct {
client *s3.PresignClient
region string
}
func NewS3Presigner(client *s3.PresignClient, region string) S3Presigner {
return S3Presigner{client, region}
}
// GetObject makes a presigned request that can be used to get an object from a bucket.
// The presigned request is valid for the specified number of seconds.
func (p S3Presigner) GetObject(ctx context.Context, bucket, key string, exp time.Duration) (*v4.PresignedHTTPRequest, error) {
input := &s3.GetObjectInput{Bucket: aws.String(bucket), Key: aws.String(key)}
return p.client.PresignGetObject(ctx, input, s3.WithPresignExpires(exp), withRegion(p.region))
}
// PutObject makes a presigned request that can be used to put an object in a bucket.
// The presigned request is valid for the specified number of seconds.
func (p S3Presigner) PutObject(ctx context.Context, bucket, key string, exp time.Duration) (*v4.PresignedHTTPRequest, error) {
input := &s3.PutObjectInput{Bucket: aws.String(bucket), Key: aws.String(key)}
return p.client.PresignPutObject(ctx, input, s3.WithPresignExpires(exp), withRegion(p.region))
}
// withRegion is a helper function that adds the region to the PresignOptions.
func withRegion(region string) func(*s3.PresignOptions) {
return func(o *s3.PresignOptions) {
o.ClientOptions = append(o.ClientOptions,
func(o *s3.Options) { o.Region = region },
)
}
}