How to set the EndPoint / Region for the C# .NET SDK : EC2Client?

萝らか妹 提交于 2019-12-01 16:03:17

You can also use an endpoint definitions delivered with Amazon SDK:

var ec2Client = new AmazonEC2Client(RegionEndpoint.EUWest1);

Since I believe hard-coding such values as endpoint addresses is not a best practice I use more configurable version (i.e. endpoint configured from web.config/app.config):

var region = RegionEndpoint.GetBySystemName("eu-west-1");
var ec2Client = new AmazonEC2Client(region);

Regions and Endpoints can be found here.

And example how to connect to EU:

AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client("key", "secret",
    new AmazonEC2Config
    {
        ServiceURL = "http://ec2.eu-west-1.amazonaws.com"
    }
);

You can also define the aws region in your configuration file using the region code:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="AWSProfileName" value="default"/>
    <add key="AWSRegion" value="eu-west-1"/>
  </appSettings>
</configuration>

Then you can simply instanciate your ec2Client without any region:

var ec2Client = new AmazonEC2Client();
Console.WriteLine(ec2Client.Config.RegionEndpoint.DisplayName);

Output:

EU West (Ireland)

Regions and endpoint are defined here: http://docs.aws.amazon.com/general/latest/gr/rande.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!