C# AWS:找不到凭据错误
我有ASP.NETCore2应用程序。我已经在appsettings.json中配置了AWSC# AWS:找不到凭据错误,c#,amazon-web-services,amazon-s3,aws-sdk,C#,Amazon Web Services,Amazon S3,Aws Sdk,我有ASP.NETCore2应用程序。我已经在appsettings.json中配置了AWS // 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.AddAWSSer
// 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>();
如下
"AWS": {
"Profile": "default",
"ProfilesLocation": "D:\\_profiles\\awsprofile"
}
var regionEndPoint = RegionEndpoint.GetBySystemName("us-west-1");
var client = new AmazonS3Client(regionEndPoint));
我确保在该位置有awsprofile
文件,其中包含以下信息
[default]
aws_access_key_id = XXXXXX
aws_secret_access_key = XXXXXXXXXXX
然后我尝试创建S3Client的实例,如下所示
"AWS": {
"Profile": "default",
"ProfilesLocation": "D:\\_profiles\\awsprofile"
}
var regionEndPoint = RegionEndpoint.GetBySystemName("us-west-1");
var client = new AmazonS3Client(regionEndPoint));
但是,我遇到异常无法在CredentialProfileStoreChain中找到“默认”配置文件。
应用程序启动异常:Amazon.Runtime.AmazonServiceException:
找不到凭据
异常1(共3个):Amazon.Runtime.AmazonClientException:无法找到
CredentialProfileStoreChain中的“默认”配置文件。在
Amazon.Runtime.FallbackCredentialsFactory.GetAWSCredentials(ICredentialProfileSource
来源)在
Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔值
回退到非紧急状态)
异常2/3:System.InvalidOperation异常:环境
变量AWS\u访问权\u密钥\u ID/AWS\u机密\u访问权\u密钥/AWS\u会话\u令牌
未使用AWS凭据设置。在
Amazon.Runtime.EnvironmentVariablesAWSCredentials.FetchCredentials()在Amazon.Runtime.EnvironmentVariablesAWSCredentials..ctor()中 Amazon.Runtime.FallbackCredentialsFactory.c.b_u9_u1()位于 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔值 回退到非紧急状态) 异常3/3:System.Net.Http.HttpRequestException:响应 状态代码不表示成功:404(未找到)。在 System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()位于 System.Net.Http.HttpClient.d_u27.MoveNext() ---来自引发异常的上一个位置的堆栈结束跟踪---在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Amazon.Runtime.Internal.Util.AsyncHelpers.c_uudisplayClass1_1
1.d.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
Amazon.Runtime.Internal.Util.AsynchHelpers.ExclusiveSynchronizationContext.BeginMessageLoop()的
在Amazon.Runtime.Internal.Util.AsyncHelpers.RunSync[T](Func
1任务)中在Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri,TimeSpan 超时)在 Amazon.Runtime.URIBasedRefreshingCredentialHelper.GetContents(Uri) 在 Amazon.Runtime.InstanceProfileAWSCredentials.d_u10.MoveNext() 位于Amazon.Runtime.InstanceProfileAWSCredentials.GetFirstRole()的 Amazon.Runtime.FallbackCredentialsFactory.ECSEC2CredentialsWrapper()
在Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔值 回退到非紧急状态) 在Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔值 回退至XXXXXXXXXXXXXXXXX处的非紧急状态。cs:30行 中的Api.Startup.ConfigureServices(IServiceCollection服务) D:\Jenkins\xxxxxxxxxxxxxxxxx\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()上 crit:Microsoft.AspNetCore.Hosting.Internal.WebHost[6] 应用程序启动异常Amazon.Runtime.AmazonServiceException:找不到凭据 异常1(共3个):Amazon.Runtime.AmazonClientException:无法找到 CredentialProfileStoreChain中的“默认”配置文件。在 Amazon.Runtime.FallbackCredentialsFactory.GetAWSCredentials(ICredentialProfileSource 来源)在 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔值 回退到非紧急状态) 异常2/3:System.InvalidOperation异常:环境 变量AWS\u访问权\u密钥\u ID/AWS\u机密\u访问权\u密钥/AWS\u会话\u令牌 未使用AWS凭据设置。在 Amazon.Runtime.EnvironmentVariablesAWSCredentials.FetchCredentials()
在Amazon.Runtime.EnvironmentVariablesAWSCredentials..ctor()中 Amazon.Runtime.FallbackCredentialsFactory.c.b_u9_u1()位于 Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔值 回退到非紧急状态) 异常3/3:System.Net.Http.HttpRequestException:响应 状态代码不表示成功:404(未找到)。在 System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()位于 System.Net.Http.HttpClient.d_u27.MoveNext() ---来自引发异常的上一个位置的堆栈结束跟踪---在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Amazon.Runtime.Internal.Util.AsyncHelpers.c_uudisplayClass1_1
1.d.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
Amazon.Runtime.Internal.Util.AsynchHelpers.ExclusiveSynchronizationContext.BeginMessageLoop()的
在Amazon.Runtime.Internal.Util.AsyncHelpers.RunSync[T](Func
1任务)中在Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri,TimeSpan 超时)在 Amazon.Runtime.URIBasedRefreshingCredentialHelper.GetContents(Uri) 在 Amazon.Runtime.InstanceProfileAWSCredentials.d_u10.MoveNext() 位于Amazon.Runtime.InstanceProfileAWSCredentials.GetFirstRole()的 Amazon.Runtime.FallbackCredentialsFactory.ECSEC2CredentialsWrapper()
在Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔值 回退到非紧急状态) 在Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(布尔值 回退至XXXXXXXXXXXXXXXXX的非紧急状态:第30行 Api.Startup.Configur
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];
}
}
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));
var s3Uri = new AmazonS3Uri(sourceS3Uri);
var s3Client = _factory.GetS3Client(s3Uri.Region.SystemName);