The AWS Cloud Map says it will work with S3 buckets. What I'm struggling to understand is how can I setup the AWS S3 SDK to use cloud map namespaces and services when making request to an S3 bucket. Is there a complete example somewhere that covers Setting up and S3 bucket, Adding it to cloud map, using the AWS S3 SDK to access the S3 bucket using cloud map.
Why I need this: I'm currently trying to setup dev, stage and prod environments using multiple aws accounts and would like the code to remain the same in the application but use cloud map to map the resources. I understand how I can make this work with database and other urls. Where I'm stuck is when my code is using the AWS SDK to access AWS resources.
You have to use the AWSSDK.ServiceDiscovery NuGet package to resolve the service from your Cloud Map namespace. Once you resolve the service you use that value with the S3 service client. Here is simple example of discoverying mystorage service which is an S3 bucket and then using the value with 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);
}
}
}
}
}