This repository has been archived by the owner on Aug 12, 2022. It is now read-only.
forked from maorfr/skbn
/
abs.go
220 lines (184 loc) · 5.37 KB
/
abs.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package skbn
import (
"context"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/Azure/azure-pipeline-go/pipeline"
"github.com/Azure/azure-storage-blob-go/azblob"
)
// GetClientToAbs checks the connection to azure blob storage and returns the tested client (pipeline)
func GetClientToAbs(ctx context.Context, path string) (pipeline.Pipeline, error) {
pSplit := strings.Split(path, "/")
a, c, _ := initAbsVariables(pSplit)
pl, err := getNewPipeline()
if err != nil {
return nil, err
}
su, err := getServiceURL(pl, a)
if err != nil {
return nil, err
}
lc, err := listContainers(ctx, su)
if err != nil {
return nil, err
}
if !containerExists(lc, c) {
return nil, fmt.Errorf("Azure Blob Storage container doesn't exist")
}
return pl, nil
}
// GetListOfFilesFromAbs gets list of files in path from azure blob storage (recursive)
func GetListOfFilesFromAbs(ctx context.Context, iClient interface{}, path string) ([]string, error) {
pSplit := strings.Split(path, "/")
if err := validateAbsPath(pSplit); err != nil {
return nil, err
}
a, c, p := initAbsVariables(pSplit)
pl := iClient.(pipeline.Pipeline)
cu, err := getContainerURL(pl, a, c)
if err != nil {
return nil, err
}
bl := []string{}
for marker := (azblob.Marker{}); marker.NotDone(); {
listBlob, err := cu.ListBlobsFlatSegment(ctx, marker, azblob.ListBlobsSegmentOptions{})
if err != nil {
return nil, err
}
marker = listBlob.NextMarker
for _, blobInfo := range listBlob.Segment.BlobItems {
if !strings.Contains(blobInfo.Name, p) {
continue
}
bl = append(bl, strings.Replace(blobInfo.Name, p, "", 1))
}
}
return bl, nil
}
// DownloadFromAbs downloads a single file from azure blob storage
func DownloadFromAbs(ctx context.Context, iClient interface{}, path string, writer io.Writer) error {
pSplit := strings.Split(path, "/")
if err := validateAbsPath(pSplit); err != nil {
return err
}
a, c, p := initAbsVariables(pSplit)
pl := iClient.(pipeline.Pipeline)
cu, err := getContainerURL(pl, a, c)
if err != nil {
return err
}
bu := getBlobURL(cu, p)
dr, err := bu.Download(ctx, 0, azblob.CountToEnd, azblob.BlobAccessConditions{}, false)
if err != nil {
return err
}
bs := dr.Body(azblob.RetryReaderOptions{MaxRetryRequests: 20})
defer bs.Close()
_, err = io.Copy(writer, bs)
if err != nil {
return err
}
return nil
}
// UploadToAbs uploads a single file to azure blob storage
func UploadToAbs(ctx context.Context, iClient interface{}, toPath, fromPath string, reader io.Reader) error {
pSplit := strings.Split(toPath, "/")
if err := validateAbsPath(pSplit); err != nil {
return err
}
if len(pSplit) == 1 {
_, fn := filepath.Split(fromPath)
pSplit = append(pSplit, fn)
}
a, c, p := initAbsVariables(pSplit)
pl := iClient.(pipeline.Pipeline)
cu, err := getContainerURL(pl, a, c)
if err != nil {
return err
}
bu := getBlobURL(cu, p)
_, err = azblob.UploadStreamToBlockBlob(ctx, reader, bu, azblob.UploadStreamToBlockBlobOptions{
BufferSize: 4 * 1024 * 1024,
MaxBuffers: 16,
})
if err != nil {
return err
}
return nil
}
func validateAbsPath(pathSplit []string) error {
if len(pathSplit) >= 1 {
return nil
}
return fmt.Errorf("illegal path: %s", filepath.Join(pathSplit...))
}
func initAbsVariables(split []string) (string, string, string) {
account := split[0]
container := split[1]
path := filepath.Join(split[2:]...)
return account, container, path
}
func getNewPipeline() (pipeline.Pipeline, error) {
accountName, accountKey := os.Getenv("AZURE_STORAGE_ACCOUNT"), os.Getenv("AZURE_STORAGE_ACCESS_KEY")
if len(accountName) == 0 {
return nil, fmt.Errorf("AZURE_STORAGE_ACCOUNT environment variable is not set")
}
if len(accountKey) == 0 {
return nil, fmt.Errorf("AZURE_STORAGE_ACCESS_KEY environment variable is not set")
}
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
if err != nil {
return nil, err
}
po := azblob.PipelineOptions{
Retry: azblob.RetryOptions{
Policy: azblob.RetryPolicyExponential,
MaxTries: 3,
TryTimeout: time.Second * 3,
RetryDelay: time.Second * 1,
MaxRetryDelay: time.Second * 3,
},
}
pl := azblob.NewPipeline(credential, po)
return pl, nil
}
func getServiceURL(pl pipeline.Pipeline, accountName string) (azblob.ServiceURL, error) {
URL, err := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net/", accountName))
if err != nil {
return azblob.ServiceURL{}, err
}
surl := azblob.NewServiceURL(*URL, pl)
return surl, nil
}
func getContainerURL(pl pipeline.Pipeline, accountName string, containerName string) (azblob.ContainerURL, error) {
URL, err := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net/%s", accountName, containerName))
if err != nil {
return azblob.ContainerURL{}, err
}
curl := azblob.NewContainerURL(*URL, pl)
return curl, nil
}
func getBlobURL(curl azblob.ContainerURL, blob string) azblob.BlockBlobURL {
return curl.NewBlockBlobURL(blob)
}
func listContainers(ctx context.Context, surl azblob.ServiceURL) ([]azblob.ContainerItem, error) {
lc, err := surl.ListContainersSegment(ctx, azblob.Marker{}, azblob.ListContainersSegmentOptions{})
if err != nil {
return nil, err
}
return lc.ContainerItems, nil
}
func containerExists(list []azblob.ContainerItem, containerName string) bool {
exists := false
for _, v := range list {
if containerName == v.Name {
exists = true
}
}
return exists
}