-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
option.go
88 lines (78 loc) · 1.82 KB
/
option.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package s3storage
import (
"strings"
"time"
"github.com/aws/aws-sdk-go/service/s3"
)
// Option S3Storage option
type Option func(h *S3Storage)
// WithBaseDir with base dir option
func WithBaseDir(baseDir string) Option {
return func(s *S3Storage) {
if baseDir != "" {
baseDir = "/" + strings.Trim(baseDir, "/")
if baseDir != "/" {
baseDir += "/"
}
s.BaseDir = baseDir
}
}
}
// WithPathPrefix with path prefix option
func WithPathPrefix(prefix string) Option {
return func(s *S3Storage) {
if prefix != "" {
prefix = "/" + strings.Trim(prefix, "/")
if prefix != "/" {
prefix += "/"
}
s.PathPrefix = prefix
}
}
}
var aclValuesMap = (func() map[string]bool {
m := map[string]bool{}
for _, acl := range s3.ObjectCannedACL_Values() {
m[acl] = true
}
return m
})()
// WithACL with ACL option
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html#canned-acl
func WithACL(acl string) Option {
return func(h *S3Storage) {
if aclValuesMap[acl] {
h.ACL = acl
}
}
}
// WithSafeChars with safe chars option
func WithSafeChars(chars string) Option {
return func(h *S3Storage) {
if chars != "" {
h.SafeChars = chars
}
}
}
// WithExpiration with modified time expiration option
func WithExpiration(exp time.Duration) Option {
return func(h *S3Storage) {
if exp > 0 {
h.Expiration = exp
}
}
}
// WithFileStorageClass with storage storage class option
func WithStorageClass(storageClass string) Option {
return func(h *S3Storage) {
allowedStorageClasses := [6]string{"REDUCED_REDUNDANCY", "STANDARD_IA", "ONEZONE_IA",
"INTELLIGENT_TIERING", "GLACIER", "DEEP_ARCHIVE"}
h.StorageClass = "STANDARD"
for _, allowedStorageClass := range allowedStorageClasses {
if storageClass == allowedStorageClass {
h.StorageClass = storageClass
break
}
}
}
}