C# Minio PutBucket 请求:抛出状态为 NameResolutionFailure 的 WebException

C# Minio PutBucket Request: A WebException with status NameResolutionFailure was thrown

我正在研究 Amazon s3 兼容对象存储解决方案 (Minio)。

我有一个 Minio 服务器,例如 192.168.235.143:9000

我试图在 C# 版本的 Amazon s3 上获取存储桶列表 Api。 一切正常。

当我尝试将 Bucket 放入此服务器时,例如 test1,Visual studio 将此请求抛出到异常中:

Amazon.Runtime.AmazonServiceException: 
'A WebException with status NameResolutionFailure was thrown.'

WebException: The remote name could not be resolved: 'test1.192.168.235.143'

这是我的代码:

AmazonS3Config config = new AmazonS3Config();
config.ServiceURL = "192.168.235.143:9000";
AmazonS3Client client = new AmazonS3Client(AccessKey, SecretKey, config);
PutBucketRequest request = new PutBucketRequest();
request.BucketName = "test1";
// this Line will throws above exception :
client.PutBucket(request);

但是 ListBuckets 让我感到困惑:

AmazonS3Client client = new AmazonS3Client(AccessKey, SecretKey, config);
var response = client.ListBuckets();
foreach (S3Bucket b in response.Buckets)
{
    MessageBox.Show(string.Format("{0}\t{1}", b.BucketName, b.CreationDate));
}

我为检查此异常的原因所做的测试:

  1. 我已经将它安装在虚拟机和本地版本上以仔细检查这个问题,但它仍然存在。
  2. 我什至将 Minio 端口更改为 port #80
  3. 我什至用过"play.min.io"但没有区别

我需要使用已知接口,以便我们能够移植到另一个对象存储平台,例如 Ceph,....

我还不知道如何解决它。

请帮我找出我的错误或更好的解决方案

在这里我找到了一个示例工作代码,感谢 Minio GitHub 社区:

using Amazon.S3;
using System;
using System.Threading.Tasks;
using Amazon;

class Program
{
     private const string accessKey = "PLACE YOUR ACCESS KEY HERE";
     private const string secretKey = "PLACE YOUR SECRET KEY HERE"; // do not store secret key hardcoded in your production source code!

     static void Main(string[] args)
     {
         Task.Run(MainAsync).GetAwaiter().GetResult();
     }

     private static async Task MainAsync()
     {
         var config = new AmazonS3Config
         {
             RegionEndpoint = RegionEndpoint.USEast1, // MUST set this before setting ServiceURL and it should match the `MINIO_REGION` enviroment variable.
             ServiceURL = "http://localhost:9000", // replace http://localhost:9000 with URL of your MinIO server
             ForcePathStyle = true // MUST be true to work correctly with MinIO server
         };
         var amazonS3Client = new AmazonS3Client(accessKey, secretKey, config); 

         // uncomment the following line if you like to troubleshoot communication with S3 storage and implement private void OnAmazonS3Exception(object sender, Amazon.Runtime.ExceptionEventArgs e)
         // amazonS3Client.ExceptionEvent += OnAmazonS3Exception;

         var listBucketResponse = await amazonS3Client.ListBucketsAsync();

         foreach (var bucket in listBucketResponse.Buckets)
         {
             Console.Out.WriteLine("bucket '" + bucket.BucketName + "' created at " + bucket.CreationDate);
         }
         if (listBucketResponse.Buckets.Count > 0)
         {
             var bucketName = listBucketResponse.Buckets[0].BucketName;

             var listObjectsResponse = await amazonS3Client.ListObjectsAsync(bucketName);

             foreach (var obj in listObjectsResponse.S3Objects)
             {
                 Console.Out.WriteLine("key = '" + obj.Key + "' | size = " + obj.Size + " | tags = '" + obj.ETag + "' | modified = " + obj.LastModified);
             }
         }
     }
 }