Aws-sdk-net: 支持使用环境变量设置区域

创建于 2016-11-19  ·  3评论  ·  资料来源: aws/aws-sdk-net

问题:
要在 Linux 和 Docker 上使用 .NET Core AWS SDK,我想使用环境变量而不是依赖凭证文件来设置区域。

预期的:
当 AWSOptions 中未设置 Region 时,它将回退到使用 'AWS_REGION' 环境变量来设置区域。

实际的:
Amazon.Extensions.NETCore.Setup.ClientFactory.CreateClient 会抛出异常“Amazon.Runtime.AmazonClientException:未配置 RegionEndpoint 或 ServiceURL。”。

重现步骤:

  1. 从 appsettings.json 中删除 Region 属性。
  2. 使用默认构造函数启动任何服务客户端,例如 AwsEcsService。

问题来源:

  1. ClientFactory.cs 第 158 行
    config.RegionEndpoint = options.Region;
    这有效地将 RegionEndpoint 设置为 null,同时 ClientConfig.cs 的 probeForRegionEndpoint 设置为 false,这将禁用从环境变量进行探测。

解决方法:

  1. 手动创建 AWSOptions 对象(不要使用 appsettings 中的对象)。 读取 Region 环境变量并将其设置为 RegionEndpoint。
var region = Environment.GetEnvironmentVariable(EnvironmentVariableAWSRegion.ENVIRONMENT_VARIABLE_REGION);
if (string.IsNullOrEmpty(region))
    throw new ArgumentNullException($"Cannot read '{EnvironmentVariableAWSRegion.ENVIRONMENT_VARIABLE_REGION}' from environment variable.", nameof(region));
services.AddDefaultAWSOptions(new AWSOptions
{
    Region = RegionEndpoint.GetBySystemName(region)
});

建议:
简单地设置 new AWSOptions() 而不必从环境变量中手动读取它会很好

services.AddDefaultAWSOptions(new AWSOptions());

最有用的评论

最近也遇到了这个问题。 我还可以通过设置 AWS_REGION 和 AWS_DEFAULT_REGION 环境变量来修复它。

所有3条评论

感谢您让我们知道这个问题。 我刚刚推送了 AWSSDK.Extensions.NETCore.Setup 的 3.3.0.2 版本,该版本修复了该问题,以便客户端可以通过环境变量选择区域。

你好,

当我的班级尝试通过 DI 解析服务 IAmazonRekognition 时,我仍然收到以下异常。

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Amazon.Runtime.AmazonClientException: No RegionEndpoint or ServiceURL configured
   at Amazon.Runtime.ClientConfig.Validate() in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\ClientConfig.cs:line 472
   at Amazon.Runtime.AmazonServiceClient..ctor(AWSCredentials credentials, ClientConfig config) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AmazonServiceClient.cs:line 154
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Amazon.Extensions.NETCore.Setup.ClientFactory.CreateClient(Type serviceInterfaceType, AWSCredentials credentials, ClientConfig config) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\extensions\src\AWSSDK.Extensions.NETCore.Setup\ClientFactory.cs:line 103
   at Amazon.Extensions.NETCore.Setup.ClientFactory.CreateServiceClient(ILogger logger, Type serviceInterfaceType, AWSOptions options) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\extensions\src\AWSSDK.Extensions.NETCore.Setup\ClientFactory.cs:line 90

我正在使用以下 nuget 包:

  • AWSSDK.Rekognition 3.3.15.6
  • AWSSDK.Extensions.NETCore.Setup 3.3.6

我还按照文档中的说明设置了环境变量:

  • AWS_DEFAULT_REGION
  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY

@imkheong的解决方法有效;)

最近也遇到了这个问题。 我还可以通过设置 AWS_REGION 和 AWS_DEFAULT_REGION 环境变量来修复它。

此页面是否有帮助?
0 / 5 - 0 等级