未配置RegionEndpoint或ServiceURL

时间:2017-07-12 10:53:23

标签: .net amazon-web-services amazon-s3 aws-sdk aws-sdk-net

我正在编写代码以将文件上传到AWS S3并收到此异常:

  

AmazonClientException:未配置RegionEndpoint或ServiceURL

我的代码:

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

例外:

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

我该怎么办?

6 个答案:

答案 0 :(得分:8)

错误的简短回答......

  

Amazon.Runtime.AmazonClientException:未配置RegionEndpoint或ServiceURL

...在我的例子中是在构造客户端对象时指定一个区域(对我而言是AmazonSimpleEmailServiceClient)。

假设您正在使用BasicAWSCredentials,请尝试以下操作:

var credentials = new BasicAWSCredentials(accessKeyID, secretKey);

new AmazonS3Client(credentials, RegionEndpoint.USEast1);

//                              ^^^^^^^^^^^^^^^^^^^^^^  add this

答案 1 :(得分:2)

在Asp.Net中,可以通过将以下行添加到Web.config中来解决此错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/pickle.py", line 1384, in load
    return Unpickler(file).load()
  File "/usr/lib64/python2.7/pickle.py", line 864, in load
    dispatch[key](self)
  File "/usr/lib64/python2.7/pickle.py", line 892, in load_proto
    raise ValueError, "unsupported pickle protocol: %d" % proto
ValueError: unsupported pickle protocol: 3

为我工作过AWSSDK.SimpleEmail v3.3

答案 2 :(得分:1)

可以使用我的访问密钥ID和密钥 因此,我放弃使用TransferUtility Class并选择另一个名为PutObjectRequest的类来上传我的文件
attontion :   PutObjectRequest的属性,它的目录名和文件名必须等于本地文件'目录名和文件名。
代码在这里:

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

答案 3 :(得分:0)

如果您使用的是TransferUtility(),请尝试以下操作:

var credentials = new BasicAWSCredentials(accessKeyID, secretKey);

var S3Client = new AmazonS3Client(credentials, RegionEndpoint.USEast1);

this._transferUtility = new TransferUtility(S3Client);

答案 4 :(得分:0)

首先,您不应该对AWS凭证进行硬编码。

我有一个类似的错误。原来是由于.Net Core的更改所致:

  

.NET Core的最大变化之一是删除了ConfigurationManager以及标准的app.config和web.config文件

     

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

对于快速而肮脏的方法,您可以在计算机上创建凭证配置文件,请参见https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html

例如Windows上的C:\Users\<USERNAME>\.aws\credentials

[default]
aws_access_key_id = your_access_key_id
aws_secret_access_key = your_secret_access_key

然后在代码中,您可以执行以下操作:

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

一种更复杂的方法是: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>();

答案 5 :(得分:0)

我在为使用 S3 客户端的 Lambda 运行测试时收到此错误。我通过简单地提供具有在 ~/.aws/config 中定义的区域的默认配置文件解决了该错误:

[default]
region = us-west-1

无需更改 Lambda 代码。当然 YMMV 取决于您的情况。