如何在没有阻止呼叫的情况下从Iobservable订阅中获取结果?

发布于 2025-01-20 17:59:36 字数 1072 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城缀染半城烟沙 2025-01-27 17:59:36

而不是订阅,而是返回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 2025-01-27 17:59:36

我认为您可以将收到的文件一一检查,并在当前文件满足特定条件时停止处理其余文件。喜欢:

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 );

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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文