天道酬勤,学无止境

Net Core 2.0 AmazonServiceException: Unable to find credentials

I have the following code:

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
        };
    }       
}

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

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

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.

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.

I have tried:

Setting up profile as: Amazon.Runtime.AmazonServiceException: Unable to find credentials

And also tried settingup of environment variables:

https://github.com/aws/aws-sdk-net/issues/499

But still cannot get past this error.

评论

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.

Method 1 - Using Credentials file

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.

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",
   }

Method 2 - Setting and Reading from Environment Variables

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"]); 

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

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

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • Amazon.Runtime.AmazonServiceException:无法找到凭证(Amazon.Runtime.AmazonServiceException: Unable to find credentials)
    问题 我们在 vs 2010 中使用连接到 AWS SNS 服务的 AWS SDK 工具包设计了 WebService 应用程序。 当我们直接从 VS 2010 Development Studio 运行时,它完美地工作,但是当我们将 Web 服务发布到本地 IIS 或专用 Web 服务器时,它会失败并显示以下错误消息。 Amazon.Runtime.AmazonServiceException: Unable to find credentials Exception 1 of 4: System.ArgumentException: Path cannot be the empty string or all whitespace. Parameter name: path at System.IO.Directory.GetParent(String path) at Amazon.Runtime.StoredProfileAWSCredentials.DetermineCredentialsFilePath(String profilesLocation) at Amazon.Runtime.StoredProfileAWSCredentials..ctor(String profileName, String profilesLocation) at Amazon
  • AmazonServiceException: Unable to find credentials
    I started running into this error after updating the sdk core to version 3.3.10.2 on my production environment only. I am deploying a .net api on 4 ec2 instances in a load balancer. All Windows Server 2012 R2. The odd part, and really the part that is driving me nuts, is the application runs without issue if I debug it in VS 2015 and it runs fine if I publish the application to my local IIS. When I deploy the application on any of our production machines the error occurs. I am using: <add key="AWSProfileName" value="{profile name}" /> <add key="AWSProfilesLocation" value="{file location}" />
  • AmazonServiceException:无法找到凭证(AmazonServiceException: Unable to find credentials)
    问题 仅在我的生产环境中将 sdk 核心更新到版本 3.3.10.2 后,我开始遇到此错误。 我正在负载均衡器中的 4 个 ec2 实例上部署 .net api。 所有 Windows Server 2012 R2。 奇怪的是,如果我在 VS 2015 中调试应用程序,应用程序运行没有问题,并且如果我将应用程序发布到我的本地 IIS,它运行良好。 当我在我们的任何生产机器上部署应用程序时,就会发生错误。 我在用: <add key="AWSProfileName" value="{profile name}" /> <add key="AWSProfilesLocation" value="{file location}" /> 以及包含适当数据的凭证文件。 在有人询问之前,该应用程序在过去 6 个月内一直在使用 AWS s3 的这种配置中运行,并且在凭证或类似内容方面零错误。 更糟糕的是,即使我尝试将 aws sdk 降级回任何其他版本,错误仍然存​​在。 我完全不知道错误来自哪里或如何修复它。 请帮忙。 这是堆栈跟踪: [AmazonServiceException: Unable to find credentials Exception 1 of 4: System.TypeInitializationException: The type initializer for
  • Amazon.Runtime.AmazonServiceException: Unable to find credentials
    we designed the WebService application in vs 2010 using AWS SDK toolkit which connect to AWS SNS Service. It Perfectly works when we directly run from VS 2010 Development studio, but when we publish webservice to Local IIS or dedicated webserver it fails with following error Messages. Amazon.Runtime.AmazonServiceException: Unable to find credentials Exception 1 of 4: System.ArgumentException: Path cannot be the empty string or all whitespace. Parameter name: path at System.IO.Directory.GetParent(String path) at Amazon.Runtime.StoredProfileAWSCredentials.DetermineCredentialsFilePath(String
  • AWS Java SDK - 无法通过区域提供商链找到区域(AWS Java SDK - Unable to find a region via the region provider chain)
    问题 我已经完成了题为“以编程方式设置 AWS 区域 1”的问题,但它没有提供我需要的所有答案。 Q1:我收到一个SDKClientException-Unable to find a region via the region provider chain 。 我究竟做错了什么? 或者我错过了一个错字。 public class CreateS3Bucket { public static void main(String[] args) throws IOException { BasicAWSCredentials creds = new BasicAWSCredentials("aws-access-key", "aws-secret-key"); AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).build(); Region region = Region.getRegion(Regions.US_EAST_1); s3Client.setRegion(region); try { String bucketName = "testBucket" + UUID.randomUUID()
  • 使用 Visual Studio 2017 运行 Docker 容器时如何管理 AWS 凭证(How to manage AWS credentials when running Docker container with Visual Studio 2017)
    问题 我有一个使用 Visual Studio 2017 开发的 .NET Core 2.0 控制台应用程序。launchSettings.json 文件设置了一个环境变量,允许它使用开发人员的默认 AWS 凭证 "environmentVariables": { "AWS_PROFILE": "default" ... } 我现在已经为 VS 解决方案添加了 Docker 支持,并且正在尝试在 Linux Docker 容器中运行该应用程序。 当然,它会失败,但有以下异常,因为它无法找到配置文件: Amazon.Runtime.AmazonClientException: Unable to find the 'default' profile in CredentialProfileStoreChain. 在开发环境中将 AWS 凭证传递给 Docker 容器的最佳方式是什么? 我显然不想将我的凭据作为环境变量放在 launchSettings.json 中,因为此文件已提交给源代码管理。 编辑 明确地说,我正在寻找一种解决方案,允许我的 Docker 容器在开发人员机器上的 Visual Studio 2017 中进行调试时访问开发人员的凭据。 发布版本将部署到 AWS,并且 IAM 角色将排除对凭证的需求。 凭据在文件%USERPROFILE%\.aws
  • Amazon Java S3 SDK - UploadDirectory(Amazon Java S3 SDK - UploadDirectory)
    问题 我正在使用 Java Amazon S3 SDK 上传我想知道的文件 - 当使用 transferManager 上传目录时 - 是否有更好的方法将 Acl 设置为公共读取 这是我的代码 public boolean uploadDirectoryToAmazon(String directory, String bucketName, String s3DirectoryKey) { boolean result = false; try { LOGGER.info("Uploading a directory to S3"); BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretAccessKey); AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(credentials); AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(awsStaticCredentialsProvider) .withRegion(amazonS3Region) .build
  • How to manage AWS credentials when running Docker container with Visual Studio 2017
    I have a .NET Core 2.0 console application developed using Visual Studio 2017. The launchSettings.json file sets an environment variable that allows it to use the developer's default AWS credentials "environmentVariables": { "AWS_PROFILE": "default" ... } I have now added Docker support to the VS solution, and am trying to run the application in a Linux Docker container. Of course it fails with the following exception, as it is unable to find the profile: Amazon.Runtime.AmazonClientException: Unable to find the 'default' profile in CredentialProfileStoreChain. What is the best way to pass AWS
  • 带有 ASP.NET Core 2.0 的 Aurelia:无法找到 ID 为资源/索引的模块(Aurelia with ASP.NET Core 2.0: Unable to find module with ID: resources/index)
    问题 我使用 SPA Aurelia 模板创建了一个 Visual Studio 2017 ASP.NET Core 2.0 解决方案。 到目前为止一切正常,但我没有得到配置(在“boot.ts”中)来找到我的“资源/元素”文件夹(或者它是 index.ts): export function configure(aurelia: Aurelia) { aurelia.use .standardConfiguration() .feature(PLATFORM.moduleName('resources')); “无法在 WebpackLoader 中找到 ID 为资源/索引的模块。(aurelia-loader-webpack.js:187)” index.ts: import { FrameworkConfiguration } from 'aurelia-framework'; export function configure(config: FrameworkConfiguration) { config.globalResources(['./elements/loading-indicator']); } 解决方案结构: 回答1 我不得不改变 .feature(PLATFORM.moduleName('resources')); 到 .feature(PLATFORM
  • Aurelia with ASP.NET Core 2.0: Unable to find module with ID: resources/index
    I created a Visual Studio 2017 ASP.NET Core 2.0 solution by using the SPA Aurelia template. Everything works fine so far but I don't get the configuration (in "boot.ts") to find my "resources/elements" folder (or it's index.ts): export function configure(aurelia: Aurelia) { aurelia.use .standardConfiguration() .feature(PLATFORM.moduleName('resources')); "Unable to find module with ID: resources/index at WebpackLoader. (aurelia-loader-webpack.js:187)" The index.ts: import { FrameworkConfiguration } from 'aurelia-framework'; export function configure(config: FrameworkConfiguration) { config
  • .net core 2.0 中的默认代理(Default proxy in .net core 2.0)
    问题 我看到几个关于核心 2.0 的问题,关于如何让 HttpClient 使用系统上配置的默认代理。 但没有找到正确的答案。 发布此问题希望可能遇到此问题的人现在可能已经找到了解决方案。 在 .net 框架版本中,我在 web.config 中使用了以下配置,它对我有用。 <system.net> <defaultProxy useDefaultCredentials="true"></defaultProxy> </system.net> 但是在 .net core 2.0 中,我从公司的内部网向外部 api 发出了 Web 请求,我的代码因 407 失败,需要代理身份验证。 经过一点研究,我认为不可能让您的 HttpClient 使用通过 IE 中的 WPAD 配置的默认代理设置。 有人可以在这里纠正我的理解吗? 在 https://github.com/dotnet/corefx/issues/7037 的这个页面上 是这样说的: “HttpClientHandler.UseProxy 属性的默认值为 true。HttpClientHandler.Proxy 的默认值为 NULL,这意味着使用默认代理。” 但我没有观察到这种行为。 更新: 我终于能够通过指定代理服务器地址然后进行 HttpClient 调用来调用外部 web api。 仍然想知道如何在 IE
  • Default proxy in .net core 2.0
    I saw couple of questions asked about core 2.0 on how to make HttpClient to use default proxy configured on the system. But no where found right answer. Posting this question hoping someone who might have encountered this issue might have found the solution by now. In .net framework versions I've used the following configuration in my web.config and it worked for me. <system.net> <defaultProxy useDefaultCredentials="true"></defaultProxy> </system.net> But in .net core 2.0 where I've make a web request to external api from my company's intranet my code is failing with 407, proxy authentication
  • .Net Core 2.0 - 获取 AAD 访问令牌以与 Microsoft Graph 一起使用(.Net Core 2.0 - Get AAD access token to use with Microsoft Graph)
    问题 使用 Azure AD 身份验证启动新的 .Net Core 2.0 项目时,您将获得一个可以登录租户的工作示例,太棒了! 现在我想为登录用户获取访问令牌,并使用它来处理 Microsoft Graph API。 我没有找到任何关于如何实现这一目标的文档。 我只想要一种简单的方法来获取访问令牌并访问图形 API,使用在启动新的 .NET Core 2.0 项目时创建的模板。 从那里我应该能够弄清楚其余的。 非常重要的是,它适用于在 Visual Studio 中创建新的 2.0 MVC Core 应用程序时遵循选择工作和学校帐户进行身份验证的过程时创建的项目。 回答1 我写了一篇博客文章,展示了如何做到这一点:ASP.NET Core 2.0 Azure AD Authentication TL;DR 是当您从 AAD 收到授权代码时,您应该添加这样的处理程序: .AddOpenIdConnect(opts => { Configuration.GetSection("Authentication").Bind(opts); opts.Events = new OpenIdConnectEvents { OnAuthorizationCodeReceived = async ctx => { var request = ctx.HttpContext.Request; var
  • 使用 Pentaho 中的用户定义 Java 类将 pdf 加载到 S3(Load a pdf into S3 using User Defined Java Class in Pentaho)
    问题 import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.PutObjectRequest; import java.io.File; import java.io.IOException; public class UploadObject { public static void main(String[] args) throws IOException { Regions clientRegion =
  • 在 ASP.NET Core 2.0 中从另一个应用服务(无用户交互)授权一个应用服务(Authorize one app service from another (no user interaction) in ASP.NET Core 2.0)
    问题 我在 Azure 中有一个面向互联网 (PubWeb) 的 Web API 应用程序,因此可以在任何地方使用它。 我创建了一个 Web API(同样,在 Azure 中,我们称之为 InWeb)。 我想要强大的安全规则: InWeb 只能由 PubWeb 使用。 这可能涉及一些网络安全 - 这是一个简单的部分(在 Azure 中)。 PubWeb 必须以某种方式获得 Azure AD 的授权才能使用 InWeb 服务。 服务主体、证书等浮现在脑海中。 但我不确定。 那么 2 的最佳解决方案是什么? 更新- 这是 InWeb Core 2.0 Auth 设置: public void ConfigureServices(IServiceCollection services) { services.AddAuthentication(sharedOptions => { sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddAzureAdBearer(options => Configuration.Bind("AzureAd", options)); services.AddMvc(options => { options.InputFormatters.Add(new
  • 使用paypal api时无法建立SOAP连接(Unable to establish SOAP connection while using paypal api)
    问题 我正在尝试使用 paypal soap 客户端,并使用 Java 参考使用 Paypal Express Checkout 执行 setExpressCheckout 方法。 所以在这里我只使用 paypal-base.jar 和 paypal-stubs.jar 而不是肥皂客户端(从 wsdl 生成)。 public class SetExpressCheckoutService{ private static Logger log = Logger.getLogger(SetExpressCheckoutService.class); public static void main(String[] args){ SetExpressCheckoutService setExpressCheckoutService = new SetExpressCheckoutService(); //the parameters for the service Long userId = 5l; String amount = "25"; String returnURL = "http://localhost:8080/integratingstuff-paypal/return_after_payment.xhtml"; String cancelURL = "http:/
  • 如何将 25k 记录放入 kinesis 流和测试工具以确认它(How to put 25k record to kinesis stream and Test tool to acknowledge it)
    问题 我开发了一款将记录写入 Amazon kinesis Stream Web 服务的软件。 我想了解我们是否有任何软件工具可以让我在一秒钟内测量我的代码为 Kinesis Stream 生成的最大吞吐量。 是的,我同意这也取决于硬件配置。 但是首先我想知道通用机器然后我将能够看到水平可扩展性 有了这个,我试图达到每秒 25k 条记录写入 kinesis 流 参考:Kinesis http://aws.amazon.com/kinesis/ 回答1 我相信你可以使用 Apache JMeter 作为 下载并安装 JMeter 下载 Amazon Kinesis Java 客户端库并将 jars 放到 JMeter 类路径(您可以使用 JMeter 安装的 /lib 文件夹) 使用 JSR223 采样器,“groovy”作为语言,AmazonKinesisRecordProducerSample 作为参考,实现将记录写入流的代码 请参阅 Beanshell 与 JSR223 与 Java JMeter 脚本:您一直在等待的性能下降! 有关安装“groovy”引擎支持和脚本最佳实践的说明的指南。 回答2 感谢您的提示。 我已经找到了在 groovy 中使用 AWS-Java-SDK 使用 Kinesis Stream 发送记录的工作代码的方法:这里是示例代码: /* *
  • Amazon Java S3 SDK - UploadDirectory
    I am using the Java Amazon S3 SDK to upload files I was wondering - when using the transferManager to upload a directory - is there a better way to set the Acl to be public-read Here is my code public boolean uploadDirectoryToAmazon(String directory, String bucketName, String s3DirectoryKey) { boolean result = false; try { LOGGER.info("Uploading a directory to S3"); BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretAccessKey); AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(credentials); AmazonS3 s3Client =
  • .Net Core 2.0 call to WCF client configuration
    I have a .NET Core 2.0 application and need to call a WCF client from one of its controllers, and pass the user credentials for authentication. Within the .net core app I created a reference for the WCF client using the Connected Services (WCF Web Service Reference Provider) and now in a process of configuring the call. Note that I can use the same endpoint form a 4.6 framework application without any problems. Here's my code: var binding = new BasicHttpBinding {Security = {Mode = BasicHttpSecurityMode.Transport}}; var address = new EndpointAddress("https://my-endpoint.asmx"); var client = new
  • ASP.Net Core 2.0 mixed authentication of JWT and Windows Authentication doesn't accept credentials
    I've API created in asp.net core 2.0 where I am using mixed mode authentication. For some controllers JWT and for some using windows authentication. I've no problem with the controllers which authorize with JWT. But for the controllers where I want to use windows authentication I am indefinitely prompted with user name and password dialog of chrome. Here my sample controller code where I want to use Windows Authentication instead of JWT. [Route("api/[controller]")] [Authorize(AuthenticationSchemes = "Windows")] public class TestController : Controller { [HttpPost("processUpload")] public async