将文件上载到Amazon S3中的存储桶失败并显示“达到最大重试次数”

时间:2011-09-13 16:31:07

标签: c# .net amazon-s3

我一直在尝试使用他们的.net SDK创建存储桶并将文件上传到Amazon S3。 我能够创建存储桶并指定它们是在欧盟地区创建的。用于创建存储区的代码如下所示

PutBucketRequest request = new PutBucketRequest();
request.WithBucketName(bucketName)
       .WithBucketRegion(S3Region.EU);

client.PutBucket(request);

然后我使用以下代码继续将文件上传到存储桶:

PutObjectRequest request = new PutObjectRequest();
request.WithBucketName(bucketName)
    .WithCannedACL(S3CannedACL.PublicRead)
    .WithKey(remoteFileName)
    .WithInputStream(uploadFileStream);

文件上传代码失败,并显示错误“已达到最大重试次数。”

任何人都可以让我知道我需要做什么才能让上传工作吗?

感谢。

编辑:尝试使用AWS管理控制台将文件上传到同一个存储桶。

1 个答案:

答案 0 :(得分:13)

我终于找到了这个问题。

在特定区域中定位存储区时,必须将Amazon S3客户端对象配置为使用特定端点。配置端点的代码与构造函数中的一样,客户端的创建位于下面类的各个方法中:

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

可以在此网址找到每个亚马逊服务的各个端点 - http://docs.amazonwebservices.com/general/latest/gr/index.html?rande.html

希望这有助于某人!