-
Notifications
You must be signed in to change notification settings - Fork 779
/
s3storage.go
70 lines (61 loc) · 1.58 KB
/
s3storage.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
package storagesvc
import (
"os"
"path"
"github.com/graymeta/stow"
"github.com/graymeta/stow/s3"
uuid "github.com/satori/go.uuid"
)
type (
s3Storage struct {
storageType StorageType
endpoint string
bucketName string
subDir string
accessKeyID string
secretAccessKey string
region string
}
)
// NewS3Storage returns a new s3 storage struct
func NewS3Storage(args ...string) Storage {
endpoint := os.Getenv("STORAGE_S3_ENDPOINT")
bucketName := os.Getenv("STORAGE_S3_BUCKET_NAME")
subDir := os.Getenv("STORAGE_S3_SUB_DIR")
accessKeyID := os.Getenv("STORAGE_S3_ACCESS_KEY_ID")
secretAccessKey := os.Getenv("STORAGE_S3_SECRET_ACCESS_KEY")
region := os.Getenv("STORAGE_S3_REGION")
return s3Storage{
endpoint: endpoint,
storageType: StorageTypeS3,
bucketName: bucketName,
subDir: subDir,
accessKeyID: accessKeyID,
secretAccessKey: secretAccessKey,
region: region,
}
}
func (ss s3Storage) getStorageType() StorageType {
return ss.storageType
}
func (ss s3Storage) getContainerName() string {
return ss.bucketName
}
func (ss s3Storage) getUploadFileName() (string, error) {
id, err := uuid.NewV4()
if err != nil {
return "", err
}
return path.Join(ss.subDir, id.String()), nil
}
func (ss s3Storage) dial() (stow.Location, error) {
kind := "s3"
config := stow.ConfigMap{
s3.ConfigEndpoint: ss.endpoint,
s3.ConfigAccessKeyID: ss.accessKeyID,
s3.ConfigSecretKey: ss.secretAccessKey,
s3.ConfigRegion: ss.region,
s3.ConfigDisableSSL: "true",
}
return stow.Dial(kind, config)
}