AWS KMS (AmazonKeyManagementServiceClient) .Net Core 未加载 AWS 配置文件 appSettings.json

AWS KMS (AmazonKeyManagementServiceClient) .Net Core not loading the AWS profile appSettings.json

我有一个 .Net Core 应用程序需要使用 AmazonKeyManagementServiceClient 访问 KMS。该应用程序使用特定的 AWS ProfilesLocation 和 appSettings.json 中的特定配置文件集。 S3 客户端工作正常并从配置文件位置获取凭据。但是,AmazonKeyManagementServiceClient 无法获取正确的配置文件和关联的凭证。有什么方法可以让 AmazonKeyManagementServiceClient 从 appSettings.json 中指定的位置获取凭据,而不必使用重载的构造函数 public AmazonKeyManagementServiceClient(AWSCredentials credentials)?

这里是appSettings.Development.json的相关代码(环境变量设置为Development):

    {
  "AWS": {
    "Profile": "prod",
    "ProfilesLocation": "C:\Users\my-folder-location\.aws\credentials",
    "Region": "us-east-1"
  }

}

这是访问 S3 和 KMS 的代码

//I checked below that the AWS options are being loaded correctly 
var options = _configuration.GetAWSOptions();

//The S3 client uses the correct credentials
var s3Client = options.CreateServiceClient<IAmazonS3>();

//However the KMS client is using default credentials
var kmsClient = new AmazonKeyManagementServiceClient();

如有任何帮助,我们将不胜感激。

如果您希望像 S3 客户端一样配置 AmazonKeyManagementServiceClient 客户端,那么您需要像为 S3 一样调用 options.CreateServiceClient<IAmazonKeyManagementService>()。仅使用服务客户端的构造函数将无法访问应用程序中的 IConfiguration 来获取凭据配置信息。