Created
April 24, 2012 14:32
-
-
Save Dillie-O/2480125 to your computer and use it in GitHub Desktop.
Amazon S3 Download File with Prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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