在我的aspnet core 3.1项目中,我使用客户端为aws获取所有实例列表为我.在构造函数AmazonEC2Client第三个参数是接受RegionEndPoint的类,我想把enum代替RegionEndPoint。
我的方法是这样的。
public static async Task<List<string>> AwsList(string awsAccessKeyId, string
awsSecretAccessKey) // should AwsRegion region as 3rd paramater
{
AmazonEC2Client client = new AmazonEC2Client(awsAccessKeyId,awsSecretAccessKey,
RegionEndpoint.EUWest1); // replace RegionEndpoint.EUWest1 with enum paramater
bool done = false;
var instanceIds = new List<string>();
DescribeInstanceTypesRequest request = new DescribeInstanceTypesRequest();
while (!done)
{
DescribeInstanceTypesResponse response = await
client.DescribeInstanceTypesAsync(request);
foreach ( var instanceType in response.InstanceTypes.Where(x =>
x.MemoryInfo.SizeInMiB >= 2048 && x.VCpuInfo.DefaultVCpus >= 2))
{
instanceIds.Add(instanceType.InstanceType);
}
request.NextToken= response.NextToken;
if (response.NextToken == null)
{
done = true;
}
}
return instanceIds;
}
我想把第三个参数是enum放到我的方法中,然后用enum代替Region。由于没有接受enum的构造函数,我如何指定AmazonECwClient构造函数,并使用扩展方法指定enum(如果可能的话)。
我想实现和替换我的枚举。
public enum AwsRegion
{
/// <summary>The US East (Virginia) endpoint.</summary>
USEast1 = 1,
/// <summary>The US East (Ohio) endpoint.</summary>
USEast2 = 2,
/// <summary>The US West (N. California) endpoint.</summary>
USWest1 = 3,
/// <summary>The US West (Oregon) endpoint.</summary>
USWest2 = 4,
/// <summary>The EU West (Ireland) endpoint.</summary>
EUWest1 = 5,
/// <summary>The EU Central (Frankfurt) endpoint.</summary>
EUCentral1 = 6,
/// <summary>The Asia Pacific (Tokyo) endpoint.</summary>
APNortheast1 = 7,
/// <summary>The Asia Pacific (Seoul) endpoint.</summary>
APNortheast2 = 8,
/// <summary>The Asia Pacific (Mumbai) endpoint.</summary>
APSouth1 = 9,
/// <summary>The Asia Pacific (Singapore) endpoint.</summary>
APSoutheast1 = 10,
/// <summary>The Asia Pacific (Sydney) endpoint.</summary>
APSoutheast2 = 11,
/// <summary>The South America (Sao Paulo) endpoint.</summary>
SAEast1 = 12,
/// <summary>The US GovCloud West (Oregon) endpoint.</summary>
USGovCloudWest1 = 13,
/// <summary>The China (Beijing) endpoint.</summary>
CNNorth1 = 14
}
我认为这个源码的 aws-sdk-net
RegionEndpoint.cs 将帮助你处理区域映射。
甚至 AWSSDK
有一个字典,以便快速地将一个区域字符串转换为其静态的 区域端点 对象。
private static Dictionary<string, RegionEndpoint> _hashBySystemName = new Dictionary<string, RegionEndpoint>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Gets the region based on its system name like "us-west-1"
/// </summary>
/// <param name="systemName">The system name of the service like "us-west-1"</param>
/// <returns></returns>
public static RegionEndpoint GetBySystemName(string systemName)
{
// ...skip
if (_hashBySystemName.TryGetValue(systemName, out regionEndpoint))
return regionEndpoint;
// ...skip
}
这意味着你可以拥有自己的字典和转换方法。
private static Dictionary<AwsRegion, RegionEndpoint> _awsRegionDict = new Dictionary<AwsRegion, RegionEndpoint>(StringComparer.OrdinalIgnoreCase);
public static RegionEndpoint ConvertByAwsRegion(AwsRegion emAwsRegion)
{
if (_awsRegionDict.TryGetValue(emAwsRegion, out regionEndpoint))
return regionEndpoint;
return RegionEndpoint.EUWest1; // your default region
}
提供另一个扩展方法会使它更容易使用。
public static class RegionEndpointExtension
{
public RegionEndpoint ToRegionEndpoint(this AwsRegion region)
{
return AwsRegionProvider.ConvertByAwsRegion(region)
}
}
因此,使用你的 AwsRegion
enum作为第三个参数,然后调用 ConvertByAwsRegion
或 ToRegionEndpoint
按需转换法。