【问题标题】:AWS: unable to find credentials errorAWS:找不到凭证错误
【发布时间】:2018-12-15 07:50:19
【问题描述】:

我有 ASP.NET Core 2 应用程序。我已经在 appsettings.json 中配置了 AWS

如下图

"AWS": {
    "Profile": "default",
    "ProfilesLocation": "D:\\_profiles\\awsprofile"
  }

我确定我在该位置有可用的awsprofile 文件以及以下信息

[default]
aws_access_key_id = XXXXXX
aws_secret_access_key = XXXXXXXXXXX

然后我尝试创建如下所示的 S3Client 实例

    var regionEndPoint = RegionEndpoint.GetBySystemName("us-west-1");                                
    var client = new AmazonS3Client(regionEndPoint));

但是,我遇到了异常Unable to find the 'default' profile in CredentialProfileStoreChain.

应用程序启动异常:Amazon.Runtime.AmazonServiceException: 找不到凭据

异常 1(共 3 个):Amazon.Runtime.AmazonClientException:无法找到 CredentialProfileStoreChain 中的“默认”配置文件。在 Amazon.Runtime.FallbackCredentialsFactory.GetAWSCredentials(ICredentialProfileSource 来源)在 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔 fallbackToAnonymous)

异常 2 之 3:System.InvalidOperationException:环境 变量 AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY/AWS_SESSION_TOKEN 未使用 AWS 凭证设置。在 Amazon.Runtime.EnvironmentVariablesAWSCredentials.FetchCredentials()
在 Amazon.Runtime.EnvironmentVariablesAWSCredentials..ctor() 在 Amazon.Runtime.FallbackCredentialsFactory.c.b__9_1() 在 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔 fallbackToAnonymous)

异常 3 之 3:System.Net.Http.HttpRequestException:响应 状态码不表示成功:404(未找到)。在 System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() 在 System.Net.Http.HttpClient.d__27.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Amazon.Runtime.Internal.Util.AsyncHelpers.c__DisplayClass1_11.<<RunSync>b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Amazon.Runtime.Internal.Util.AsyncHelpers.ExclusiveSynchronizationContext.BeginMessageLoop() at Amazon.Runtime.Internal.Util.AsyncHelpers.RunSync[T](Func1 任务)
在 Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri uri,TimeSpan 超时)在 Amazon.Runtime.URIBasedRefreshingCredentialHelper.GetContents(Uri uri) 在 Amazon.Runtime.InstanceProfileAWSCredentials.d__10.MoveNext() 在 Amazon.Runtime.InstanceProfileAWSCredentials.GetFirstRole() 在 Amazon.Runtime.FallbackCredentialsFactory.ECSEC2CredentialsWrapper()
在 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔 fallbackToAnonymous)

在 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous) 在 XXXXXXXXXXXXXXXXXXXXX.cs:line 30 at Api.Startup.ConfigureServices(IServiceCollection services) 中 D:\Jenkins\XXXXXXXXXXXXXXXXXXXXX\Src\Api\Startup.cs:第 75 行 --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection 服务)在 Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() 暴击:Microsoft.AspNetCore.Hosting.Internal.WebHost[6] 应用程序启动异常 Amazon.Runtime.AmazonServiceException:找不到凭据

异常 1(共 3 个):Amazon.Runtime.AmazonClientException:无法找到 CredentialProfileStoreChain 中的“默认”配置文件。在 Amazon.Runtime.FallbackCredentialsFactory.GetAWSCredentials(ICredentialProfileSource 来源)在 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔 fallbackToAnonymous)

异常 2 之 3:System.InvalidOperationException:环境 变量 AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY/AWS_SESSION_TOKEN 未使用 AWS 凭证设置。在 Amazon.Runtime.EnvironmentVariablesAWSCredentials.FetchCredentials()
在 Amazon.Runtime.EnvironmentVariablesAWSCredentials..ctor() 在 Amazon.Runtime.FallbackCredentialsFactory.c.b__9_1() 在 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔 fallbackToAnonymous)

异常 3 之 3:System.Net.Http.HttpRequestException:响应 状态码不表示成功:404(未找到)。在 System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() 在 System.Net.Http.HttpClient.d__27.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Amazon.Runtime.Internal.Util.AsyncHelpers.c__DisplayClass1_11.<<RunSync>b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Amazon.Runtime.Internal.Util.AsyncHelpers.ExclusiveSynchronizationContext.BeginMessageLoop() at Amazon.Runtime.Internal.Util.AsyncHelpers.RunSync[T](Func1 任务)
在 Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri uri,TimeSpan 超时)在 Amazon.Runtime.URIBasedRefreshingCredentialHelper.GetContents(Uri uri) 在 Amazon.Runtime.InstanceProfileAWSCredentials.d__10.MoveNext() 在 Amazon.Runtime.InstanceProfileAWSCredentials.GetFirstRole() 在 Amazon.Runtime.FallbackCredentialsFactory.ECSEC2CredentialsWrapper()
在 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔 fallbackToAnonymous)

在 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous) 在 XXXXXXXXXXXXXXXXXXXXX: 第 30 行 Api.Startup.ConfigureServices(IServiceCollection services) 中 D:\Jenkins\XXXXXXXXXXXXXXXXXXXXX\Startup.cs:第 75 行 --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection 服务)在 Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

未处理的异常:Amazon.Runtime.AmazonServiceException:无法 查找凭据

异常 1(共 3 个):Amazon.Runtime.AmazonClientException:无法找到 CredentialProfileStoreChain 中的“默认”配置文件。在 Amazon.Runtime.FallbackCredentialsFactory.GetAWSCredentials(ICredentialProfileSource 来源)在 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔 fallbackToAnonymous)

