@@ -106,7 +106,7 @@ public override async Task<Result<Stream>> GetStreamAsync(string fileName, Cance
106106 await EnsureContainerExist ( cancellationToken ) ;
107107 cancellationToken . ThrowIfCancellationRequested ( ) ;
108108
109- var filePath = GetPathFromOptions ( new DownloadOptions { FileName = fileName } ) ;
109+ var filePath = GetPathFromOptions ( new DownloadOptions { FileName = fileName } , createDirectories : false ) ;
110110 cancellationToken . ThrowIfCancellationRequested ( ) ;
111111
112112 return File . Exists ( filePath )
@@ -205,7 +205,7 @@ protected override async Task<Result<LocalFile>> DownloadInternalAsync(LocalFile
205205 await EnsureContainerExist ( cancellationToken ) ;
206206 cancellationToken . ThrowIfCancellationRequested ( ) ;
207207
208- var sourcePath = GetPathFromOptions ( options ) ;
208+ var sourcePath = GetPathFromOptions ( options , createDirectories : false ) ;
209209 cancellationToken . ThrowIfCancellationRequested ( ) ;
210210
211211 if ( ! File . Exists ( sourcePath ) )
@@ -248,7 +248,7 @@ protected override async Task<Result<bool>> DeleteInternalAsync(DeleteOptions op
248248 await EnsureContainerExist ( cancellationToken ) ;
249249 cancellationToken . ThrowIfCancellationRequested ( ) ;
250250
251- var filePath = GetPathFromOptions ( options ) ;
251+ var filePath = GetPathFromOptions ( options , createDirectories : false ) ;
252252 cancellationToken . ThrowIfCancellationRequested ( ) ;
253253
254254 if ( ! File . Exists ( filePath ) )
@@ -270,7 +270,7 @@ protected override async Task<Result<bool>> ExistsInternalAsync(ExistOptions opt
270270 await EnsureContainerExist ( cancellationToken ) ;
271271 cancellationToken . ThrowIfCancellationRequested ( ) ;
272272
273- var filePath = GetPathFromOptions ( options ) ;
273+ var filePath = GetPathFromOptions ( options , createDirectories : false ) ;
274274 cancellationToken . ThrowIfCancellationRequested ( ) ;
275275
276276 return Result < bool > . Succeed ( File . Exists ( filePath ) ) ;
@@ -289,7 +289,7 @@ protected override async Task<Result<BlobMetadata>> GetBlobMetadataInternalAsync
289289 await EnsureContainerExist ( cancellationToken ) ;
290290 cancellationToken . ThrowIfCancellationRequested ( ) ;
291291
292- var filePath = GetPathFromOptions ( options ) ;
292+ var filePath = GetPathFromOptions ( options , createDirectories : false ) ;
293293 cancellationToken . ThrowIfCancellationRequested ( ) ;
294294
295295 var fileInfo = new FileInfo ( filePath ) ;
@@ -323,7 +323,7 @@ protected override async Task<Result> SetLegalHoldInternalAsync(bool hasLegalHol
323323 {
324324 try
325325 {
326- var filePath = GetPathFromOptions ( options ) ;
326+ var filePath = GetPathFromOptions ( options , createDirectories : false ) ;
327327 await EnsureContainerExist ( cancellationToken ) ;
328328 cancellationToken . ThrowIfCancellationRequested ( ) ;
329329
@@ -362,7 +362,7 @@ protected override async Task<Result<bool>> HasLegalHoldInternalAsync(LegalHoldO
362362 {
363363 try
364364 {
365- var filePath = GetPathFromOptions ( options ) ;
365+ var filePath = GetPathFromOptions ( options , createDirectories : false ) ;
366366 await EnsureContainerExist ( cancellationToken ) ;
367367 cancellationToken . ThrowIfCancellationRequested ( ) ;
368368
@@ -374,7 +374,7 @@ protected override async Task<Result<bool>> HasLegalHoldInternalAsync(LegalHoldO
374374 }
375375 }
376376
377- private string GetPathFromOptions ( BaseOptions options )
377+ private string GetPathFromOptions ( BaseOptions options , bool createDirectories = true )
378378 {
379379 if ( string . IsNullOrWhiteSpace ( options . FileName ) )
380380 throw new ArgumentException ( "File name cannot be null or empty" , nameof ( options ) ) ;
@@ -389,7 +389,10 @@ private string GetPathFromOptions(BaseOptions options)
389389 ? SanitizeDirectory ( combinedDirectory )
390390 : null ;
391391
392- if ( sanitizedDirectory is not null )
392+ if ( createDirectories && ! StorageOptions . CreateContainerIfNotExists && ! Directory . Exists ( StorageClient ) )
393+ throw new DirectoryNotFoundException ( "The configured storage container does not exist and automatic creation is disabled." ) ;
394+
395+ if ( createDirectories && sanitizedDirectory is not null )
393396 {
394397 EnsureDirectoryExist ( sanitizedDirectory ) ;
395398 }
@@ -408,7 +411,8 @@ private string GetPathFromOptions(BaseOptions options)
408411 throw new UnauthorizedAccessException ( $ "Access to path '{ options . FileName } ' is denied. Path traversal detected.") ;
409412 }
410413
411- EnsureDirectoryExist ( Path . GetDirectoryName ( fullPath ) ! ) ;
414+ if ( createDirectories )
415+ EnsureDirectoryExist ( Path . GetDirectoryName ( fullPath ) ! ) ;
412416 return fullPath ;
413417 }
414418
0 commit comments