Skip to content

Commit

Permalink
Added support to not return virtual folders in get files implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Mar 13, 2024
1 parent 3b98ec4 commit c83a6a5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
13 changes: 12 additions & 1 deletion src/Foundatio.AWS/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ internal static FileSpec ToFileInfo(this S3Object blob)
if (blob == null)
return null;

// Skip directories
if (blob.Key is not null&& blob.Size is 0 && blob.Key.EndsWith("/"))
return null;

return new FileSpec
{
Path = blob.Key,
Expand All @@ -30,7 +34,14 @@ internal static FileSpec ToFileInfo(this S3Object blob)

internal static IEnumerable<S3Object> MatchesPattern(this IEnumerable<S3Object> blobs, Regex patternRegex)
{
return blobs.Where(blob => patternRegex == null || patternRegex.IsMatch(blob.ToFileInfo().Path));
return blobs.Where(blob =>
{
var info = blob.ToFileInfo();
if (info?.Path is null)
return false;

return patternRegex == null || patternRegex.IsMatch(info.Path);
});
}
}
}
1 change: 0 additions & 1 deletion src/Foundatio.AWS/Metrics/CloudWatchMetricsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon;
using Amazon.CloudWatch;
using Amazon.CloudWatch.Model;
using Amazon.Runtime;
Expand Down
1 change: 0 additions & 1 deletion src/Foundatio.AWS/Queues/SQSQueueEntry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Linq;
using Amazon.SQS.Model;

Expand Down
58 changes: 46 additions & 12 deletions tests/Foundatio.AWS.Tests/Storage/S3FileStorageTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Threading.Tasks;
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using Foundatio.Storage;
using Foundatio.Tests.Storage;
using Xunit;
Expand All @@ -12,12 +10,14 @@ namespace Foundatio.AWS.Tests.Storage
{
public class S3FileStorageTests : FileStorageTestsBase
{
private const string BUCKET_NAME = "foundatio-ci";

public S3FileStorageTests(ITestOutputHelper output) : base(output) { }

protected override IFileStorage GetStorage()
{
return new S3FileStorage(
o => o.ConnectionString($"serviceurl=http://localhost:4566;bucket=foundatio-ci;AccessKey=xxx;SecretKey=xxx")
o => o.ConnectionString($"serviceurl=http://localhost:4566;bucket={BUCKET_NAME};AccessKey=xxx;SecretKey=xxx")
.LoggerFactory(Log));
}

Expand Down Expand Up @@ -129,17 +129,51 @@ public override Task WillRespectStreamOffsetAsync()
return base.WillRespectStreamOffsetAsync();
}

protected override async Task ResetAsync(IFileStorage storage)
[Fact]
public virtual async Task WillNotReturnDirectoryInGetPagedFileListAsync()
{
var client = new AmazonS3Client(
new BasicAWSCredentials("xxx", "xxx"),
new AmazonS3Config
var storage = GetStorage();
if (storage == null)
return;

await ResetAsync(storage);

using (storage)
{
var result = await storage.GetPagedFileListAsync();
Assert.False(result.HasMore);
Assert.Empty(result.Files);
Assert.False(await result.NextPageAsync());
Assert.False(result.HasMore);
Assert.Empty(result.Files);

// To create an empty folder (or what appears as a folder) in an Amazon S3 bucket using the AWS SDK for .NET,
// you typically create an object with a key that ends with a trailing slash ('/') because S3 doesn't
// actually have a concept of folders, but it mimics the behavior of folders using object keys.
var client = storage is S3FileStorage s3Storage ? s3Storage.Client : null;
Assert.NotNull(client);

await client.PutObjectAsync(new PutObjectRequest
{
RegionEndpoint = RegionEndpoint.USEast1,
ServiceURL = "http://localhost:4566",
ForcePathStyle = true
BucketName = BUCKET_NAME,
Key = "EmptyFolder/",
ContentBody = String.Empty
});
await client.PutBucketAsync("foundatio-ci");

result = await storage.GetPagedFileListAsync();
Assert.False(result.HasMore);
Assert.Empty(result.Files);
Assert.False(await result.NextPageAsync());
Assert.False(result.HasMore);
Assert.Empty(result.Files);
}
}

protected override async Task ResetAsync(IFileStorage storage)
{
var client = storage is S3FileStorage s3Storage ? s3Storage.Client : null;
Assert.NotNull(client);
await client.PutBucketAsync(BUCKET_NAME);

await base.ResetAsync(storage);
}
Expand Down

0 comments on commit c83a6a5

Please sign in to comment.