Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for pagination in list objects calls #18

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 70 additions & 25 deletions FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,27 @@ public override async Task<IDirectory> GetDirectoryAsync(string virtualPath, Can
}

var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = path};
var response = await client.ListObjectsV2Async(request, cancellationToken);
ListObjectsV2Response response;

if (response.KeyCount == 0)
do
{
throw new DirectoryNotFoundException(path, Prefix);
}
response = await client.ListObjectsV2Async(request, cancellationToken);

foreach (var item in response.S3Objects)
{
if (item.Key == path)
if (response.KeyCount == 0)
{
return ModelFactory.CreateDirectory(item, virtualPath);
throw new DirectoryNotFoundException(path, Prefix);
}

foreach (var item in response.S3Objects)
{
if (item.Key == path)
{
return ModelFactory.CreateDirectory(item, virtualPath);
}
}
}

request.ContinuationToken = response.NextContinuationToken;
} while (response.IsTruncated);

throw new DirectoryNotFoundException(path, Prefix);
}
Expand All @@ -109,22 +116,35 @@ public override async Task<IEnumerable<IFile>> GetFilesAsync(string virtualPath
path += "/";
}

if (path == "/")
{
path = "";
}

try
{
var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = path};
var response = await client.ListObjectsV2Async(request, cancellationToken);
ListObjectsV2Response response;

var files = new List<IFile>();

foreach (var item in response.S3Objects)
do
{
var itemName = item.Key.Substring(0, item.Key.Length - path.Length);
response = await client.ListObjectsV2Async(request, cancellationToken);

if (!item.Key.EndsWith("/") && !itemName.Contains('/'))
foreach (var item in response.S3Objects)
{
files.Add(ModelFactory.CreateFile(item, GetVirtualPath(item.Key)));
// var itemName = item.Key.Substring(0, item.Key.Length - path.Length);
var itemName = item.Key.Substring(path.Length).RemoveLeadingForwardSlash();

if (!item.Key.EndsWith("/") && !itemName.Contains('/'))
{
files.Add(ModelFactory.CreateFile(item, GetVirtualPath(item.Key)));
}
}
}

request.ContinuationToken = response.NextContinuationToken;
} while (response.IsTruncated);

return files;
}
Expand All @@ -139,22 +159,39 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string v
await GetDirectoryAsync(virtualPath, cancellationToken);
var path = GetPath(virtualPath);

if (!path.EndsWith("/"))
{
path += "/";
}

if (path == "/")
{
path = "";
}

try
{
var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = path};
var response = await client.ListObjectsV2Async(request, cancellationToken);
ListObjectsV2Response response;

var directories = new List<IDirectory>();

foreach (var item in response.S3Objects)
do
{
var itemName = item.Key.Substring(path.Length).RemoveLeadingForwardSlash();
response = await client.ListObjectsV2Async(request, cancellationToken);

if (item.Key.EndsWith("/") && itemName.Count(c => c.Equals('/')) == 1)
foreach (var item in response.S3Objects)
{
directories.Add(ModelFactory.CreateDirectory(item, GetVirtualPath(item.Key)));
var itemName = item.Key.Substring(path.Length).RemoveLeadingForwardSlash();

if (item.Key.EndsWith("/") && itemName.Count(c => c.Equals('/')) == 1)
{
directories.Add(ModelFactory.CreateDirectory(item, GetVirtualPath(item.Key)));
}
}
}

request.ContinuationToken = response.NextContinuationToken;
} while (response.IsTruncated);

return directories;
}
Expand Down Expand Up @@ -197,12 +234,20 @@ public override async Task DeleteDirectoryAsync(string virtualPath, Cancellation
var deleteObjectsRequest = new DeleteObjectsRequest {BucketName = bucketName};
var listObjectsRequest = new ListObjectsV2Request {BucketName = bucketName, Prefix = path};

var response = await client.ListObjectsV2Async(listObjectsRequest, cancellationToken);
ListObjectsV2Response response;

foreach (S3Object entry in response.S3Objects)
do
{
deleteObjectsRequest.AddKey(entry.Key);
}
response = await client.ListObjectsV2Async(listObjectsRequest, cancellationToken);

foreach (S3Object entry in response.S3Objects)
{
deleteObjectsRequest.AddKey(entry.Key);
}

listObjectsRequest.ContinuationToken = response.NextContinuationToken;
} while (response.IsTruncated);


await client.DeleteObjectsAsync(deleteObjectsRequest, cancellationToken);
}
Expand Down
Loading