xie*_*ang 21 .net amazon-s3 amazon-web-services 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);
Run Code Online (Sandbox Code Playgroud)
例外:
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()
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
Jak*_*ler 14
我在对使用 S3 客户端的 Lambda 运行测试时收到此错误。我通过简单地提供一个默认配置文件以及以下中定义的区域来解决了该错误~/.aws/config
:
[default]
region = us-west-1
Run Code Online (Sandbox Code Playgroud)
无需更改 Lambda 代码。当然YMMV取决于你的情况。
Dre*_*kes 12
对错误的简短回答......
Amazon.Runtime.AmazonClientException:未配置RegionEndpoint或ServiceURL
...在我的例子中是在构造客户端对象时指定一个区域(对我而言AmazonSimpleEmailServiceClient
).
假设你正在使用BasicAWSCredentials
然后试试这个:
var credentials = new BasicAWSCredentials(accessKeyID, secretKey);
new AmazonS3Client(credentials, RegionEndpoint.USEast1);
// ^^^^^^^^^^^^^^^^^^^^^^ add this
Run Code Online (Sandbox Code Playgroud)
小智 8
在 Asp.Net 中,可以通过将此行添加到 Web.config 来修复此错误:
<add key="AWSRegion" value="us-east-1" />
Run Code Online (Sandbox Code Playgroud)
为我工作 AWSSDK.SimpleEmail v3.3
首先,您不应该对 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
例如C:\Users\<USERNAME>\.aws\credentials
在 Windows 上
[default]
aws_access_key_id = your_access_key_id
aws_secret_access_key = your_secret_access_key
Run Code Online (Sandbox Code Playgroud)
然后在您的代码中,您可以执行以下操作:
var dbClient = new AmazonDynamoDBClient(new StoredProfileAWSCredentials(),
RegionEndpoint.USEast1);
Run Code Online (Sandbox Code Playgroud)
更复杂的方法是: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();
}
Run Code Online (Sandbox Code Playgroud)
appsettings.Development.json
{
"AWS": {
"Profile": "local-test-profile",
"Region": "us-west-2"
}
}
var options = Configuration.GetAWSOptions();
IAmazonS3 client = options.CreateServiceClient<IAmazonS3>();
Run Code Online (Sandbox Code Playgroud)
我的访问密钥 ID 和秘密密钥都可以使用。
\n因此我放弃使用 TransferUtility 类并选择另一个名为 PutObjectRequest 的类来上传我的文件
\n注意:\n PutObjectRequest\xe2\x80\x99s 属性键,它的目录名和文件名必须等于本地文件\'目录名和文件名。
\n代码在这里:
String s3Path = "987977/Baby.db";\n Console.WriteLine("Ready to upload");\n AWSCredentials credentials;\n credentials = new BasicAWSCredentials(accessKeyID.Trim(), secretKey.Trim());\n AmazonS3Client s3Client = new AmazonS3Client(accessKeyID.Trim(), secretKey.Trim(), Amazon.RegionEndpoint.USEast1);\n Console.WriteLine("Successful verification");\n Console.WriteLine("Check\xef\xbc\x9a if the bucket exists");\n if (!CheckBucketExists(s3Client, bucketName))\n {\n s3Client.PutBucket(bucketName);\n Console.WriteLine("Creat bucket");\n }\n string localPath = @"E:\\telerikFile\\987977\\Baby.db";\n PutObjectRequest obj = new PutObjectRequest();\n var fileStream = new FileStream(localPath, FileMode.Open, FileAccess.Read);\n // obj.FilePath= @"E:\\telerikFile\\987977\\Baby.db";\n obj.InputStream = fileStream;\n obj.BucketName = bucketName;\n obj.Key = s3Path;\n // obj.ContentBody = "This is sample content...";\n obj.CannedACL = S3CannedACL.PublicRead;\n Console.WriteLine("uploading");\n // default to set public right \n s3Client.PutObject(obj);\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
18378 次 |
最近记录: |