-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
44 lines (34 loc) · 1.12 KB
/
s3.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
package s3
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/s3"
)
// PreparePresignedURL to get object presign then return a PUT url
func (s *Service) PreparePresignedURL(key string, expireTime int, presignedURL *string) error {
req, _ := s.s3.PutObjectRequest(&s3.PutObjectInput{
Bucket: aws.String(s.cfg.BucketName),
Key: aws.String(key),
})
return processPresignedURL(req, expireTime, presignedURL)
}
// GetPresignedURL to get object presigned || get object with signed URL
func (s *Service) GetPresignedURL(key string, expireTime int, presignedURL *string) error {
req, _ := s.s3.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(s.cfg.BucketName),
Key: aws.String(key),
})
return processPresignedURL(req, expireTime, presignedURL)
}
func processPresignedURL(request *request.Request, expireTime int, presignedURL *string) error {
if expireTime == 0 {
expireTime = defaultExpireTime
}
url, err := request.Presign(time.Duration(expireTime) * time.Minute)
if err != nil {
return err
}
*presignedURL = url
return nil
}