This repository has been archived by the owner on Jun 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob.go
140 lines (120 loc) · 3.95 KB
/
blob.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
package main
import (
"context"
"fmt"
"net/url"
"strings"
"time"
"github.com/Azure/azure-storage-blob-go/azblob"
)
func parseConnectionString(s string) (*string, *string, error) {
if len(s) < 1 {
return nil, nil, fmt.Errorf("empty connection string.")
}
var account *string
var key *string
for _, kv := range strings.Split(s, ";") {
kv := strings.SplitN(kv, "=", 2)
k, v := kv[0], kv[1]
switch k {
case "AccountName":
account = &v
case "AccountKey":
key = &v
}
}
if account == nil || key == nil {
return nil, nil, fmt.Errorf("no AccountName or AccountKey")
}
return account, key, nil
}
func newAzblobCredential(s string) (*azblob.SharedKeyCredential, error) {
account, key, err := parseConnectionString(s)
if err != nil {
return nil, err
}
cred, err := azblob.NewSharedKeyCredential(*account, *key)
if err != nil {
return nil, err
}
return cred, nil
}
const defaultContainerTemplate = "https://%s.blob.core.windows.net/%s"
func ensureContainer(context context.Context, env env, cred *azblob.SharedKeyCredential, container string) (*azblob.ContainerURL, error) {
urlTemplate := env.containerTemplate()
if urlTemplate == nil {
t := defaultContainerTemplate
urlTemplate = &t
}
rawurl, err := url.Parse(fmt.Sprintf(*urlTemplate, cred.AccountName(), container))
if err != nil {
return nil, err
}
conurl := azblob.NewContainerURL(*rawurl, azblob.NewPipeline(cred, azblob.PipelineOptions{}))
_, err = conurl.Create(context, azblob.Metadata{}, azblob.PublicAccessNone)
if err != nil {
if err, ok := err.(azblob.StorageError); ok && err.ServiceCode() == azblob.ServiceCodeContainerAlreadyExists {
// pass
} else {
return nil, err
}
}
return &conurl, nil
}
func newBlobUrlWithSas(env env, cred azblob.StorageAccountCredential, blob *azblob.BlobURL, expireInMinutes int, read bool, write bool) (*azblob.BlobURL, error) {
bloburl := blob.URL()
parts := azblob.NewBlobURLParts(bloburl)
sas, err := azblob.BlobSASSignatureValues{
Protocol: azblob.SASProtocolHTTPS,
ExpiryTime: env.now().UTC().Add(15 * time.Minute),
ContainerName: parts.ContainerName,
BlobName: parts.BlobName,
Permissions: azblob.BlobSASPermissions{Read: read, Write: write}.String(),
}.NewSASQueryParameters(cred)
if err != nil {
return nil, err
}
bloburl.RawQuery = sas.Encode()
u := azblob.NewBlobURL(bloburl, azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{}))
return &u, nil
}
func existsBlob(context context.Context, blob *azblob.BlobURL) (bool, error) {
_, err := blob.GetProperties(context, azblob.BlobAccessConditions{}, azblob.ClientProvidedKeyOptions{})
if err != nil {
if err, ok := err.(azblob.StorageError); ok && err.ServiceCode() == azblob.ServiceCodeBlobNotFound {
return false, nil
}
return false, err
}
return true, nil
}
func newBlobUrlFromSas(urlwithsas string) (*azblob.BlobURL, error) {
rawurl, err := url.Parse(urlwithsas)
if err != nil {
return nil, err
}
result := azblob.NewBlobURL(*rawurl, azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{}))
return &result, nil
}
func touchIfAbsent(context context.Context, blob *azblob.BlobURL) (*azblob.CommonResponse, error) {
created, err := azblob.UploadBufferToBlockBlob(context, []byte{}, blob.ToBlockBlobURL(), azblob.UploadToBlockBlobOptions{
AccessConditions: azblob.BlobAccessConditions{
ModifiedAccessConditions: azblob.ModifiedAccessConditions{
// fail if exists
IfNoneMatch: azblob.ETagAny,
},
},
})
return &created, err
}
func putIfUnmodified(context context.Context, blob *azblob.BlobURL, content string, previous *azblob.CommonResponse) error {
_, err := azblob.UploadBufferToBlockBlob(context, []byte(content), blob.ToBlockBlobURL(), azblob.UploadToBlockBlobOptions{
AccessConditions: azblob.BlobAccessConditions{
ModifiedAccessConditions: azblob.ModifiedAccessConditions{
// fail if modified after created
IfMatch: (*previous).ETag(),
},
},
})
return err
}