/
presignurl.go
62 lines (51 loc) · 1.63 KB
/
presignurl.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
54
55
56
57
58
59
60
61
62
package main
import (
"context"
"fmt"
"log"
"os"
"time"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
// Presigner 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 Presigner struct {
PresignClient *s3.PresignClient
}
// 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 (presigner Presigner) PutObject(
bucketName string, objectKey string, lifetimeSecs int64) (*v4.PresignedHTTPRequest, error) {
request, err := presigner.PresignClient.PresignPutObject(context.TODO(), &s3.PutObjectInput{
Bucket: &bucketName,
Key: &objectKey,
}, func(opts *s3.PresignOptions) {
opts.Expires = time.Duration(lifetimeSecs * int64(time.Second))
})
if err != nil {
log.Printf("Couldn't get a presigned request to put %v:%v. Here's why: %v\n",
bucketName, objectKey, err)
}
return request, err
}
func playgroundGetPresignedURL() {
// ctx := context.Background()
config := getRawConfig()
s3Client := getRawS3Driver(config)
presignClient := s3.NewPresignClient(s3Client)
presigner := Presigner{
PresignClient: presignClient,
}
presignedPutRequest, err := presigner.PutObject("bucketname", "folder/file.pdf", 300)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
fmt.Printf("%+v\n", presignedPutRequest)
}
func main() {
playgroundGetPresignedURL()
}