Net Core 2.0 AmazonServiceException:无法找到凭证 [英] Net Core 2.0 AmazonServiceException: Unable to find credentials

查看:82
本文介绍了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:

> https://github.com/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:无法找到凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