@@ -23,30 +23,40 @@ const (
23
23
// DefaultBlobMaxRetryRequests Default value for Azure Blob Storage Max Retry Requests.
24
24
DefaultBlobMaxRetryRequests = 4
25
25
26
- defaultBufferSize = 3 * 1024 * 1024
26
+ defaultBufferSize = 4 * 1024 * 1024
27
27
defaultMaxBuffers = 4
28
28
)
29
29
30
30
// Backend implements sotrage.Backend for Azure Blob Storage.
31
31
type Backend struct {
32
- logger log.Logger
33
- httpClient * http.Client
34
- cfg Config
35
- containerURL azblob.ContainerURL
36
- sharedKeyCredential * azblob.SharedKeyCredential
32
+ logger log.Logger
33
+ httpClient * http.Client
34
+ cfg Config
35
+ containerURL azblob.ContainerURL
36
+ sasToken string
37
+ // sharedKeyCredential *azblob.SharedKeyCredential
37
38
}
38
39
39
40
// New creates an AzureBlob backend.
40
41
func New (l log.Logger , c Config ) (* Backend , error ) {
41
- // 1. From the Azure portal, get your storage account name and key and set environment variables.
42
- if c .AccountName == "" || c .AccountKey == "" {
43
- return nil , errors .New ("either the AZURE_ACCOUNT_NAME or AZURE_ACCOUNT_KEY environment variable is not set" )
44
- }
42
+ var credential azblob.Credential
43
+
44
+ var err error
45
45
46
+ if c .AccountName == "" {
47
+ return nil , errors .New ("azure account name is required" )
48
+ }
46
49
// 2. Create a default request pipeline using your storage account name and account key.
47
- credential , err := azblob .NewSharedKeyCredential (c .AccountName , c .AccountKey )
48
- if err != nil {
49
- return nil , fmt .Errorf ("azure, invalid credentials, %w" , err )
50
+ if c .SASToken != "" {
51
+ level .Info (l ).Log ("msg" , "using token for cache operation" )
52
+ credential = azblob .NewAnonymousCredential ()
53
+ } else if c .AccountKey == "" {
54
+ return nil , errors .New ("azure account key is required" )
55
+ } else if c .AccountKey != "" {
56
+ credential , err = azblob .NewSharedKeyCredential (c .AccountName , c .AccountKey )
57
+ if err != nil {
58
+ return nil , fmt .Errorf ("azure, invalid credentials, %w" , err )
59
+ }
50
60
}
51
61
52
62
// 3. Azurite has different URL pattern than production Azure Blob Storage.
@@ -57,6 +67,10 @@ func New(l log.Logger, c Config) (*Backend, error) {
57
67
blobURL , err = url .Parse (fmt .Sprintf ("https://%s.%s/%s" , c .AccountName , c .BlobStorageURL , c .ContainerName ))
58
68
}
59
69
70
+ if c .SASToken != "" {
71
+ blobURL .RawQuery = c .SASToken
72
+ }
73
+
60
74
if err != nil {
61
75
level .Error (l ).Log ("msg" , "can't create url with : " + err .Error ())
62
76
}
@@ -85,9 +99,11 @@ func New(l log.Logger, c Config) (*Backend, error) {
85
99
}
86
100
}
87
101
88
- return & Backend {logger : l , cfg : c , containerURL : containerURL ,
89
- httpClient : http .DefaultClient ,
90
- sharedKeyCredential : credential ,
102
+ return & Backend {
103
+ logger : l ,
104
+ cfg : c ,
105
+ containerURL : containerURL ,
106
+ httpClient : http .DefaultClient ,
91
107
}, nil
92
108
}
93
109
@@ -127,8 +143,8 @@ func (b *Backend) Get(ctx context.Context, p string, w io.Writer) error {
127
143
128
144
} else {
129
145
blobURL := b .containerURL .NewBlockBlobURL (p )
130
- // nolint: lll
131
- resp , err := blobURL . Download ( ctx , 0 , azblob . CountToEnd , azblob.BlobAccessConditions {}, false , azblob.ClientProvidedKeyOptions {})
146
+ resp , err := blobURL . Download ( ctx , 0 , azblob . CountToEnd ,
147
+ azblob.BlobAccessConditions {}, false , azblob.ClientProvidedKeyOptions {})
132
148
if err != nil {
133
149
errCh <- fmt .Errorf ("get the object, %w" , err )
134
150
@@ -187,31 +203,18 @@ func (b *Backend) Exists(ctx context.Context, p string) (bool, error) {
187
203
188
204
// Exists checks if path already exists.
189
205
func (b * Backend ) generateSASTokenWithCDN (containerName , blobPath string ) (string , error ) {
190
-
191
206
if runtime .GOOS == "windows" {
192
207
containerName = strings .Replace (containerName , "\\ " , "/" , - 1 ) // Replace backslashes with forward slashes
193
208
blobPath = strings .Replace (blobPath , "\\ " , "/" , - 1 ) // Replace backslashes with forward slashes
194
209
}
195
210
196
- sasDefaultSignature := azblob.BlobSASSignatureValues {
197
- Protocol : azblob .SASProtocolHTTPS ,
198
- ExpiryTime : time .Now ().UTC ().Add (12 * time .Hour ),
199
- ContainerName : containerName ,
200
- BlobName : blobPath ,
201
- Permissions : azblob.BlobSASPermissions {Read : true , List : true }.String (),
202
- }
203
- sasQueryParams , err := sasDefaultSignature .NewSASQueryParameters (b .sharedKeyCredential )
204
- if err != nil {
205
- return "" , err
206
- }
207
211
parts := azblob.BlobURLParts {
208
212
Scheme : "https" ,
209
213
Host : b .cfg .CDNHost ,
210
214
ContainerName : containerName ,
211
215
BlobName : blobPath ,
212
- SAS : sasQueryParams ,
213
216
}
214
-
215
217
rawURL := parts .URL ()
218
+ rawURL .RawQuery = b .sasToken
216
219
return rawURL .String (), nil
217
220
}
0 commit comments