No RegionEndpoint or ServiceURL configured

后端 未结 5 1961
野性不改
野性不改 2021-01-01 09:35

I am writing code to upload files to AWS S3 and receiving this exception:

AmazonClientException: No RegionEndpoint or ServiceURL configured

5条回答
  •  囚心锁ツ
    2021-01-01 10:26

    First of all, you shouldn't hardcode aws credentials.

    I had a similar error. Turned out it was due to changes in .Net Core:

    One of the biggest changes in .NET Core is the removal of ConfigurationManager and the standard app.config and web.config files

    https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-netcore.html

    For quick and dirty approach you can create credintial profile file on your machine see https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html

    e.g. C:\Users\\.aws\credentials on Windows

    [default]
    aws_access_key_id = your_access_key_id
    aws_secret_access_key = your_secret_access_key
    

    then in your code you can just do something like:

    var dbClient = new AmazonDynamoDBClient(new StoredProfileAWSCredentials(), 
                         RegionEndpoint.USEast1);
    

    A more involved way is: https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-netcore.html#net-core-configuration-builder

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }
    

    appsettings.Development.json

    {
      "AWS": {
        "Profile": "local-test-profile",
        "Region": "us-west-2"
      }
    }
    
    
    var options = Configuration.GetAWSOptions();
    IAmazonS3 client = options.CreateServiceClient();
    

提交回复
热议问题