AWS达到最大重试次数:3

时间:2013-08-02 10:19:58

标签: amazon-web-services amazon-s3

我在亚马逊云中托管了简单的Web应用程序。我在托管的webapplication中遇到了读取存储桶对象的问题。当我尝试创建时,我在下面的行中出现错误,

GetObjectResponse response = client.GetObject(request);

错误: 达到最大重试次数:3

即使我尝试增加MaxErrorRetry = 5但仍然遇到问题

* 如果有人可以在这里帮助我,我将非常感谢,谢谢:) *

以下是源代码

public class HomeController : Controller
{
    static string keyName = "";
    static AmazonS3 client;

    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";

        string data = readCloudData("filename.txt");

        ViewBag.Message = data;
        return View();
    }

    public ActionResult About()
    {
        return View();
    }

    public string readCloudData(string objName)
    {
        string data = string.Empty;
        if (checkRequiredFields(objName))
        {
            NameValueCollection appConfig = ConfigurationManager.AppSettings;

            string accessKeyID          = appConfig["AWSAccessKey"];
            string secretAccessKeyID    = appConfig["AWSSecretKey"];
            string bucketName           = appConfig["AWSBucketName"];

            try
            {
                data = ReadingAnObject(accessKeyID, secretAccessKeyID, bucketName, objName);
            }
            catch (AmazonS3Exception s3Exception)
            {
                return "Error in reading file!";
            }
        }
        data = data.Trim();
        return data;
    }

    static string ReadingAnObject(string accessKeyID, string secretAccessKeyID, string bucketName, string keyName)
    {
        string responseBody = "";           
        using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                                         accessKeyID, secretAccessKeyID))
        {
            GetObjectRequest request = new GetObjectRequest()
                .WithBucketName(bucketName).WithKey(keyName);
            try
            {
                using (GetObjectResponse response = client.GetObject(request))
                {             

                    using (Stream responseStream = response.ResponseStream)
                    {
                        using (StreamReader reader =
                            new StreamReader(responseStream))
                        {
                            responseBody = reader.ReadToEnd();
                        }
                    }
                }

            }
            catch (AmazonS3Exception s3Exception)
            {

                return s3Exception.Message;
            }
        }
        return responseBody;
    }

    static bool checkRequiredFields(string keyName)
    {
        NameValueCollection appConfig = ConfigurationManager.AppSettings;

        if (string.IsNullOrEmpty(appConfig["AWSAccessKey"]))
        {
            return false;
        }
        if (string.IsNullOrEmpty(appConfig["AWSSecretKey"]))
        {
            return false;
        }
        if (string.IsNullOrEmpty(appConfig["AWSBucketName"]))
        {
            return false;
        }
        if (string.IsNullOrEmpty(keyName))
        {
            return false;
        }

        return true;
    }
}

1 个答案:

答案 0 :(得分:2)

我发现此问题的最佳解决方案是将CommunicationProtocol设置为HTTP,如下所示:

AmazonS3Config s3Config = new AmazonS3Config();

s3Config.CommunicationProtocol = Protocol.HTTP;

AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKey, s3Config);

...

我们在Ubuntu和C# AWSSDK on GitHub上使用Mono 3.2.3。在OSX上运行相同的代码时,不会出现此问题。它可能与HTTPS和Mono在Linux上使用证书有关。