Quick Tip: How to Download a File from Amazon S3 and Prompt the User Using .Net

Quick Tip

Amazon S3 is a wonderfully simple and powerful way to have mass storage at your disposal in the cloud. Amazon provides a wonderful SDK with great examples on how to do the common tasks. However, I ran into one issue I didn’t see an example for, and spend the better part of a day piecing my way though. How do download a file from S3 using a web application and prompt the user on what to do with the file.

By itself, it doesn’t seem like there would be an issue with automatically downloading the file. However, if the user doesn’t know where the file has been downloaded to, or wants to change the location, problems can occur. Amazon does allow you to change the response headers in the GetObject() method, but this didn’t seem to work for me.

The solution I found was to download the file from S3 into a temporary location on the web server, and then serve the file to the user in the HttpResponse using the TransmitFile() method. One advantage to this is that you could potentially setup some local caching with the file, or even do some additional checking with the file, before sending it on to the user. Here’s what the final code looks like. This assumes you already have the AWS SDK for .Net configured in your application, if not, you’ll need to set that up first. The other thing to note is that our bucket may have a pseudo folder structure in place by use of “/” characters between “folders”. The code will parse that to get the actual file name to properly name it upon download.


9 thoughts on “Quick Tip: How to Download a File from Amazon S3 and Prompt the User Using .Net

  1. Thank you very much for this post. I’ve been bashing my head against the wall all day trying to figure this out. I think it might be because the stream retrieved from Amazon is actually and MD5Stream and thus may not read out to a standard stream properly. I’m not sure though and that is just a guess .:) Either way, thanks again for your post. It’s a shame there wasn’t a simpler way.

    1. I’m not quite sure what you’re asking. If you are referring to where the user downloads their file, then that will be selected by the user itself when they receive the download prompt.

      If you’re talking about where the .Net app code downloads the file temporarily before presenting it to the user, you’ll want to update the “dest” variable to specify your location. Note: depending on where that location is, the web server may not have permissions to access the file afterwards to give to the user, that’s why the standard HttpCodeGen folder is used.

  2. It says unable to find credentials when i try to download to a client system, but downloads in server .how can this be solved ?

    1. Have you properly initialized your AWS client with the credentials? This code assumes you have already done that. There is documentation out there on how to do this if you are unsure. In my case the AWS keys were in the web.config file, which the library automatically knew to retrieve and use.

What are your 10 bits on the matter? I want to know!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s