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 pagination to ListObjects() #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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