Skip to content

Instantly share code, notes, and snippets.

@Dillie-O
Created April 24, 2012 14:32
Show Gist options
  • Save Dillie-O/2480125 to your computer and use it in GitHub Desktop.
Save Dillie-O/2480125 to your computer and use it in GitHub Desktop.
Amazon S3 Download File with Prompt
public static void DownloadObject(string keyName)
{
string[] keySplit = keyName.Split('/');
string fileName = keySplit[keySplit.Length - 1];
string dest = Path.Combine(HttpRuntime.CodegenDir, fileName);
using (client = Amazon.AWSClientFactory.CreateAmazonS3Client())
{
GetObjectRequest request = new GetObjectRequest().WithBucketName(bucketName).WithKey(keyName);
using (GetObjectResponse response = client.GetObject(request))
{
response.WriteResponseStreamToFile(dest, false);
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.TransmitFile(dest);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
// Clean up temporary file.
System.IO.File.Delete(dest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment