@@ -29,20 +29,24 @@ const (
2929
3030// Backend implements sotrage.Backend for Azure Blob Storage.
3131type Backend struct {
32- logger log.Logger
33- httpClient * http.Client
34- cfg Config
35- containerURL azblob.ContainerURL
36- sasToken string
37- // sharedKeyCredential * azblob.SharedKeyCredential
32+ logger log.Logger
33+ httpClient * http.Client
34+ cfg Config
35+ containerURL azblob.ContainerURL
36+ sasToken string
37+ sharedKeyCredential azblob.StorageAccountCredential
3838}
3939
4040// New creates an AzureBlob backend.
4141func New (l log.Logger , c Config ) (* Backend , error ) {
4242 var credential azblob.Credential
4343
4444 var err error
45-
45+ b := & Backend {
46+ logger : l ,
47+ cfg : c ,
48+ httpClient : http .DefaultClient ,
49+ }
4650 if c .AccountName == "" {
4751 return nil , errors .New ("azure account name is required" )
4852 }
@@ -57,6 +61,11 @@ func New(l log.Logger, c Config) (*Backend, error) {
5761 if err != nil {
5862 return nil , fmt .Errorf ("azure, invalid credentials, %w" , err )
5963 }
64+ var ok bool
65+ b .sharedKeyCredential , ok = credential .(azblob.StorageAccountCredential )
66+ if ! ok {
67+ return nil , errors .New ("azure, invalid credentials" )
68+ }
6069 }
6170
6271 // 3. Azurite has different URL pattern than production Azure Blob Storage.
@@ -98,13 +107,9 @@ func New(l log.Logger, c Config) (*Backend, error) {
98107 level .Error (l ).Log ("msg" , "container already exists" , "err" , err )
99108 }
100109 }
101-
102- return & Backend {
103- logger : l ,
104- cfg : c ,
105- containerURL : containerURL ,
106- httpClient : http .DefaultClient ,
107- }, nil
110+ b .containerURL = containerURL
111+ b .sasToken = c .SASToken
112+ return b , nil
108113}
109114
110115// Get writes downloaded content to the given writer.
@@ -207,14 +212,31 @@ func (b *Backend) generateSASTokenWithCDN(containerName, blobPath string) (strin
207212 containerName = strings .Replace (containerName , "\\ " , "/" , - 1 ) // Replace backslashes with forward slashes
208213 blobPath = strings .Replace (blobPath , "\\ " , "/" , - 1 ) // Replace backslashes with forward slashes
209214 }
210-
211215 parts := azblob.BlobURLParts {
212216 Scheme : "https" ,
213217 Host : b .cfg .CDNHost ,
214218 ContainerName : containerName ,
215219 BlobName : blobPath ,
216220 }
217- rawURL := parts .URL ()
218- rawURL .RawQuery = b .sasToken
221+ var rawURL url.URL
222+ if b .sasToken == "" {
223+ sasDefaultSignature := azblob.BlobSASSignatureValues {
224+ Protocol : azblob .SASProtocolHTTPS ,
225+ ExpiryTime : time .Now ().UTC ().Add (12 * time .Hour ),
226+ ContainerName : containerName ,
227+ BlobName : blobPath ,
228+ Permissions : azblob.BlobSASPermissions {Read : true , List : true }.String (),
229+ }
230+ sasQueryParams , err := sasDefaultSignature .NewSASQueryParameters (b .sharedKeyCredential )
231+ if err != nil {
232+ return "" , err
233+ }
234+ parts .SAS = sasQueryParams
235+ rawURL = parts .URL ()
236+ } else {
237+ rawURL = parts .URL ()
238+ rawURL .RawQuery = b .sasToken
239+ }
240+
219241 return rawURL .String (), nil
220242}
0 commit comments