.net 未配置 RegionEndpoint 或 ServiceURL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45055665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:57:32  来源:igfitidea点击:

No RegionEndpoint or ServiceURL configured

.netamazon-web-servicesamazon-s3aws-sdkaws-sdk-net

提问by xiehongguang

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

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

AmazonClientException: No RegionEndpoint or ServiceURL configured

AmazonClientException:未配置 RegionEndpoint 或 ServiceURL

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:\telerikFileginabdfglil.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?

我该怎么办?

回答by Drew Noakes

The short answer to error...

错误的简短回答...

Amazon.Runtime.AmazonClientException: No RegionEndpoint or ServiceURL configured

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

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

...在我的情况下是在构造客户端对象时指定一个区域(对我来说是AmazonSimpleEmailServiceClient)。

Assuming you're using BasicAWSCredentialsthen try this:

假设你正在使用BasicAWSCredentials然后试试这个:

var credentials = new BasicAWSCredentials(accessKeyID, secretKey);

new AmazonS3Client(credentials, RegionEndpoint.USEast1);

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

回答by safinilnur

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

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

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

Worked for me for AWSSDK.SimpleEmail v3.3

对我来说有效 AWSSDK.SimpleEmail v3.3

回答by Neil

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

首先,您不应该对 aws 凭据进行硬编码。

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

我有一个类似的错误。原来这是由于 .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

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

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

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

e.g. C:\Users\<USERNAME>\.aws\credentialson Windows

例如C:\Users\<USERNAME>\.aws\credentials在 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

更复杂的方法是: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

appsettings.Development.json

{
  "AWS": {
    "Profile": "local-test-profile",
    "Region": "us-west-2"
  }
}


var options = Configuration.GetAWSOptions();
IAmazonS3 client = options.CreateServiceClient<IAmazonS3>();

回答by xiehongguang

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:

可以使用我的访问密钥 ID 和密钥。
因此我放弃使用 TransferUtility 类并选择另一个名为 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:\telerikFile7977\Baby.db";
        PutObjectRequest obj = new PutObjectRequest();
        var fileStream = new FileStream(localPath, FileMode.Open, FileAccess.Read);
  //      obj.FilePath= @"E:\telerikFile7977\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);

回答by GRD

If you are using TransferUtility()try this:

如果你正在使用TransferUtility()试试这个:

var credentials = new BasicAWSCredentials(accessKeyID, secretKey);

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

this._transferUtility = new TransferUtility(S3Client);