任何人都知道为什么我在尝试从秘密管理器获取 AWS 秘密时收到此错误?它是在 AWS Fargate 中运行的 docker 容器。
未处理的异常:System.AggregateException:发生一个或多个错误。(无效参数)---> System.Net.Http.HttpRequestException:无效参数---> System.Net.Sockets.SocketException:System.Net.Http.ConnectHelper.ConnectAsync 的参数无效(字符串主机,Int32 端口,CancellationToken取消令牌)
代码片段如下。并且该任务具有足够的 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;
}
}
}