@@ -84,7 +84,7 @@ public IEnumerable<string> GetDirectories(string path)
84
84
{
85
85
ArgumentNullException . ThrowIfNull ( path ) ;
86
86
87
- return ListBlobs ( GetDirectoryPath ( path ) )
87
+ return ListBlobs ( path )
88
88
. Where ( x => x . IsPrefix )
89
89
. Select ( x => GetRelativePath ( $ "/{ x . Prefix } ") . Trim ( '/' ) ) ;
90
90
}
@@ -104,13 +104,9 @@ public void DeleteDirectory(string path, bool recursive)
104
104
{
105
105
ArgumentNullException . ThrowIfNull ( path ) ;
106
106
107
- foreach ( BlobHierarchyItem blob in ListBlobs ( GetDirectoryPath ( path ) ) )
107
+ foreach ( BlobHierarchyItem blob in ListBlobs ( path , true ) )
108
108
{
109
- if ( blob . IsPrefix )
110
- {
111
- DeleteDirectory ( blob . Prefix , true ) ;
112
- }
113
- else if ( blob . IsBlob )
109
+ if ( blob . IsBlob )
114
110
{
115
111
_container . GetBlobClient ( blob . Blob . Name ) . DeleteIfExists ( ) ;
116
112
}
@@ -123,7 +119,10 @@ public bool DirectoryExists(string path)
123
119
{
124
120
ArgumentNullException . ThrowIfNull ( path ) ;
125
121
126
- return GetBlobClient ( GetDirectoryPath ( path ) ) . Exists ( ) ;
122
+ // Try getting a single item/page
123
+ Page < BlobHierarchyItem > ? firstPage = ListBlobs ( path ) . AsPages ( pageSizeHint : 1 ) . FirstOrDefault ( ) ;
124
+
125
+ return firstPage ? . Values . Count > 0 ;
127
126
}
128
127
129
128
/// <inheritdoc />
@@ -148,7 +147,7 @@ public void AddFile(string path, Stream stream, bool overrideIfExists)
148
147
BlobClient blob = GetBlobClient ( path ) ;
149
148
if ( ! overrideIfExists && blob . Exists ( ) )
150
149
{
151
- throw new InvalidOperationException ( $ "A file at path '{ path } ' already exists") ;
150
+ throw new InvalidOperationException ( $ "A file at path '{ path } ' already exists. ") ;
152
151
}
153
152
154
153
var headers = new BlobHttpHeaders ( ) ;
@@ -180,7 +179,7 @@ public void AddFile(string path, string physicalPath, bool overrideIfExists = tr
180
179
BlobClient destinationBlob = GetBlobClient ( path ) ;
181
180
if ( ! overrideIfExists && destinationBlob . Exists ( ) )
182
181
{
183
- throw new InvalidOperationException ( $ "A file at path '{ path } ' already exists") ;
182
+ throw new InvalidOperationException ( $ "A file at path '{ path } ' already exists. ") ;
184
183
}
185
184
186
185
BlobClient sourceBlob = GetBlobClient ( physicalPath ) ;
@@ -220,7 +219,7 @@ public IEnumerable<string> GetFiles(string path, string? filter)
220
219
{
221
220
ArgumentNullException . ThrowIfNull ( path ) ;
222
221
223
- IEnumerable < string > files = ListBlobs ( GetDirectoryPath ( path ) ) . Where ( x => x . IsBlob ) . Select ( x => x . Blob . Name ) ;
222
+ IEnumerable < string > files = ListBlobs ( path ) . Where ( x => x . IsBlob ) . Select ( x => x . Blob . Name ) ;
224
223
if ( ! string . IsNullOrEmpty ( filter ) && filter != "*.*" )
225
224
{
226
225
// TODO: Might be better to use a globbing library
@@ -331,7 +330,7 @@ public BlobClient GetBlobClient(string path)
331
330
{
332
331
ArgumentNullException . ThrowIfNull ( path ) ;
333
332
334
- return _container . GetBlobClient ( GetBlobPath ( path ) ) ;
333
+ return _container . GetBlobClient ( GetBlobName ( path ) ) ;
335
334
}
336
335
337
336
/// <inheritdoc />
@@ -348,17 +347,15 @@ private static string GetRequestRootPath(AzureBlobFileSystemOptions options, IHo
348
347
private static string EnsureUrlSeparatorChar ( string path )
349
348
=> path . Replace ( "\\ " , "/" , StringComparison . InvariantCultureIgnoreCase ) ;
350
349
351
- private string GetDirectoryPath ( string fullPathOrUrl )
350
+ private Pageable < BlobHierarchyItem > ListBlobs ( string path , bool recursive = false )
352
351
{
353
- var path = GetFullPath ( fullPathOrUrl ) ;
352
+ string ? delimiter = recursive ? null : "/" ;
353
+ string prefix = GetFullPath ( path ) . EnsureEndsWith ( '/' ) ;
354
354
355
- return path . Length == 0 ? path : path . EnsureEndsWith ( '/' ) ;
355
+ return _container . GetBlobsByHierarchy ( delimiter : delimiter , prefix : prefix ) ;
356
356
}
357
357
358
- private Pageable < BlobHierarchyItem > ListBlobs ( string path )
359
- => _container . GetBlobsByHierarchy ( prefix : path ) ;
360
-
361
- private string GetBlobPath ( string path )
358
+ private string GetBlobName ( string path )
362
359
{
363
360
path = EnsureUrlSeparatorChar ( path ) ;
364
361
0 commit comments