Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to show image(thumbnail) in view page using controller/action.(like: /Image/Thumbnail) I can send image file that is stored locally by calling the method in 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 image file that is stored in Amazon S3 ?

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

Thanks

like image 888
user240181 Avatar asked Feb 18 '10 13:02

user240181


People also ask

How do I show an image from my Amazon S3 on my website?

Easiest thing to do is make them public in s3, at least read-only. If you don't want them to be public on s3, for whatever reason, you could add a cloudfront distribution that will serve the images from your s3 bucket, and you can give cloudfront access to the files, without making the images public in s3.

How do I view an S3 bucket file?

In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it. The procedure for saving the object depends on the browser and operating system that you are using.


1 Answers

You can return a redirect result:

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

If the url points at the image file then it will work. Of course you shouldn't present the url of this action to user but use it in some other view as <img> src attribute value:

<img src="<%= Url.Action("Thumbnail", "ControllerName") %>" />
like image 53
PanJanek Avatar answered Sep 20 '22 23:09

PanJanek