我在亚马逊云中托管了简单的 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;
}
}