我一直在试图创建桶和使用他们的.NET SDK文件上传到Amazon S3。 我能够创造桶,并指定他们在欧盟地区创建。用于创建桶的code是如下
I have been trying to create buckets and upload files to Amazon S3 using their .net SDK. I am able to create the buckets and specify that they be created in the EU region. The code used to create the buckets is as below
PutBucketRequest request = new PutBucketRequest();
request.WithBucketName(bucketName)
.WithBucketRegion(S3Region.EU);
client.PutBucket(request);
我再继续使用下面的code将文件上传到斗:
I then proceed to upload a file to the bucket using the following code:
PutObjectRequest request = new PutObjectRequest();
request.WithBucketName(bucketName)
.WithCannedACL(S3CannedACL.PublicRead)
.WithKey(remoteFileName)
.WithInputStream(uploadFileStream);
文件上传code失败,错误达到最大重试次数。
The file upload code fails with the error "Maximum retry attempts reached."
任何人都可以让我知道还有什么我需要做,以便上传工作?
Can anyone let me know what else I need to be doing in order for the upload to work?
感谢。
编辑:尝试将文件上传到使用AWS管理控制台工作正常同一个桶
Trying to upload a file to the same bucket using the AWS Management Console works fine.
我发现这个问题,在最后。
I found the issue, at last.
当靶向在特定区域的桶,亚马逊S3客户对象将要被配置为使用特定端点。在code将配置端点是在客户端的建设者和创造是在下面的类的各个方法:
When targeting buckets in a specific region, the Amazon S3 client object will have to be configured to use a specific endpoint. The code to configure the endpoint is as in the constructor and creation of the client is in the individual methods of the class below:
public class AmazonS3Service : IAmazonS3Service
{
private AmazonS3 client;
private string accessKeyID;
private string secretAccessKeyID;
private AmazonS3Config config;
public AmazonS3Service()
{
accessKeyID = ConfigurationManager.AppSettings["AWSAccessKey"];
secretAccessKeyID = ConfigurationManager.AppSettings["AWSSecretKey"];
config = new AmazonS3Config();
config.ServiceURL = ConfigurationManager.AppSettings["AWSEUEndPoint"];
}
public void CreateBucket(string bucketName)
{
using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID, config))
{
try
{
PutBucketRequest request = new PutBucketRequest();
request.WithBucketName(bucketName)
.WithBucketRegion(S3Region.EU);
client.PutBucket(request);
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
//log exception - ("Please check the provided AWS Credentials.");
}
else
{
//log exception - ("An Error, number {0}, occurred when creating a bucket with the message '{1}", amazonS3Exception.ErrorCode, amazonS3Exception.Message);
}
}
}
}
public void UploadFile(string bucketName, Stream uploadFileStream, string remoteFileName)
{
using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID, config))
{
try
{
PutObjectRequest request = new PutObjectRequest();
request.WithBucketName(bucketName)
.WithCannedACL(S3CannedACL.PublicRead)
.WithKey(remoteFileName)
.WithInputStream(uploadFileStream);
using (S3Response response = client.PutObject(request))
{
WebHeaderCollection headers = response.Headers;
foreach (string key in headers.Keys)
{
//log headers ("Response Header: {0}, Value: {1}", key, headers.Get(key));
}
}
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
//log exception - ("Please check the provided AWS Credentials.");
}
else
{
//log exception -("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message);
}
}
}
}
}
每个Amazon的服务的各个端点可以在此URL中找到 - http://docs.amazonwebservices.com/general/latest/gr/index.html?rande.html
The various endpoints of each of Amazon's services can be found at at this URL - http://docs.amazonwebservices.com/general/latest/gr/index.html?rande.html
希望这可以帮助别人!