繁体   English   中英

AWS:无法找到凭证错误

[英]AWS: unable to find credentials error

我有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凭证设置环境变量AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY / AWS_SESSION_TOKEN。 在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.HttpClient.d__27.MoveNext()的System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()处-从上一个引发异常的位置开始的堆栈跟踪-在System.Runtime.ExceptionServices处。 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)处的ExceptionDispatchInfo.Throw()Amazon.Runtime.Internal.Util.AsyncHelpers。<> c__DisplayClass1_1 1.<<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](Func 1任务)
在Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri uri,TimeSpan timeout)在Amazon.Runtime.URIBasedRefreshingCredentialHelper.GetContents(Uri uri)在Amazon.Runtime.InstanceProfileAWSCredentials.d__10.MoveNext()在Amazon.Runtime.InstanceProfileAWSColeentials.Get Amazon.Runtime.FallbackCredentialsFactory.ECSEC2CredentialsWrapper()
在Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔fallbackToAnonymous)

在Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)在XXXXXXXXXXXXXXXXXXXX.cs处:Api.Startup.ConfigureServices(IServiceCollection服务)的第30行在D:\\ Jenkins \\ XXXXXXXXXXXXXXXXXXXX \\ Src \\ Api \\ Startup.cs:第75行-从上一个引发异常的位置开始的堆栈结束跟踪---位于Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection服务)的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()处的.EnsureApplicationServices()严重: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凭证设置环境变量AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY / AWS_SESSION_TOKEN。 在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.HttpClient.d__27.MoveNext()的System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()处-从上一个引发异常的位置开始的堆栈跟踪-在System.Runtime.ExceptionServices处。 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)处的ExceptionDispatchInfo.Throw()Amazon.Runtime.Internal.Util.AsyncHelpers。<> c__DisplayClass1_1 1.<<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](Func 1任务)
在Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri uri,TimeSpan timeout)在Amazon.Runtime.URIBasedRefreshingCredentialHelper.GetContents(Uri uri)在Amazon.Runtime.InstanceProfileAWSCredentials.d__10.MoveNext()在Amazon.Runtime.InstanceProfileAWSColeentials.Get Amazon.Runtime.FallbackCredentialsFactory.ECSEC2CredentialsWrapper()
在Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔fallbackToAnonymous)

在Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔值fallbackToAnonymous)在XXXXXXXXXXXXXXXXXXXX:第30行,在D:\\ Jenkins \\ XXXXXXXXXXXXXXXXXXXXXXX \\ Startup.cs:第75行-Api.Startup.ConfigureServices(IServiceCollection服务):第75行-来自上一个的堆栈跟踪引发异常的位置---在Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(在Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() .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凭证设置环境变量AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY / AWS_SESSION_TOKEN。 在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.HttpClient.d__27.MoveNext()的System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()处-从上一个引发异常的位置开始的堆栈跟踪-在System.Runtime.ExceptionServices处。 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)处的ExceptionDispatchInfo.Throw()Amazon.Runtime.Internal.Util.AsyncHelpers。<> c__DisplayClass1_1 1.<<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](Func 1任务)
在Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri uri,TimeSpan timeout)在Amazon.Runtime.URIBasedRefreshingCredentialHelper.GetContents(Uri uri)在Amazon.Runtime.InstanceProfileAWSCredentials.d__10.MoveNext()在Amazon.Runtime.InstanceProfileAWSColeentials.Get Amazon.Runtime.FallbackCredentialsFactory.ECSEC2CredentialsWrapper()
在Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔fallbackToAnonymous)

在Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)在XXXXXXXXXXXXXXXXXXXX:第30行在XXXXXXXXXXXXXXXXXXXX \\ Src \\ Api \\ Startup.cs:第75行---从上一个引发异常的位置开始的堆栈跟踪-在系统处。 Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection服务)的Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()的Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() )
在D:\\ Jenkins \\ XXXXXXXXXXXXXXXXXXXX \\ Src \\ Api \\ Program.cs:第20行的Api.Program.Main(String [] args)处的Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()

您的appsettings和凭据文件看起来不错。 但是,我不相信以这种方式调用构造函数会自动找到凭据。 您可以使用AWS .NET Core安装程序包为您处理此问题。 按照文档通过依赖注入获得IAmazonS3相当容易。

简而言之:

如果您使用的是ASP.NET Core, AWSSDK.Extensions.NETCore.Setup从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;
    }

    ...

}

我想使用AmazonS3Client下载给定s3 URL的文件。 文件可以位于美国4个区域中的任意一个区域us-west-1us-west-2us-east-1us-east-2当您使用以下代码创建AmazonS3Client实例时, AmazonS3Client使用s3 client只能从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>();

因此,我创建了一个Factory,该工厂创建AmazonS3Client的实例,然后根据区域使用适当的S3 Client。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM