55using System ;
66using System . Collections . Generic ;
77using System . IO ;
8- using System . Linq ;
98using System . Threading ;
109using System . Threading . Tasks ;
1110
@@ -66,7 +65,7 @@ public Task<IReadOnlyList<IUnixFileSystemEntry>> GetEntriesAsync(
6665 prefix += '/' ;
6766 }
6867
69- return ListObjectsAsync ( prefix , cancellationToken ) ;
68+ return ListObjectsAsync ( prefix , false , cancellationToken ) ;
7069 }
7170
7271 /// <inheritdoc />
@@ -75,19 +74,18 @@ public Task<IReadOnlyList<IUnixFileSystemEntry>> GetEntriesAsync(
7574 string name ,
7675 CancellationToken cancellationToken )
7776 {
78- var prefix = S3Path . Combine ( ( ( S3DirectoryEntry ) directoryEntry ) . Key , name ) ;
77+ var key = S3Path . Combine ( ( ( S3DirectoryEntry ) directoryEntry ) . Key , name ) ;
7978
80- var objects = await ListObjectsAsync ( prefix , cancellationToken ) ;
81-
82- if ( objects . Count == 1 && objects . Single ( ) is IUnixFileEntry entry )
83- {
79+ var entry = await GetObjectAsync ( key , cancellationToken ) ;
80+ if ( entry != null )
8481 return entry ;
85- }
8682
83+ // not a file search for directory
84+ key += '/' ;
85+
86+ var objects = await ListObjectsAsync ( key , true , cancellationToken ) ;
8787 if ( objects . Count > 0 )
88- {
89- return new S3DirectoryEntry ( prefix ) ;
90- }
88+ return new S3DirectoryEntry ( key ) ;
9189
9290 return null ;
9391 }
@@ -218,8 +216,35 @@ public Task<IUnixFileSystemEntry> SetMacTimeAsync(
218216 return Task . FromResult ( entry ) ;
219217 }
220218
219+ private async Task < IUnixFileSystemEntry ? > GetObjectAsync ( string key , CancellationToken cancellationToken )
220+ {
221+ try
222+ {
223+ var s3Object = await _client . GetObjectMetadataAsync (
224+ new GetObjectMetadataRequest
225+ {
226+ BucketName = _options . BucketName ,
227+ Key = key ,
228+ } , cancellationToken ) ;
229+
230+ if ( key . EndsWith ( "/" ) )
231+ return new S3DirectoryEntry ( key ) ;
232+
233+ return new S3FileEntry ( key , s3Object . Headers . ContentLength )
234+ {
235+ LastWriteTime = s3Object . LastModified ,
236+ } ;
237+ }
238+ catch ( AmazonS3Exception )
239+ {
240+ }
241+
242+ return null ;
243+ }
244+
221245 private async Task < IReadOnlyList < IUnixFileSystemEntry > > ListObjectsAsync (
222246 string prefix ,
247+ bool includeSelf ,
223248 CancellationToken cancellationToken )
224249 {
225250 var objects = new List < IUnixFileSystemEntry > ( ) ;
@@ -247,7 +272,11 @@ private async Task<IReadOnlyList<IUnixFileSystemEntry>> ListObjectsAsync(
247272 {
248273 if ( s3Object . Key . EndsWith ( "/" ) && s3Object . Key == prefix )
249274 {
250- continue ; // this is the folder itself
275+ // this is the folder itself
276+ if ( includeSelf )
277+ objects . Add ( new S3DirectoryEntry ( s3Object . Key ) ) ;
278+
279+ continue ;
251280 }
252281
253282 objects . Add (
0 commit comments