No RegionEndpoint or ServiceURL configured

2019-02-12 06:55发布

问题:

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

AmazonClientException: No RegionEndpoint or ServiceURL configured

My code:

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("create bucket");
}
TransferUtility utility = new TransferUtility();
Console.WriteLine("Upload  Directory......");
//exception here
utility.UploadDirectory(@"E:\telerikFile\13ginabdfglil.com", bucketName);

The exception:

Amazon.Runtime.AmazonClientException: No RegionEndpoint or ServiceURL configured
  Amazon.Runtime.ClientConfig.Validate()
  Amazon.S3.AmazonS3Config.Validate()
  Amazon.Runtime.AmazonServiceClient..ctor(AWSCredentials credentials, ClientConfig config)
  Amazon.S3.AmazonS3Client..ctor()
  Amazon.S3.Transfer.TransferUtility..ctor()
  Telerik2Amazon.Program.UploadFile()

What should I do?

回答1:

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


回答2:

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("准备上传");
        AWSCredentials credentials;
        credentials = new BasicAWSCredentials(accessKeyID.Trim(), secretKey.Trim());
        AmazonS3Client s3Client = new AmazonS3Client(accessKeyID.Trim(), secretKey.Trim(), Amazon.RegionEndpoint.USEast1);
        Console.WriteLine("验证成功");
        Console.WriteLine("检查存储桶是否存在");
        if (!CheckBucketExists(s3Client, bucketName))
        {
            s3Client.PutBucket(bucketName);
            Console.WriteLine("创建数据桶");
        }
        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("正在上传");
        // 默认添加public权限  
        s3Client.PutObject(obj);


回答3:

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


回答4:

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