Net Core 2.0 AmazonServiceException:无法找到凭据
Net Core 2.0 AmazonServiceException: Unable to find credentials
我有以下代码:
public void ConfigureServices(IServiceCollection services)
{
// AWS Options
var awsOptions = Configuration.GetAWSOptions();
services.AddDefaultAWSOptions(awsOptions);
var client = awsOptions.CreateServiceClient<IAmazonDynamoDB>();
var dynamoDbOptions = new DynamoDbOptions();
ConfigurationBinder.Bind(Configuration.GetSection("DynamoDbTables"), dynamoDbOptions);
services.AddScoped<IDynamoDbManager<MyModel>>(provider => new DynamoDbManager<MyModel>(client, dynamoDbOptions.MyModel));
}
public class DynamoDbManager<T> : DynamoDBContext, IDynamoDbManager<T> where T : class
{
private DynamoDBOperationConfig _config;
public DynamoDbManager(IAmazonDynamoDB client, string tableName) : base(client)
{
_config = new DynamoDBOperationConfig()
{
OverrideTableName = tableName
};
}
}
我的 Appsettings.json 是:
{
"AWS": {
"Region": "us-east-1",
"AwsId": "xxx",
"AwsPassword": "xxx"
},
"DynamoDbTables": {
"MyModel": "MyTable"
}
}
当我 运行 我的代码出现错误时:
AmazonServiceException: Unable to find credentials
异常 1,共 3 个:
Amazon.Runtime.AmazonClientException:无法在 CredentialProfileStoreChain 中找到 'default' 配置文件。在 Amazon.Runtime.FallbackCredentialsFactory.GetAWSCredentials(ICredentialProfileSource 来源)在 E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\FallbackCredentialsFactory.cs:line 72
异常 2,共 3:
System.InvalidOperationException:环境变量 AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY/AWS_SESSION_TOKEN 未使用 AWS 凭据设置。
异常 3,共 3 个:
System.Net.Http.HttpRequestException: 发送请求时出错。 ---> System.Net.Http.WinHttpException: 操作超时
在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
我已经尝试了很多东西,但没有让它起作用。
我试过:
正在将配置文件设置为:
还尝试设置环境变量:
https://github.com/aws/aws-sdk-net/issues/499
但仍然无法克服这个错误。
因此,当我们使用 AWS SDK 时,我们需要设置并提供一个 AWS 访问密钥和一个秘密密钥。从我所遇到的情况来看,它不会直接从应用程序设置中读取。所以我在下面找到了可以设置这些凭据的两种工作方法。
方法 1 - 使用凭证文件
您可以创建一个凭据文件并将您的凭据存储在那里。下面是文件的格式。
[default]
aws_access_key_id = your id goes here
aws_secret_access_key = your password goes here
在上面的文件中,"default" 是您的个人资料的名称。
创建上述文件后,您需要在 Appsettings.json 文件中将其指定为:
"AWS": {
"Profile": "default",
"ProfilesLocation": "C:\filelocation\awscredentials",
"Region": "us-east-1",
}
方法 2 - 设置和读取环境变量
我们可以在 startup.cs 文件中设置环境变量,如下所示:
Environment.SetEnvironmentVariable("AWS_ACCESS_KEY_ID", Configuration["AWS:AwsId"]);
Environment.SetEnvironmentVariable("AWS_SECRET_ACCESS_KEY", Configuration["AWS:AwsPassword"]);
Environment.SetEnvironmentVariable("AWS_REGION", Configuration["AWS:Region"]);
并从我们的 appSettings.json 文件中读取这些变量:
AWS": {
"Region": "us-east-1",
"AwsId": "xxxx",
"AwsPassword": "xxxx"
}
我有以下代码:
public void ConfigureServices(IServiceCollection services)
{
// AWS Options
var awsOptions = Configuration.GetAWSOptions();
services.AddDefaultAWSOptions(awsOptions);
var client = awsOptions.CreateServiceClient<IAmazonDynamoDB>();
var dynamoDbOptions = new DynamoDbOptions();
ConfigurationBinder.Bind(Configuration.GetSection("DynamoDbTables"), dynamoDbOptions);
services.AddScoped<IDynamoDbManager<MyModel>>(provider => new DynamoDbManager<MyModel>(client, dynamoDbOptions.MyModel));
}
public class DynamoDbManager<T> : DynamoDBContext, IDynamoDbManager<T> where T : class
{
private DynamoDBOperationConfig _config;
public DynamoDbManager(IAmazonDynamoDB client, string tableName) : base(client)
{
_config = new DynamoDBOperationConfig()
{
OverrideTableName = tableName
};
}
}
我的 Appsettings.json 是:
{
"AWS": {
"Region": "us-east-1",
"AwsId": "xxx",
"AwsPassword": "xxx"
},
"DynamoDbTables": {
"MyModel": "MyTable"
}
}
当我 运行 我的代码出现错误时:
AmazonServiceException: Unable to find credentials
异常 1,共 3 个: Amazon.Runtime.AmazonClientException:无法在 CredentialProfileStoreChain 中找到 'default' 配置文件。在 Amazon.Runtime.FallbackCredentialsFactory.GetAWSCredentials(ICredentialProfileSource 来源)在 E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\FallbackCredentialsFactory.cs:line 72
异常 2,共 3: System.InvalidOperationException:环境变量 AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY/AWS_SESSION_TOKEN 未使用 AWS 凭据设置。
异常 3,共 3 个: System.Net.Http.HttpRequestException: 发送请求时出错。 ---> System.Net.Http.WinHttpException: 操作超时 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
我已经尝试了很多东西,但没有让它起作用。
我试过:
正在将配置文件设置为:
还尝试设置环境变量:
https://github.com/aws/aws-sdk-net/issues/499
但仍然无法克服这个错误。
因此,当我们使用 AWS SDK 时,我们需要设置并提供一个 AWS 访问密钥和一个秘密密钥。从我所遇到的情况来看,它不会直接从应用程序设置中读取。所以我在下面找到了可以设置这些凭据的两种工作方法。
方法 1 - 使用凭证文件
您可以创建一个凭据文件并将您的凭据存储在那里。下面是文件的格式。
[default]
aws_access_key_id = your id goes here
aws_secret_access_key = your password goes here
在上面的文件中,"default" 是您的个人资料的名称。
创建上述文件后,您需要在 Appsettings.json 文件中将其指定为:
"AWS": {
"Profile": "default",
"ProfilesLocation": "C:\filelocation\awscredentials",
"Region": "us-east-1",
}
方法 2 - 设置和读取环境变量
我们可以在 startup.cs 文件中设置环境变量,如下所示:
Environment.SetEnvironmentVariable("AWS_ACCESS_KEY_ID", Configuration["AWS:AwsId"]);
Environment.SetEnvironmentVariable("AWS_SECRET_ACCESS_KEY", Configuration["AWS:AwsPassword"]);
Environment.SetEnvironmentVariable("AWS_REGION", Configuration["AWS:Region"]);
并从我们的 appSettings.json 文件中读取这些变量:
AWS": {
"Region": "us-east-1",
"AwsId": "xxxx",
"AwsPassword": "xxxx"
}