-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
azure_storage.go
172 lines (155 loc) · 5.04 KB
/
azure_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
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package cloud
import (
"context"
"fmt"
"io"
"net/url"
"path"
"strings"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/contextutil"
"github.com/cockroachdb/errors"
)
func azureQueryParams(conf *roachpb.ExternalStorage_Azure) string {
q := make(url.Values)
if conf.AccountName != "" {
q.Set(AzureAccountNameParam, conf.AccountName)
}
if conf.AccountKey != "" {
q.Set(AzureAccountKeyParam, conf.AccountKey)
}
return q.Encode()
}
type azureStorage struct {
conf *roachpb.ExternalStorage_Azure
container azblob.ContainerURL
prefix string
settings *cluster.Settings
}
var _ ExternalStorage = &azureStorage{}
func makeAzureStorage(
conf *roachpb.ExternalStorage_Azure, settings *cluster.Settings,
) (ExternalStorage, error) {
if conf == nil {
return nil, errors.Errorf("azure upload requested but info missing")
}
credential, err := azblob.NewSharedKeyCredential(conf.AccountName, conf.AccountKey)
if err != nil {
return nil, errors.Wrap(err, "azure credential")
}
p := azblob.NewPipeline(credential, azblob.PipelineOptions{})
u, err := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", conf.AccountName))
if err != nil {
return nil, errors.Wrap(err, "azure: account name is not valid")
}
serviceURL := azblob.NewServiceURL(*u, p)
return &azureStorage{
conf: conf,
container: serviceURL.NewContainerURL(conf.Container),
prefix: conf.Prefix,
settings: settings,
}, nil
}
func (s *azureStorage) getBlob(basename string) azblob.BlockBlobURL {
name := path.Join(s.prefix, basename)
return s.container.NewBlockBlobURL(name)
}
func (s *azureStorage) Conf() roachpb.ExternalStorage {
return roachpb.ExternalStorage{
Provider: roachpb.ExternalStorageProvider_Azure,
AzureConfig: s.conf,
}
}
func (s *azureStorage) WriteFile(
ctx context.Context, basename string, content io.ReadSeeker,
) error {
err := contextutil.RunWithTimeout(ctx, "write azure file", timeoutSetting.Get(&s.settings.SV),
func(ctx context.Context) error {
blob := s.getBlob(basename)
_, err := blob.Upload(
ctx, content, azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{},
)
return err
})
return errors.Wrapf(err, "write file: %s", basename)
}
func (s *azureStorage) ReadFile(ctx context.Context, basename string) (io.ReadCloser, error) {
// https://github.com/cockroachdb/cockroach/issues/23859
blob := s.getBlob(basename)
get, err := blob.Download(ctx, 0, 0, azblob.BlobAccessConditions{}, false)
if err != nil {
return nil, errors.Wrap(err, "failed to create azure reader")
}
reader := get.Body(azblob.RetryReaderOptions{MaxRetryRequests: 3})
return reader, nil
}
func (s *azureStorage) ListFiles(ctx context.Context, patternSuffix string) ([]string, error) {
pattern := s.prefix
if patternSuffix != "" {
if containsGlob(s.prefix) {
return nil, errors.New("prefix cannot contain globs pattern when passing an explicit pattern")
}
pattern = path.Join(pattern, patternSuffix)
}
var fileList []string
response, err := s.container.ListBlobsFlatSegment(ctx,
azblob.Marker{},
azblob.ListBlobsSegmentOptions{Prefix: getPrefixBeforeWildcard(s.prefix)},
)
if err != nil {
return nil, errors.Wrap(err, "unable to list files for specified blob")
}
for _, blob := range response.Segment.BlobItems {
matches, err := path.Match(pattern, blob.Name)
if err != nil {
continue
}
if matches {
azureURL := url.URL{
Scheme: "azure",
Host: strings.TrimPrefix(s.container.URL().Path, "/"),
Path: blob.Name,
RawQuery: azureQueryParams(s.conf),
}
fileList = append(fileList, azureURL.String())
}
}
return fileList, nil
}
func (s *azureStorage) Delete(ctx context.Context, basename string) error {
err := contextutil.RunWithTimeout(ctx, "delete azure file", timeoutSetting.Get(&s.settings.SV),
func(ctx context.Context) error {
blob := s.getBlob(basename)
_, err := blob.Delete(ctx, azblob.DeleteSnapshotsOptionNone, azblob.BlobAccessConditions{})
return err
})
return errors.Wrap(err, "delete file")
}
func (s *azureStorage) Size(ctx context.Context, basename string) (int64, error) {
var props *azblob.BlobGetPropertiesResponse
err := contextutil.RunWithTimeout(ctx, "size azure file", timeoutSetting.Get(&s.settings.SV),
func(ctx context.Context) error {
blob := s.getBlob(basename)
var err error
props, err = blob.GetProperties(ctx, azblob.BlobAccessConditions{})
return err
})
if err != nil {
return 0, errors.Wrap(err, "get file properties")
}
return props.ContentLength(), nil
}
func (s *azureStorage) Close() error {
return nil
}