您必须使用 AWSSDK.ServiceDiscovery NuGet 包从您的 Cloud Map 命名空间解析服务。解析服务后,您可以将该值用于 S3 服务客户端。这是发现作为 S3 存储桶的 mystorage 服务然后将值与 S3 一起使用的简单示例。
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.ServiceDiscovery;
using Amazon.ServiceDiscovery.Model;
namespace ServiceDiscoveryTest
{
class Program
{
static async Task Main(string[] args)
{
using(var discoveryClient = new AmazonServiceDiscoveryClient(RegionEndpoint.USEast1))
using(var s3Client = new AmazonS3Client(RegionEndpoint.USEast1))
{
var discoveryResponse = await discoveryClient.DiscoverInstancesAsync(new DiscoverInstancesRequest
{
NamespaceName = "dev",
ServiceName = "mystorage",
QueryParameters = new Dictionary<string, string>
{
{ "Version", "1.0" }
}
});
var listResponse = await s3Client.ListObjectsAsync(new ListObjectsRequest
{
BucketName = discoveryResponse.Instances[0].InstanceId
});
foreach(var s3Object in listResponse.S3Objects)
{
Console.WriteLine(s3Object.Key);
}
}
}
}
}