forked from chartmuseum/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
microsoft.go
190 lines (150 loc) · 4.95 KB
/
microsoft.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
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package storage
import (
"errors"
"io/ioutil"
pathutil "path"
"time"
"os"
microsoft_storage "github.com/Azure/azure-sdk-for-go/storage"
)
const (
maxChunkSize = 4 * 1024 * 1024
)
// MicrosoftBlobBackend is a storage backend for Microsoft Azure Blob Storage
type MicrosoftBlobBackend struct {
Prefix string
Container *microsoft_storage.Container
}
// NewMicrosoftBlobBackend creates a new instance of MicrosoftBlobBackend
func NewMicrosoftBlobBackend(container string, prefix string) *MicrosoftBlobBackend {
// From the Azure portal, get your storage account name and key and set environment variables.
accountName, accountKey := os.Getenv("AZURE_STORAGE_ACCOUNT"), os.Getenv("AZURE_STORAGE_ACCESS_KEY")
var serviceBaseURL, apiVersion string
if serviceBaseURL = os.Getenv("AZURE_BASE_URL"); serviceBaseURL == "" {
serviceBaseURL = microsoft_storage.DefaultBaseURL
}
if apiVersion = os.Getenv("AZURE_API_VERSION"); apiVersion == "" {
apiVersion = microsoft_storage.DefaultAPIVersion
}
if len(accountName) == 0 || len(accountKey) == 0 {
panic("Either the AZURE_STORAGE_ACCOUNT or AZURE_STORAGE_ACCESS_KEY environment variable is not set")
}
client, err := microsoft_storage.NewClient(accountName, accountKey, serviceBaseURL, apiVersion, true)
if err != nil {
panic(err)
}
blobClient := client.GetBlobService()
containerRef := blobClient.GetContainerReference(container)
b := &MicrosoftBlobBackend{
Prefix: prefix,
Container: containerRef,
}
return b
}
// ListObjects lists all objects in Microsoft Azure Blob Storage container
func (b MicrosoftBlobBackend) ListObjects(prefix string) ([]Object, error) {
var objects []Object
if b.Container == nil {
return objects, errors.New("Unable to obtain a container reference.")
}
var params microsoft_storage.ListBlobsParameters
prefix = pathutil.Join(b.Prefix, prefix)
params.Prefix = prefix
for {
response, err := b.Container.ListBlobs(params)
if err != nil {
return objects, err
}
for _, blob := range response.Blobs {
path := removePrefixFromObjectPath(prefix, blob.Name)
if objectPathIsInvalid(path) {
continue
}
object := Object{
Path: path,
Content: []byte{},
LastModified: time.Time(blob.Properties.LastModified),
}
objects = append(objects, object)
}
if response.NextMarker == "" {
break
}
params.Marker = response.NextMarker
}
return objects, nil
}
// GetObject retrieves an object from Microsoft Azure Blob Storage, at path
func (b MicrosoftBlobBackend) GetObject(path string) (Object, error) {
var object Object
object.Path = path
if b.Container == nil {
return object, errors.New("Unable to obtain a container reference.")
}
var content []byte
blobReference := b.Container.GetBlobReference(pathutil.Join(b.Prefix, path))
exists, err := blobReference.Exists()
if err != nil {
return object, err
}
if !exists {
return object, errors.New("Object does not exist.")
}
readCloser, err := blobReference.Get(nil)
if err != nil {
return object, err
}
content, err = ioutil.ReadAll(readCloser)
if err != nil {
return object, err
}
object.Content = content
err = blobReference.GetProperties(nil)
object.LastModified = time.Time(blobReference.Properties.LastModified)
return object, nil
}
// PutObject uploads an object to Microsoft Azure Blob Storage container, at path
func (b MicrosoftBlobBackend) PutObject(path string, content []byte) error {
if b.Container == nil {
return errors.New("Unable to obtain a container reference.")
}
blobReference := b.Container.GetBlobReference(pathutil.Join(b.Prefix, path))
err := blobReference.PutAppendBlob(nil)
if err == nil {
err = writeToBlob(content, blobReference)
}
return err
}
func writeToBlob(content []byte, blobRef *microsoft_storage.Blob) error {
for offset := 0; offset < len(content); offset += maxChunkSize {
chunkSize := maxChunkSize
if offset+chunkSize > len(content) {
chunkSize = len(content) - offset
}
if err := blobRef.AppendBlock(content[offset:offset+chunkSize], nil); err != nil {
return err
}
}
return nil
}
// DeleteObject removes an object from Microsoft Azure Blob Storage container, at path
func (b MicrosoftBlobBackend) DeleteObject(path string) error {
if b.Container == nil {
return errors.New("Unable to obtain a container reference.")
}
blobReference := b.Container.GetBlobReference(pathutil.Join(b.Prefix, path))
_, err := blobReference.DeleteIfExists(nil)
return err
}