1

I have ASP.NET Core 2 application. I have configured AWS in appsettings.json

like below

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

i made sure i have the awsprofile file available at that location with below information

[default]
aws_access_key_id = XXXXXX
aws_secret_access_key = XXXXXXXXXXX

Then im trying to create instance of S3Client like below

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

However, i am getting exception Unable to find the 'default' profile in CredentialProfileStoreChain.

Application startup exception: Amazon.Runtime.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) at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)

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. at Amazon.Runtime.EnvironmentVariablesAWSCredentials.FetchCredentials()
at Amazon.Runtime.EnvironmentVariablesAWSCredentials..ctor() at Amazon.Runtime.FallbackCredentialsFactory.<>c.b__9_1() at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)

Exception 3 of 3: System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found). at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at System.Net.Http.HttpClient.d__27.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at 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 task)
at Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri uri, TimeSpan timeout) at Amazon.Runtime.URIBasedRefreshingCredentialHelper.GetContents(Uri uri) at Amazon.Runtime.InstanceProfileAWSCredentials.d__10.MoveNext() at Amazon.Runtime.InstanceProfileAWSCredentials.GetFirstRole() at Amazon.Runtime.FallbackCredentialsFactory.ECSEC2CredentialsWrapper()
at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)

at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous) at XXXXXXXXXXXXXXXXXXXX.cs:line 30 at Api.Startup.ConfigureServices(IServiceCollection services) in D:\Jenkins\XXXXXXXXXXXXXXXXXXXX\Src\Api\Startup.cs:line 75 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() crit: Microsoft.AspNetCore.Hosting.Internal.WebHost[6] Application startup exception Amazon.Runtime.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) at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)

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. at Amazon.Runtime.EnvironmentVariablesAWSCredentials.FetchCredentials()
at Amazon.Runtime.EnvironmentVariablesAWSCredentials..ctor() at Amazon.Runtime.FallbackCredentialsFactory.<>c.b__9_1() at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)

Exception 3 of 3: System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found). at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at System.Net.Http.HttpClient.d__27.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at 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 task)
at Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri uri, TimeSpan timeout) at Amazon.Runtime.URIBasedRefreshingCredentialHelper.GetContents(Uri uri) at Amazon.Runtime.InstanceProfileAWSCredentials.d__10.MoveNext() at Amazon.Runtime.InstanceProfileAWSCredentials.GetFirstRole() at Amazon.Runtime.FallbackCredentialsFactory.ECSEC2CredentialsWrapper()
at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)

at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous) at XXXXXXXXXXXXXXXXXXXX:line 30 at Api.Startup.ConfigureServices(IServiceCollection services) in D:\Jenkins\XXXXXXXXXXXXXXXXXXXX\Startup.cs:line 75 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

Unhandled Exception: Amazon.Runtime.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) at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)

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. at Amazon.Runtime.EnvironmentVariablesAWSCredentials.FetchCredentials()
at Amazon.Runtime.EnvironmentVariablesAWSCredentials..ctor() at Amazon.Runtime.FallbackCredentialsFactory.<>c.b__9_1() at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)

Exception 3 of 3: System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found). at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at System.Net.Http.HttpClient.d__27.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at 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 task)
at Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri uri, TimeSpan timeout) at Amazon.Runtime.URIBasedRefreshingCredentialHelper.GetContents(Uri uri) at Amazon.Runtime.InstanceProfileAWSCredentials.d__10.MoveNext() at Amazon.Runtime.InstanceProfileAWSCredentials.GetFirstRole() at Amazon.Runtime.FallbackCredentialsFactory.ECSEC2CredentialsWrapper()
at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous)

at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous) at XXXXXXXXXXXXXXXXXXXX:line 30 at XXXXXXXXXXXXXXXXXXXX\Src\Api\Startup.cs:line 75 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() at Api.Program.Main(String[] args) in D:\Jenkins\XXXXXXXXXXXXXXXXXXXX\Src\Api\Program.cs:line 20

2 Answers 2

1

Your appsettings and credential file look good. However, I don't believe calling the constructor that way will automatically find the credentials. You can use the AWS .NET Core setup package to handle this for you. It's quite easy to follow the docs to get the IAmazonS3 via dependency injection.

In a nutshell:

If you're using ASP.NET Core, add the package AWSSDK.Extensions.NETCore.Setup from nuget.

The add the AWS config and service in ConfigureServices:

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

Then you can access the IAmazonS3 instance in your controller, class etc by dependency injection:

public class HomeController : Controller
{
    IAmazonS3 S3Client { get; set; }

    public HomeController(IAmazonS3 s3Client)
    {
        this.S3Client = s3Client;
    }

    ...

}
Sign up to request clarification or add additional context in comments.

1 Comment

Correct, in .NET Core we don't have ConfigurationManager available to us in the SDK to find this configuration information. Which is why AWSSDK.Extensions.NETCore.Setup was created.
0

I want to use AmazonS3Client to download a file given s3 url. A File can be situated in any of the 4 US regions us-west-1, us-west-2,us-east-1 and us-east-2 When you create a instance of AmazonS3Client using the flowing code, then s3 client can download a file only from the configured region in 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>();

So, instead i created a Factory that creates the instances of AmazonS3Client and then use the appropriate S3 Client based on the region.

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

and the in Startup.cs register it

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

i then parse the incoming s3 url using Amazon.S3.Util.AmazonS3Uri to get region and use factory to get correct S3Client

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.