I'm trying to get filenames within a bucket of my MinIO server using the ListObjectsAsync
method.
Here is the relevant code:
public async Task<string> GetFileName(string userID, string datasetID)
{
ListObjectsArgs args = new ListObjectsArgs()
.WithBucket(userID)
.WithPrefix(datasetID)
.WithRecursive(true);
var files = _minio.ListObjectsAsync(args);
List<string> fileNames = new();
IDisposable subscription = files
.Subscribe(item =>
fileNames.Add(item.Key),
() => Console.WriteLine(fileNames.Count)
);
// I want to return one of the filenames here!
return "";
}
The code above fills the list fileNames
as expected and prints out the correct number of files within the respective locations. However, I would like to user the data within the function above.
I know I could do the following:
files.ToList().Wait();
However, this is a blocking call. Should I simply wrap this call into Task
or is there some other (better) way to get the required data?
CodePudding user response:
Instead of doing the subscribe, return files.ToTask()
.
EDIT
As you mention in the comments, first use .ToList()
in order to gather all the next events into one event. If you are looking for a specific file, like another answer implies, then use .Filter
.
Also I think your return type should be a Task with an array of strings rather than a single string... (Unless you only want one file and have filtered out all the others.)
CodePudding user response:
I think you can check the files you received one by one and stop processing the rest of them when the current file meets a specific condition. Like:
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
Item file = null;
files.Subscribe(
item => {
if (item.Key=="MyFile")
{
file=item;
source.Cancel();
}
},
() => Console.WriteLine(fileNames.Count),
token );