No RegionEndpoint or ServiceURL configured

后端 未结 5 1962
野性不改
野性不改 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:16

    The short answer to error...

    Amazon.Runtime.AmazonClientException: No RegionEndpoint or ServiceURL configured

    ...in my case was to specify a region when constructing the client object (for me it was AmazonSimpleEmailServiceClient).

    Assuming you're using BasicAWSCredentials then try this:

    var credentials = new BasicAWSCredentials(accessKeyID, secretKey);
    
    new AmazonS3Client(credentials, RegionEndpoint.USEast1);
    
    //                              ^^^^^^^^^^^^^^^^^^^^^^  add this
    
    0 讨论(0)
  • 2021-01-01 10:18

    My access key id and secret key are can be used.
    Therefore I give up using TransferUtility Class and chosing another Class named PutObjectRequest to upload my files
    attontion: PutObjectRequest’s Property Key,it's directory name and file name must equal to local files' directory name and file name.
    codes here:

            String s3Path = "987977/Baby.db";
            Console.WriteLine("Ready to upload");
            AWSCredentials credentials;
            credentials = new BasicAWSCredentials(accessKeyID.Trim(), secretKey.Trim());
            AmazonS3Client s3Client = new AmazonS3Client(accessKeyID.Trim(), secretKey.Trim(), Amazon.RegionEndpoint.USEast1);
            Console.WriteLine("Successful verification");
            Console.WriteLine("Check: if the bucket exists");
            if (!CheckBucketExists(s3Client, bucketName))
            {
                s3Client.PutBucket(bucketName);
                Console.WriteLine("Creat bucket");
            }
            string localPath = @"E:\telerikFile\987977\Baby.db";
            PutObjectRequest obj = new PutObjectRequest();
            var fileStream = new FileStream(localPath, FileMode.Open, FileAccess.Read);
      //      obj.FilePath= @"E:\telerikFile\987977\Baby.db";
            obj.InputStream = fileStream;
            obj.BucketName = bucketName;
            obj.Key = s3Path;
           // obj.ContentBody = "This is sample content...";
            obj.CannedACL = S3CannedACL.PublicRead;
            Console.WriteLine("uploading");
            // default to set public right  
            s3Client.PutObject(obj);
    
    0 讨论(0)
  • 2021-01-01 10:22

    In Asp.Net this error can be fixed by adding this line to Web.config:

    <add key="AWSRegion" value="us-east-1" />
    

    Worked for me for AWSSDK.SimpleEmail v3.3

    0 讨论(0)
  • 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\<USERNAME>\.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<IAmazonS3>();
    
    0 讨论(0)
  • 2021-01-01 10:35

    If you are using TransferUtility() try this:

    var credentials = new BasicAWSCredentials(accessKeyID, secretKey);
    
    var S3Client = new AmazonS3Client(credentials, RegionEndpoint.USEast1);
    
    this._transferUtility = new TransferUtility(S3Client);
    
    0 讨论(0)
提交回复
热议问题