@@ -19,7 +19,7 @@ import (
19
19
const (
20
20
AnonymousAccess = 0
21
21
ServicePrincipalAccess = 1
22
- ManagedIdentityAccess = 2
22
+ AzureCLIAccess = 2
23
23
)
24
24
25
25
type AzureBlobStorage struct {
@@ -36,13 +36,13 @@ func (abs *AzureBlobStorage) Upload(
36
36
37
37
localFile , err := os .OpenFile (localFileName , os .O_RDONLY , 0 )
38
38
if err != nil {
39
- return fmt .Errorf ("Failed to open local file for upload:\n %w" , err )
39
+ return fmt .Errorf ("failed to open local file for upload:\n %w" , err )
40
40
}
41
41
defer localFile .Close ()
42
42
43
43
_ , err = abs .theClient .UploadFile (ctx , containerName , blobName , localFile , nil )
44
44
if err != nil {
45
- return fmt .Errorf ("Failed to upload local file to blob:\n %w" , err )
45
+ return fmt .Errorf ("failed to upload local file to blob:\n %w" , err )
46
46
}
47
47
48
48
uploadEndTime := time .Now ()
@@ -77,7 +77,7 @@ func (abs *AzureBlobStorage) Download(
77
77
78
78
_ , err = abs .theClient .DownloadFile (ctx , containerName , blobName , localFile , nil )
79
79
if err != nil {
80
- return fmt .Errorf ("Failed to download blob to local file:\n %w" , err )
80
+ return fmt .Errorf ("failed to download blob to local file:\n %w" , err )
81
81
}
82
82
83
83
downloadEndTime := time .Now ()
@@ -94,7 +94,7 @@ func (abs *AzureBlobStorage) Delete(
94
94
deleteStartTime := time .Now ()
95
95
_ , err = abs .theClient .DeleteBlob (ctx , containerName , blobName , nil )
96
96
if err != nil {
97
- return fmt .Errorf ("Failed to delete blob:\n %w" , err )
97
+ return fmt .Errorf ("failed to delete blob:\n %w" , err )
98
98
}
99
99
deleteEndTime := time .Now ()
100
100
logger .Log .Infof (" delete time: %v" , deleteEndTime .Sub (deleteStartTime ))
@@ -103,49 +103,45 @@ func (abs *AzureBlobStorage) Delete(
103
103
}
104
104
105
105
func Create (tenantId string , userName string , password string , storageAccount string , authenticationType int ) (abs * AzureBlobStorage , err error ) {
106
-
107
106
url := "https://" + storageAccount + ".blob.core.windows.net/"
108
107
109
108
abs = & AzureBlobStorage {}
110
109
111
- if authenticationType == AnonymousAccess {
112
-
110
+ switch authenticationType {
111
+ case AnonymousAccess :
113
112
abs .theClient , err = azblob .NewClientWithNoCredential (url , nil )
114
113
if err != nil {
115
- return nil , fmt .Errorf ("Unable to init azure blob storage read-only client:\n %w" , err )
114
+ return nil , fmt .Errorf ("unable to init azure blob storage read-only client:\n %w" , err )
116
115
}
117
116
118
117
return abs , nil
119
118
120
- } else if authenticationType == ServicePrincipalAccess {
121
-
119
+ case ServicePrincipalAccess :
122
120
credential , err := azidentity .NewClientSecretCredential (tenantId , userName , password , nil )
123
121
if err != nil {
124
- return nil , fmt .Errorf ("Unable to init azure service principal identity:\n %w" , err )
122
+ return nil , fmt .Errorf ("unable to init azure service principal identity:\n %w" , err )
125
123
}
126
124
127
125
abs .theClient , err = azblob .NewClient (url , credential , nil )
128
126
if err != nil {
129
- return nil , fmt .Errorf ("Unable to init azure blob storage read-write client:\n %w" , err )
127
+ return nil , fmt .Errorf ("unable to init azure blob storage read-write client:\n %w" , err )
130
128
}
131
129
132
130
return abs , nil
133
131
134
- } else if authenticationType == ManagedIdentityAccess {
135
-
136
- credential , err := azidentity .NewDefaultAzureCredential (nil )
132
+ case AzureCLIAccess :
133
+ credential , err := azidentity .NewAzureCLICredential (nil )
137
134
if err != nil {
138
- return nil , fmt .Errorf ("Unable to init azure managed identity:\n %w" , err )
135
+ return nil , fmt .Errorf ("unable to init azure managed identity:\n %w" , err )
139
136
}
140
137
141
138
abs .theClient , err = azblob .NewClient (url , credential , nil )
142
139
if err != nil {
143
- return nil , fmt .Errorf ("Unable to init azure blob storage read-write client:\n %w" , err )
140
+ return nil , fmt .Errorf ("unable to init azure blob storage read-write client:\n %w" , err )
144
141
}
145
142
146
143
return abs , nil
147
-
148
144
}
149
145
150
- return nil , errors .New ("Unknown authentication type. " )
146
+ return nil , errors .New ("unknown authentication type" )
151
147
}
0 commit comments