Tuesday, January 31, 2017

C# MVC Download Big File from S3 Async

Leave a Comment

I have to download a file from aws S3 async. I have a anchor tag, on clicking it a method will be hit in a controller for download. The file should be start downloading at the bottom of the browser, like other file download.

In View

<a href="/controller/action?parameter">Click here</a> 

In Controller

public void action() {      try      {            AmazonS3Client client = new AmazonS3Client(accessKeyID, secretAccessKey);            GetObjectRequest req = new GetObjectRequest();            req.Key = originalName;            req.BucketName = ConfigurationManager.AppSettings["bucketName"].ToString() + DownloadPath;            FileInfo fi = new FileInfo(originalName);            string ext = fi.Extension.ToLower();            string mimeType = ReturnmimeType(ext);            var res = client.GetObject(req);            Stream responseStream = res.ResponseStream;            Stream response = responseStream;            return File(response, mimeType, downLoadName);      }      catch (Exception)      {            failure = "File download failed. Please try after some time.";         }               } 

The above function makes the browser to load until the file is fully downloaded. Then only the file is visible at the bottom. I cant see the how mb is downloading.
Thanks in advance.

2 Answers

Answers 1

You must send ContentLength to client in order to display a progress. Browser has no information about how much data it will receive.

If you look at source of FileStreamResult class, used by File method, it does not inform client about "Content-Length". https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/FileStreamResult.cs

Replace this,

return File(response, mimeType, downLoadName); 

with

return new FileStreamResultEx(response, res.ContentLength, mimeType, downloadName);   public class FileStreamResultEx : ActionResult{       public FileStreamResultEx(         Stream stream,          long contentLength,                  string mimeType,         string fileName){         this.stream = stream;         this.mimeType = mimeType;         this.fileName = fileName;         this.contentLength = contentLength;      }        public override void ExecuteResult(          ControllerContext context)      {          var response = context.HttpContext.Response;           response.BufferOutput = false;          response.Headers.Add("Content-Type", mimeType);          response.Headers.Add("Content-Length", contentLength.ToString());          response.Headers.Add("Content-Disposition","attachment; filename=" + fileName);           stream.CopyTo(response.OutputStream);      }  } 

Alternative

Generally this is a bad practice to download and delivery S3 file from your server. You will be charged twice bandwidth on your hosting account. Instead, you can use signed URLs to deliver non public S3 objects, with few seconds of time to live. You could simply use Pre-Signed-URL

 public ActionResult Action(){      try{          using(AmazonS3Client client =                new AmazonS3Client(accessKeyID, secretAccessKey)){             var bucketName =                   ConfigurationManager.AppSettings["bucketName"]                 .ToString() + DownloadPath;             GetPreSignedUrlRequest request1 =                 new GetPreSignedUrlRequest(){                   BucketName = bucketName,                   Key = originalName,                   Expires = DateTime.Now.AddMinutes(5)                };              string url = client.GetPreSignedURL(request1);             return Redirect(url);          }      }      catch (Exception)      {          failure = "File download failed. Please try after some time.";         }                } 

As long as object do not have public read policy, objects are not accessible to users without signing.

Also, you must use using around AmazonS3Client in order to quickly dispose networks resources, or just use one static instance of AmazonS3Client that will reduce unnecessary allocation and deallocation.

Answers 2

As i understand, you want to make something like "reverse proxy" from your server to S3. Very userful article how to do that with Nginx you can find here: https://coderwall.com/p/rlguog/nginx-as-proxy-for-amazon-s3-public-private-files

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment