Net Core 2.0 AmazonServiceException:无法找到凭证

编程入门 行业动态 更新时间:2024-10-23 22:32:59
本文介绍了Net Core 2.0 AmazonServiceException:无法找到凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码:

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是:

My Appsettings.json is as:

{ "AWS": { "Region": "us-east-1", "AwsId": "xxx", "AwsPassword": "xxx" }, "DynamoDbTables": { "MyModel": "MyTable" } }

运行代码时,我得到了错误:

When I run my code I am getting the error:

AmazonServiceException: Unable to find credentials

异常1之3: Amazon.Runtime.AmazonClientException:无法在CredentialProfileStoreChain中找到默认配置文件。在E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\FallbackCredentialsFactory.cs中的Amazon.Runtime.FallbackCredentialsFactory.GetAWSCredentials(ICredentialProfileSource源) :行72

Exception 1 of 3: Amazon.Runtime.AmazonClientException: Unable to find the 'default' profile in CredentialProfileStoreChain. at Amazon.Runtime.FallbackCredentialsFactory.GetAWSCredentials(ICredentialProfileSource source) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\FallbackCredentialsFactory.cs:line 72

异常2之3: System.InvalidOperationException:未使用AWS凭证设置环境变量AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY / AWS_SESSION_TOKEN。

Exception 2 of 3: System.InvalidOperationException: The environment variables AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY/AWS_SESSION_TOKEN were not set with AWS credentials.

3之3的异常: System.Net.Http.HttpRequestException:发送请求时发生错误。 ---> System.Net.Http.WinHttpException:操作在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()处的超时了

Exception 3 of 3: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: The operation timed out at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

很多事情,但没有使它起作用。

I have tried many things but not getting this to work.

我尝试过:

将个人资料设置为: Amazon.Runtime.AmazonServiceException:无法找到凭证

,还尝试设置环境变量:

And also tried settingup of environment variables:

> github/aws/aws-sdk-net/issues/499

但仍然无法克服此错误。

But still cannot get past this error.

推荐答案

因此,当我们使用AWS开发工具包时,需要进行设置并提供一个AWS访问密钥&一个秘密的钥匙。从我遇到的情况来看,它不会直接从应用程序设置中读取。因此,我发现以下是可以用来设置这些凭据的两种工作方法。

So when we are using AWS SDK we need to setup and provide an AWS access key & a secret key. And from what I have come across it does not read directly from the app settings. So I found below are the two working methods with which you can set these credentials.

方法1 -使用凭据文件

您可以创建一个凭证文件并将凭证存储在此处。下面是文件的格式。

You can create a credentials file and store your credentials there. Below is the format of the file.

[default] aws_access_key_id = your id goes here aws_secret_access_key = your password goes here

在上面的文件中,默认是您的个人资料名称。

In above file, "default" is the name of your profile.

创建以上文件后,需要在Appsettings.json文件中指定以下内容:

After creating the above file you need to specify the same in Appsettings.json file as:

"AWS": { "Profile": "default", "ProfilesLocation": "C:\\filelocation\\awscredentials", "Region": "us-east-1", }

方法2 -设置和读取环境变量

Method 2 - Setting and Reading from Environment Variables

我们可以在我们的startup.cs文件中设置环境变量,如下所示:

We can setup the environment variables in our startup.cs file as below:

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文件中读取以下变量:

And read these variables from our appSettings.json file as:

AWS": { "Region": "us-east-1", "AwsId": "xxxx", "AwsPassword": "xxxx" }

更多推荐

Net Core 2.0 AmazonServiceException:无法找到凭证

本文发布于:2023-11-14 00:47:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585764.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:凭证   Core   Net   AmazonServiceException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!