【发布时间】:2019-11-12 13:38:57
【问题描述】:
任何人都知道为什么我在尝试从密钥管理器获取 AWS 密钥时收到此错误?它是在 AWS Fargate 中运行的 docker 容器。
未处理的异常:System.AggregateException:一个或多个错误 发生了。 (无效参数)---> System.Net.Http.HttpRequestException:参数无效---> System.Net.Sockets.SocketException:参数无效 System.Net.Http.ConnectHelper.ConnectAsync(字符串主机,Int32 端口, CancellationToken cancelToken)
代码sn-p如下。并且该任务已分配了足够的 IAM 角色。
using System;
using System.IO;
using Amazon;
using Amazon.SecretsManager;
using Amazon.SecretsManager.Model;
namespace AssetView.Contacts.WebApi
{
public static class SecretManager
{
public static string GetSecret(string secretName, string region)
{
//string secretName = "av/connectionstring/dev";
// region = "us-east-1";
string secret = "";
MemoryStream memoryStream = new MemoryStream();
IAmazonSecretsManager client = new AmazonSecretsManagerClient(RegionEndpoint.GetBySystemName(region));
GetSecretValueRequest request = new GetSecretValueRequest();
request.SecretId = secretName;
//request.VersionStage = "AWSCURRENT"; // VersionStage defaults to AWSCURRENT if unspecified.
GetSecretValueResponse response = null;
// In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
// See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
// We rethrow the exception by default.
try
{
response = client.GetSecretValueAsync(request).Result;
}
catch
{
throw;
}
// Decrypts secret using the associated KMS CMK.
// Depending on whether the secret is a string or binary, one of these fields will be populated.
if (response.SecretString != null)
{
secret = response.SecretString;
}
else
{
memoryStream = response.SecretBinary;
StreamReader reader = new StreamReader(memoryStream);
secret = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(reader.ReadToEnd()));
}
return secret;
}
}
}
【问题讨论】:
标签: .net-core aws-sdk aws-secrets-manager