forked from microsoft/presidio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
199 lines (168 loc) · 6.18 KB
/
storage.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package storage
import (
"bytes"
"fmt"
"strings"
log "github.com/Microsoft/presidio/pkg/logger"
"github.com/korovkin/limiter"
"github.com/presid-io/stow"
"github.com/presid-io/stow/azure"
"github.com/presid-io/stow/s3"
types "github.com/Microsoft/presidio-genproto/golang"
)
//API storage
type API struct {
location stow.Location
concurrencyLimit int
}
//New initialize a new storage instance.
// Kind is the storage kind: s3/blob storage/ google cloud.
// config holds the storage connection string
// concurrencyLimit is the limit for how many item needs to be scanned at once.
func New(kind string, config stow.Config, concurrencyLimit int) (*API, error) {
location, err := stow.Dial(kind, config)
if err != nil {
return &API{}, err
}
return &API{location: location, concurrencyLimit: concurrencyLimit}, nil
}
// Init cloud storage config
func Init(cloudStorageConfig *types.CloudStorageConfig) (stow.ConfigMap, string, string, error) {
if cloudStorageConfig.GetBlobStorageConfig() != nil {
config, containerName := InitBlobStorage(cloudStorageConfig)
return config, containerName, "azure", nil
} else if cloudStorageConfig.GetS3Config() != nil {
config, containerName := InitS3(cloudStorageConfig)
return config, containerName, "s3", nil
// } else if cloudStorageConfig.GetGoogleStorageConfig() != nil {
// config, containerName := InitGoogle(cloudStorageConfig)
// return config, containerName, "google", nil
} else {
return nil, "", "", fmt.Errorf("storage config is not defined")
}
}
//CreateS3Config create S3 configuration
func CreateS3Config(accessKeyID string, secretKey string, region string, endpoint string) stow.ConfigMap {
configMap := stow.ConfigMap{
s3.ConfigAccessKeyID: accessKeyID,
s3.ConfigSecretKey: secretKey,
s3.ConfigRegion: region,
}
if endpoint != "" {
configMap.Set(s3.ConfigEndpoint, endpoint)
}
return configMap
}
// //CreatGoogleConfig create google configuration
// func CreatGoogleConfig(json string, projectID string, scopes string) (string, stow.ConfigMap) {
// return "google", stow.ConfigMap{
// google.ConfigJSON: json,
// google.ConfigProjectId: projectID,
// google.ConfigScopes: scopes,
// }
// }
// // InitGoogle inits the storage with the supplied parameters
// func InitGoogle(cloudStorageConfig *types.CloudStorageConfig) (stow.ConfigMap, string) {
// googleJson := cloudStorageConfig.GoogleStorageConfig.GetJson()
// googleProjectID := cloudStorageConfig.GoogleStorageConfig.GetProjectId()
// googleScopes := cloudStorageConfig.GoogleStorageConfig.GetScopes()
// if googleJson == "" || googleProjectId == "" || googleScopes == "" {
// log.Fatal("json, projectId, scopes must me set for google storage kind.")
// }
// _, config := CreatGoogleConfig(googleJson, googleProjectID, googleScopes)
// return config, s3Bucket
// }
// InitS3 inits the storage with the supplied credentials
func InitS3(cloudStorageConfig *types.CloudStorageConfig) (stow.ConfigMap, string) {
s3AccessKeyID := cloudStorageConfig.S3Config.GetAccessId()
s3SecretKey := cloudStorageConfig.S3Config.GetAccessKey()
s3Region := cloudStorageConfig.S3Config.GetRegion()
s3Bucket := cloudStorageConfig.S3Config.GetBucketName()
s3Endpoint := cloudStorageConfig.S3Config.GetEndpoint()
if s3AccessKeyID == "" || s3SecretKey == "" || s3Region == "" || s3Bucket == "" {
log.Fatal("accessId, accessKey, region, bucket must me set for s3 storage kind.")
}
config := CreateS3Config(s3AccessKeyID, s3SecretKey, s3Region, s3Endpoint)
return config, s3Bucket
}
//CreateAzureConfig create azure configuration
func CreateAzureConfig(account string, key string) stow.ConfigMap {
return stow.ConfigMap{
azure.ConfigAccount: account,
azure.ConfigKey: key,
}
}
// InitBlobStorage inits the storage with the supplied credentials
func InitBlobStorage(cloudStorageConfig *types.CloudStorageConfig) (stow.ConfigMap, string) {
azureAccountName := cloudStorageConfig.BlobStorageConfig.GetAccountName()
azureAccountKey := cloudStorageConfig.BlobStorageConfig.GetAccountKey()
azureContainer := cloudStorageConfig.BlobStorageConfig.GetContainerName()
if azureAccountKey == "" || azureAccountName == "" || azureContainer == "" {
log.Fatal("accountName, AccountKey and containerName vars must me set for azure config.")
}
config := CreateAzureConfig(azureAccountName, azureAccountKey)
return config, azureContainer
}
// CreateContainer create a container/bucket or return a reference if already exists
func (a *API) CreateContainer(name string) (stow.Container, error) {
container, err := a.location.CreateContainer(name)
if err != nil {
if strings.Contains(err.Error(), "ContainerAlreadyExists") {
x, err1 := a.location.Container(name)
return x, err1
}
return nil, err
}
return container, err
}
// RemoveContainer removes a container/bucket from the storage
func (a *API) RemoveContainer(name string) error {
return a.location.RemoveContainer(name)
}
// walkFunc contain the logic that need to be implemented on each of the items in the container
type walkFunc func(item stow.Item)
// WalkFiles walks over the files in 'container' and executes fn func
func (a *API) WalkFiles(container stow.Container, walkFunc walkFunc) error {
limit := limiter.NewConcurrencyLimiter(a.concurrencyLimit)
err := stow.Walk(container, stow.NoPrefix, 100, func(item stow.Item, err error) error {
if err != nil {
return err
}
limit.Execute(func() {
walkFunc(item)
})
return err
})
limit.Wait()
return err
}
// ReadObject reads the item's content
func ReadObject(item stow.Item) (string, error) {
reader, err := item.Open()
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
if _, err = buf.ReadFrom(reader); err != nil {
return "", err
}
n, err := item.Size()
if err != nil {
return "", err
}
maxFileSizeInBytes := 500000
// if file size > 0.5Mb trim it
if n > int64(maxFileSizeInBytes) {
buf.Truncate(maxFileSizeInBytes)
}
fileContent := buf.String()
err = reader.Close()
return fileContent, err
}
// PutItem writes a new item to the container
func PutItem(name string, content string, container stow.Container) error {
r := strings.NewReader(content)
size := int64(len(content))
_, err := container.Put(name, r, size, nil)
return err
}