如何在没有阻止呼叫的情况下从Iobservable订阅中获取结果?
我正在尝试使用 ListObjectsAsync
方法。
以下是相关代码:
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 "";
}
上面的代码按预期填充列表 fileNames
并打印出相应位置中正确的文件数量。但是,我想在上面的函数中使用数据。 我知道我可以执行以下操作:
files.ToList().Wait();
但是,这是一个阻塞调用。我应该简单地将这个调用包装到 Task
中还是有其他(更好)的方法来获取所需的数据?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而不是订阅,而是返回
files.totask()
。如您在评论中提到的编辑
,首先使用
.tolist()
,以将所有下一个事件收集到一个事件中。如果您正在寻找一个特定文件,就像另一个答案所暗示的那样,请使用.filter
。另外,我认为您的返回类型应该是带有一系列字符串而不是单个字符串的任务...(除非您只想要一个文件并过滤了所有其他文件。)
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.)
我认为您可以将收到的文件一一检查,并在当前文件满足特定条件时停止处理其余文件。喜欢:
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: