77 "fmt"
88 "io"
99 "log/slog"
10+ "net/http"
1011 "os"
1112 "strconv"
1213 "strings"
@@ -104,9 +105,45 @@ func createContext(dsc DefaultStorageClient) (context.Context, context.CancelFun
104105}
105106
106107type DefaultStorageClient struct {
107- credential * azblob.SharedKeyCredential
108- serviceURL string
109- storageConfig config.AZStorageConfig
108+ credential * azblob.SharedKeyCredential
109+ serviceURL string
110+ storageConfig config.AZStorageConfig
111+ httpRequestTimeout time.Duration
112+ }
113+
114+ // clientOptions returns azblob.ClientOptions with a timeout-configured http.Client,
115+ // or nil when no http_request_timeout is set (use SDK defaults).
116+ func (dsc DefaultStorageClient ) blockblobClientOptions () * blockblob.ClientOptions {
117+ if dsc .httpRequestTimeout == 0 {
118+ return nil
119+ }
120+ return & blockblob.ClientOptions {
121+ ClientOptions : azcore.ClientOptions {
122+ Transport : & http.Client {Timeout : dsc .httpRequestTimeout },
123+ },
124+ }
125+ }
126+
127+ func (dsc DefaultStorageClient ) blobClientOptions () * azBlob.ClientOptions {
128+ if dsc .httpRequestTimeout == 0 {
129+ return nil
130+ }
131+ return & azBlob.ClientOptions {
132+ ClientOptions : azcore.ClientOptions {
133+ Transport : & http.Client {Timeout : dsc .httpRequestTimeout },
134+ },
135+ }
136+ }
137+
138+ func (dsc DefaultStorageClient ) containerClientOptions () * azContainer.ClientOptions {
139+ if dsc .httpRequestTimeout == 0 {
140+ return nil
141+ }
142+ return & azContainer.ClientOptions {
143+ ClientOptions : azcore.ClientOptions {
144+ Transport : & http.Client {Timeout : dsc .httpRequestTimeout },
145+ },
146+ }
110147}
111148
112149func NewStorageClient (storageConfig config.AZStorageConfig ) (StorageClient , error ) {
@@ -115,9 +152,19 @@ func NewStorageClient(storageConfig config.AZStorageConfig) (StorageClient, erro
115152 return nil , err
116153 }
117154
155+ httpRequestTimeout , err := storageConfig .HTTPRequestTimeoutValue ()
156+ if err != nil {
157+ return nil , err
158+ }
159+
118160 serviceURL := fmt .Sprintf ("https://%s.%s/%s" , storageConfig .AccountName , storageConfig .StorageEndpoint (), storageConfig .ContainerName )
119161
120- return DefaultStorageClient {credential : credential , serviceURL : serviceURL , storageConfig : storageConfig }, nil
162+ return DefaultStorageClient {
163+ credential : credential ,
164+ serviceURL : serviceURL ,
165+ storageConfig : storageConfig ,
166+ httpRequestTimeout : httpRequestTimeout ,
167+ }, nil
121168}
122169
123170func (dsc DefaultStorageClient ) Upload (
@@ -138,7 +185,7 @@ func (dsc DefaultStorageClient) Upload(
138185 }
139186 defer cancel ()
140187
141- client , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , nil )
188+ client , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , dsc . blockblobClientOptions () )
142189 if err != nil {
143190 return nil , err
144191 }
@@ -173,7 +220,7 @@ func (dsc DefaultStorageClient) UploadStream(
173220 }
174221 defer cancel ()
175222
176- client , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , nil )
223+ client , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , dsc . blockblobClientOptions () )
177224 if err != nil {
178225 return err
179226 }
@@ -196,7 +243,7 @@ func (dsc DefaultStorageClient) Download(
196243) error {
197244 blobURL := fmt .Sprintf ("%s/%s" , dsc .serviceURL , source )
198245 slog .Info ("Downloading blob from container" , "container" , dsc .storageConfig .ContainerName , "blob" , source , "local_file" , dest .Name ())
199- client , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , nil )
246+ client , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , dsc . blockblobClientOptions () )
200247 if err != nil {
201248 return err
202249 }
@@ -226,7 +273,7 @@ func (dsc DefaultStorageClient) Copy(
226273 srcURL := fmt .Sprintf ("%s/%s" , dsc .serviceURL , srcBlob )
227274 destURL := fmt .Sprintf ("%s/%s" , dsc .serviceURL , destBlob )
228275
229- destClient , err := blockblob .NewClientWithSharedKeyCredential (destURL , dsc .credential , nil )
276+ destClient , err := blockblob .NewClientWithSharedKeyCredential (destURL , dsc .credential , dsc . blockblobClientOptions () )
230277 if err != nil {
231278 return fmt .Errorf ("failed to create destination client: %w" , err )
232279 }
@@ -268,7 +315,7 @@ func (dsc DefaultStorageClient) Delete(
268315 blobURL := fmt .Sprintf ("%s/%s" , dsc .serviceURL , dest )
269316
270317 slog .Info ("Deleting blob from container" , "container" , dsc .storageConfig .ContainerName , "blob" , dest , "url" , blobURL )
271- client , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , nil )
318+ client , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , dsc . blockblobClientOptions () )
272319 if err != nil {
273320 return err
274321 }
@@ -295,7 +342,7 @@ func (dsc DefaultStorageClient) DeleteRecursive(
295342 slog .Info ("Deleting all blobs in container" , "container" , dsc .storageConfig .ContainerName )
296343 }
297344
298- containerClient , err := azContainer .NewClientWithSharedKeyCredential (dsc .serviceURL , dsc .credential , nil )
345+ containerClient , err := azContainer .NewClientWithSharedKeyCredential (dsc .serviceURL , dsc .credential , dsc . containerClientOptions () )
299346 if err != nil {
300347 return fmt .Errorf ("failed to create container client: %w" , err )
301348 }
@@ -315,7 +362,7 @@ func (dsc DefaultStorageClient) DeleteRecursive(
315362
316363 for _ , blob := range resp .Segment .BlobItems {
317364 blobURL := fmt .Sprintf ("%s/%s" , dsc .serviceURL , * blob .Name )
318- blobClient , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , nil )
365+ blobClient , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , dsc . blockblobClientOptions () )
319366 if err != nil {
320367 slog .Error ("Failed to create blob client" , "blob" , * blob .Name , "error" , err )
321368 continue
@@ -338,7 +385,7 @@ func (dsc DefaultStorageClient) Exists(
338385 blobURL := fmt .Sprintf ("%s/%s" , dsc .serviceURL , dest )
339386
340387 slog .Info ("Checking if blob exists" , "container" , dsc .storageConfig .ContainerName , "blob" , dest , "url" , blobURL )
341- client , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , nil )
388+ client , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , dsc . blockblobClientOptions () )
342389 if err != nil {
343390 return false , err
344391 }
@@ -365,7 +412,7 @@ func (dsc DefaultStorageClient) SignedUrl(
365412 blobURL := fmt .Sprintf ("%s/%s" , dsc .serviceURL , dest )
366413
367414 slog .Info ("Generating SAS URL for blob" , "container" , dsc .storageConfig .ContainerName , "blob" , dest , "request_type" , requestType , "expiration" , expiration )
368- client , err := azBlob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , nil )
415+ client , err := azBlob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , dsc . blobClientOptions () )
369416 if err != nil {
370417 return "" , err
371418 }
@@ -398,7 +445,7 @@ func (dsc DefaultStorageClient) List(
398445 slog .Info ("Listing blobs in container" , "container" , dsc .storageConfig .ContainerName )
399446 }
400447
401- client , err := azContainer .NewClientWithSharedKeyCredential (dsc .serviceURL , dsc .credential , nil )
448+ client , err := azContainer .NewClientWithSharedKeyCredential (dsc .serviceURL , dsc .credential , dsc . containerClientOptions () )
402449 if err != nil {
403450 return nil , fmt .Errorf ("failed to create container client: %w" , err )
404451 }
@@ -437,7 +484,7 @@ func (dsc DefaultStorageClient) Properties(
437484 blobURL := fmt .Sprintf ("%s/%s" , dsc .serviceURL , dest )
438485
439486 slog .Info ("Getting properties for blob" , "container" , dsc .storageConfig .ContainerName , "blob" , dest , "url" , blobURL )
440- client , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , nil )
487+ client , err := blockblob .NewClientWithSharedKeyCredential (blobURL , dsc .credential , dsc . blockblobClientOptions () )
441488 if err != nil {
442489 return err
443490 }
@@ -469,7 +516,7 @@ func (dsc DefaultStorageClient) Properties(
469516func (dsc DefaultStorageClient ) EnsureContainerExists () error {
470517 slog .Info ("Ensuring container exists" , "container" , dsc .storageConfig .ContainerName )
471518
472- containerClient , err := azContainer .NewClientWithSharedKeyCredential (dsc .serviceURL , dsc .credential , nil )
519+ containerClient , err := azContainer .NewClientWithSharedKeyCredential (dsc .serviceURL , dsc .credential , dsc . containerClientOptions () )
473520 if err != nil {
474521 return fmt .Errorf ("failed to create container client: %w" , err )
475522 }
0 commit comments