How can I show image file from Amazon S3 in asp.net mvc?

I need to show an image (thumbnail) on a view page using a controller / action. (for example: / Image / Thumbnail) I can send an image file that is stored locally by calling a method in the controller.

// sample code
public FileResult Thumbnail()
{
    // get image
    Stream outFile = System.IO.File.Open("c:\\test.jpg", FileMode.Open);

    // send image
    return File(outFile, "image/jpeg");
}

How can I send an image file that is stored in Amazon S3?

Can I use the Amazon S3 URL in the above method to return an image? http://bucketname.s3.amazonaws.com/test.jpg?AWSAccessKeyId=AKIAIDLH65EJ6LSWERDF&Expires=1266497098&Signature=lopDEDErjNLy2uz6X6QCNlIjkpB0%3D

thank

+5
source share
3 answers

:

public ActionResult Thumbnail()
{
    return Redirect("http://domain.com/test.jpg");
}

URL- , . , URL , - <img> src:

<img src="<%= Url.Action("Thumbnail", "ControllerName") %>" />
+3

WebClient:

WebClient wClient = new WebClient();
Stream stream = new MemoryStream(wClient.DownloadData('http://....jpg'));

return File(stream, "image/jpg");
+1

you can create a web request to receive a stream

public FileResult Thumbnail()
    {
        // get image
        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(@"http://bucketname.s3.amazonaws.com/test.jpg?AWSAccessKeyId=AKIAIDLH65EJ6LSWERDF&Expires=1266497098&Signature=lopDEDErjNLy2uz6X6QCNlIjkpB0%3D");
        WebResponse myResp = myReq.GetResponse();

        Stream outFile = myResp.GetResponseStream();

        // send image
        return File(outFile, "image/jpeg");
    }
+1
source

All Articles