Skip to content

Commit

Permalink
Add pagination to ListObjects()
Browse files Browse the repository at this point in the history
Fixes #36

Add pagination support to ListObjects() method.

* Add a new overload for `ListObjectsAsync` in `IObjectService` interface to take a 'Cursor'-value and a max entries parameter.
* Update `ListObjectsOptions` class to include a `MaxEntries` property and update the `ToSWIG` method to include this property.
* Implement the new overload for `ListObjectsAsync` in `ObjectService` class to handle the 'Cursor'-value and max entries parameter.
* Update `BucketContentViewModel` class to use the new overload of `ListObjectsAsync` with 'Cursor'-value and max entries parameter.
* Add a new test method in `ObjectServiceTest` to verify the new overload of `ListObjectsAsync` with 'Cursor'-value and max entries parameter.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/TopperDEL/uplink.net/issues/36?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
TopperDEL committed Oct 28, 2024
1 parent 7a25518 commit d5ff198
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public async Task InitAsync()
{
var bucket = await _bucketService.GetBucketAsync(BucketName);
var listOptions = new ListObjectsOptions();
var objects = await _objectService.ListObjectsAsync(bucket, listOptions);
var objects = await _objectService.ListObjectsAsync(bucket, listOptions, null, 1000);
foreach (var obj in objects.Items)
{
var entry = new BucketEntryViewModel(this, _bucketService, _objectService);
Expand Down
31 changes: 31 additions & 0 deletions uplink.NET/uplink.NET.Test/ObjectServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,36 @@ public async Task CopyObject_CopiesObject_InDifferentBucket()
}
}

[TestMethod]
public async Task ListObjects_WithCursorAndMaxEntries()
{
string bucketname = "listobject-with-cursor-maxentries";

await _bucketService.CreateBucketAsync(bucketname);
var bucket = await _bucketService.GetBucketAsync(bucketname);
byte[] bytesToUpload = GetRandomBytes(2048);

var uploadOperation1 = await _objectService.UploadObjectAsync(bucket, "myfile1.txt", new UploadOptions(), bytesToUpload, false);
await uploadOperation1.StartUploadAsync();
var uploadOperation2 = await _objectService.UploadObjectAsync(bucket, "myfile2.txt", new UploadOptions(), bytesToUpload, false);
await uploadOperation2.StartUploadAsync();
var uploadOperation3 = await _objectService.UploadObjectAsync(bucket, "myfile3.txt", new UploadOptions(), bytesToUpload, false);
await uploadOperation3.StartUploadAsync();

var listOptions = new ListObjectsOptions { MaxEntries = 2 };
var objectList = await _objectService.ListObjectsAsync(bucket, listOptions);

Assert.AreEqual(2, objectList.Items.Count);
Assert.AreEqual("myfile1.txt", objectList.Items[0].Key);
Assert.AreEqual("myfile2.txt", objectList.Items[1].Key);

listOptions.Cursor = objectList.Items[1].Key;
var objectList2 = await _objectService.ListObjectsAsync(bucket, listOptions);

Assert.AreEqual(1, objectList2.Items.Count);
Assert.AreEqual("myfile3.txt", objectList2.Items[0].Key);
}

public static byte[] GetRandomBytes(long length)
{
byte[] bytes = new byte[length];
Expand Down Expand Up @@ -640,6 +670,7 @@ public async Task CleanupAsync()
await DeleteBucketAsync("copyobject-copies-object-samebucket");
await DeleteBucketAsync("copyobject-copies-object-diffbucket1");
await DeleteBucketAsync("copyobject-copies-object-diffbucket2");
await DeleteBucketAsync("listobject-with-cursor-maxentries");
}

private async Task DeleteBucketAsync(string bucketName)
Expand Down
9 changes: 9 additions & 0 deletions uplink.NET/uplink.NET/Interfaces/IObjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ public interface IObjectService
/// <returns>The list of found objects within the bucket and with the given ListOptions or throws an ObjectListException</returns>
Task<ObjectList> ListObjectsAsync(Bucket bucket, ListObjectsOptions listObjectsOptions);
/// <summary>
/// Lists objects within a bucket with pagination support
/// </summary>
/// <param name="bucket">The Bucket to list entries from</param>
/// <param name="listOptions">Options for the listing</param>
/// <param name="cursor">The cursor value to start listing from</param>
/// <param name="maxEntries">The maximum number of entries to return</param>
/// <returns>The list of found objects within the bucket and with the given ListOptions or throws an ObjectListException</returns>
Task<ObjectList> ListObjectsAsync(Bucket bucket, ListObjectsOptions listObjectsOptions, string cursor, int maxEntries);
/// <summary>
/// Gets the specific object
/// </summary>
/// <param name="bucket">The Bucket where the object resides in</param>
Expand Down
5 changes: 5 additions & 0 deletions uplink.NET/uplink.NET/Models/ListObjectsOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class ListObjectsOptions
/// Custom includes CustomMetadata in the results.
/// </summary>
public bool Custom { get; set; }
/// <summary>
/// MaxEntries sets the maximum number of entries to return.
/// </summary>
public int MaxEntries { get; set; }

internal SWIG.UplinkListObjectsOptions ToSWIG()
{
Expand All @@ -38,6 +42,7 @@ internal SWIG.UplinkListObjectsOptions ToSWIG()
ret.recursive = Recursive;
ret.system = System;
ret.custom = Custom;
ret.max_entries = MaxEntries;

return ret;
}
Expand Down
7 changes: 7 additions & 0 deletions uplink.NET/uplink.NET/Services/ObjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ public async Task<ObjectList> ListObjectsAsync(Bucket bucket, ListObjectsOptions
}
}

public async Task<ObjectList> ListObjectsAsync(Bucket bucket, ListObjectsOptions listObjectsOptions, string cursor, int maxEntries)
{
listObjectsOptions.Cursor = cursor;
listObjectsOptions.MaxEntries = maxEntries;
return await ListObjectsAsync(bucket, listObjectsOptions).ConfigureAwait(false);
}

public async Task<uplink.NET.Models.Object> GetObjectAsync(Bucket bucket, string targetPath)
{
using (var objectResult = await Task.Run(() => SWIG.storj_uplink.uplink_stat_object(_access._project, bucket.Name, targetPath)).ConfigureAwait(false))
Expand Down

0 comments on commit d5ff198

Please sign in to comment.