1

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

GetObjectResponse 响应 = 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;
    }
}
4

1 回答 1

2

我为这个问题找到的最佳解决方案是将 设置CommunicationProtocol为 HTTP,如下所示:

AmazonS3Config s3Config = new AmazonS3Config();

s3Config.CommunicationProtocol = Protocol.HTTP;

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

...

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

于 2013-10-16T23:36:58.743 回答