我需要从 S3 检索图像。我不能公开文件夹,也不能使用预签名的 URL,所以我只剩下 GetObject();。现在,我要返回的图像必须设置为 iframe 的源。为此,我使用了 HttpWebHandler。问题是,如果我检索一个 html 页面,它工作正常。但是当我尝试取回图像时,我得到的只是垃圾数据。这是我的代码:
public void ProcessRequest(HttpContext context)
{
NameValueCollection appConfig = ConfigurationManager.AppSettings;
_accessKeyId = appConfig["AWSAccessKey"];
_secretAccessKeyId = appConfig["AWSSecretKey"];
S3 = new AmazonS3Client(_accessKeyId, _secretAccessKeyId);
string responseBody = "";
var request = new GetObjectRequest()
.WithBucketName(bucketName).WithKey("020/images/intro.jpg");
var responseHeaders = new ResponseHeaderOverrides
{
ContentType = "image/jpeg"
};
request.ResponseHeaderOverrides = responseHeaders;
using (var response = S3.GetObject(request))
{
using (var responseStream = response.ResponseStream)
{
using (var reader =
new StreamReader(responseStream))
{
responseBody = reader.ReadToEnd();
}
}
}
context.Response.Write(responseBody);
context.Response.Flush();
context.Response.End();
}
}