异常 2 之 3:System.InvalidOperationException:环境 变量 AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY/AWS_SESSION_TOKEN 未使用 AWS 凭证设置。在 Amazon.Runtime.EnvironmentVariablesAWSCredentials.FetchCredentials()
在 Amazon.Runtime.EnvironmentVariablesAWSCredentials..ctor() 在 Amazon.Runtime.FallbackCredentialsFactory.c.b__9_1() 在 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔 fallbackToAnonymous)

异常 3 之 3:System.Net.Http.HttpRequestException:响应 状态码不表示成功:404(未找到)。在 System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() 在 System.Net.Http.HttpClient.d__27.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Amazon.Runtime.Internal.Util.AsyncHelpers.c__DisplayClass1_11.<<RunSync>b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Amazon.Runtime.Internal.Util.AsyncHelpers.ExclusiveSynchronizationContext.BeginMessageLoop() at Amazon.Runtime.Internal.Util.AsyncHelpers.RunSync[T](Func1 任务)
在 Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri uri,TimeSpan 超时)在 Amazon.Runtime.URIBasedRefreshingCredentialHelper.GetContents(Uri uri) 在 Amazon.Runtime.InstanceProfileAWSCredentials.d__10.MoveNext() 在 Amazon.Runtime.InstanceProfileAWSCredentials.GetFirstRole() 在 Amazon.Runtime.FallbackCredentialsFactory.ECSEC2CredentialsWrapper()
在 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔 fallbackToAnonymous)

在 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous) 在 XXXXXXXXXXXXXXXXXXXXX: 第 30 行 XXXXXXXXXXXXXXXXXXXXX\Src\Api\Startup.cs:第 75 行 --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection 服务)在 Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
在 Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() 在 Api.Program.Main(String[] args) 在 D:\Jenkins\XXXXXXXXXXXXXXXXXXXXX\Src\Api\Program.cs:20行

【问题讨论】:

    标签: c# amazon-web-services amazon-s3 aws-sdk


    【解决方案1】:

    我想使用AmazonS3Client 下载给定 s3 url 的文件。文件可以位于美国 4 个区域中的任何一个 us-west-1us-west-2us-east-1us-east-2 当您使用流动代码创建AmazonS3Client 的实例时,s3 客户端只能从 appsettings.json 中配置的区域下载文件

    // This will find "AWS" section in your app settings
    services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
    
    // This is used to find your credentials and give you the dependency
    services.AddAWSService<IAmazonS3>();
    

    因此,我创建了一个工厂来创建 AmazonS3Client 的实例,然后根据区域使用适当的 S3 客户端。

    public class S3ClientFactory : IS3ClientFactory
    {        
        private IDictionary<string, IAmazonS3> _container = null;
    
        private S3ClientFactory()
        {
            _container = new Dictionary<string, IAmazonS3>();
        }
    
        public static IS3ClientFactory Create(string[] regions, AWSOptions options)
        {
            var factory = new S3ClientFactory();
            foreach (var region in regions)
            {
                var regionEndPoint = RegionEndpoint.GetBySystemName(region);
                options.Region = regionEndPoint;
                factory._container.Add(region, options.CreateServiceClient<IAmazonS3>());
            }
    
            return factory;
        }
    
        public IAmazonS3 GetS3Client(string region)
        {
            if (!_container.ContainsKey(region))
            {
                throw new Exception(string.Format("Could not find s3 client for key {0}", region));
            }
            return _container[region];
        }
    }
    

    并在 Startup.cs 中注册它

    var awsOptions = Configuration.GetAWSOptions();
    var regions = new string[] { "us-west-1", "us-west-2", "us-east-1", "us-east-2" }; // add supported regions as needed
    services.AddSingleton<IS3ClientFactory>(S3ClientFactory.Create(regions,awsOptions));
    

    然后我使用Amazon.S3.Util.AmazonS3Uri 解析传入的 s3 url 以获取区域并使用工厂获取正确的 S3Client

       var s3Uri = new AmazonS3Uri(sourceS3Uri);
       var s3Client = _factory.GetS3Client(s3Uri.Region.SystemName);
    

    【讨论】:

      【解决方案2】:

      您的应用设置和凭据文件看起来不错。但是,我不相信以这种方式调用构造函数会自动找到凭据。您可以使用 AWS .NET Core 安装包为您处理此问题。关注the docs通过依赖注入获取IAmazonS3非常简单。

      简而言之:

      如果您使用的是 ASP.NET Core,请从 nuget 添加包 AWSSDK.Extensions.NETCore.Setup

      在 ConfigureServices 中添加 AWS 配置和服务:

      public void ConfigureServices(IServiceCollection services)
      {
          // Add framework services.
          services.AddMvc();
      
          ....
      
          // This will find "AWS" section in your app settings
          services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
      
          // This is used to find your credentials and give you the dependency
          services.AddAWSService<IAmazonS3>();
      }
      

      然后您可以通过依赖注入访问控制器、类等中的 IAmazonS3 实例:

      public class HomeController : Controller
      {
          IAmazonS3 S3Client { get; set; }
      
          public HomeController(IAmazonS3 s3Client)
          {
              this.S3Client = s3Client;
          }
      
          ...
      
      }
      

      【讨论】:

      • 正确,在 .NET Core 中,SDK 中没有可供我们使用的 ConfigurationManager 来查找此配置信息。这就是创建AWSSDK.Extensions.NETCore.Setup 的原因。
      猜你喜欢
      • 2014-04-10
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多