在 .Net 中使用 Minio 从 S3 存储桶中获取对象列表

Get list of objects from S3 bucket using Minio in .Net

我在 Web 应用程序中使用 Minio .Net SDK (v 3.1.13) 在 IONOS 云存储中获取存储桶和文件存储。它使用 AWS S3 规范。 我可以检查是否存在一个桶及其内容,但只是在编译后的第一次。下次我得到空结果。

S3ObjectStorage.cs

{
    private MinioClient _minioClient;
    public S3ObjectStorage(string key, string secret, string region, string httpsEndPoint) {
        _minioClient = new MinioClient(httpsEndPoint, key, secret, region).WithSSL();
    }
    public async Task<bool> BucketExists(string bucketName) {
            bool found = await _minioClient.BucketExistsAsync(bucketName);
            return found;
    }

    public async Task<string> ListFiles(string bucketName, string path)
    {
        if (!await BucketExists(bucketName)) throw new Exception($"Buckect {bucketName} doesn't exists");
        List<object> files = new List<object>();
        IObservable<Item> observable = _minioClient.ListObjectsAsync(bucketName, path);
        IDisposable subscription = observable.Subscribe(
            item => files.Add(new { item.Key, item.Size })
            //,ex => //Console.WriteLine($"OnError: {ex}")
        );
        return Newtonsoft.Json.JsonConvert.SerializeObject(files);
    }
}```

TestS3.aspx.vb
```Private Async Function List(bucketName As String, path As String) As Task
        Dim cs As New S3ObjectStorage(CS_KEY, CS_SECRET, CS_REGION, HTTPS_ENDPOINT)
        Dim result As String = Await cs.ListFiles(bucketName, path)
        Dim objects = Newtonsoft.Json.JsonConvert.DeserializeObject(result)
        Me.rptObjetos.DataSource = objects
        Me.rptObjetos.DataBind()
        Me.lblPath.Text = $"{bucketName}>>{path} ({objects.Count} - {result})"
    End Function```

Thanks a lot.

我知道了!

using System.Reactive.Linq;
    
public async Task<string> ListFiles(string bucketName, string path)
{
    if (!await BucketExists(bucketName)) throw new Exception($"Buckect {bucketName} doesn't exists");
    var files = await _minioClient.ListObjectsAsync(bucketName, path).ToList();
    return Newtonsoft.Json.JsonConvert.SerializeObject(files);
}