title: How do I efficiently read AWS S3 object and return as view model
body: <p>I am storing json bits on S3 from an ASP.NET MVC app hosted on AppHarbor.  I am able to read, write, delete, etc. to S3 without any issue.  Most interactions are quite fast.  However, when reading the content out something in my current implementation is going quite slow.  </p>
  
  <p>The code in the controller is super simple making a call directly into the repo that talks to S3:</p>
  
  <pre><code>public class ContentController : Controller
  {
      public ActionResult LoadContent(string slug)
      {
          IContentRepository repo = new S3ContentRepository(ConfigurationManager.AppSettings["BucketName"]);
          var content = repo.GetContent(slug);
          return View(content);
      }
  
  }
  </code></pre>
  
  <p>In the repo I have the read from S3 working quite fast.  It takes the object out of S3, puts it into memory, assembles it as a string, then converts it to json,  then converts the json back into my model.  No lag here.</p>
  
  <pre><code>public Content GetContent(string slug)
      {
          Content result = null;
          using (client = Amazon.AWSClientFactory.CreateAmazonS3Client())
          {
              try
              {
                  GetObjectRequest request = new GetObjectRequest().WithBucketName(_bucketName).WithKey(slug);
  
                  using (GetObjectResponse response = client.GetObject(request))
                  {
                      string title = response.Metadata["x-amz-meta-title"];
  
                      byte[] arr = new byte[response.ContentLength];
                      response.ResponseStream.BeginRead(arr, 0, Convert.ToInt32(response.ContentLength), null, null);
                      string json = new System.Text.ASCIIEncoding().GetString(arr);
                      result = JsonConvert.DeserializeObject&lt;Content&gt;(json);
                      return result;
                  }
              }
  
              ...
  
          }
      }
  </code></pre>
  
  <p>However, once I return the model out of the repo back to the controller the request seriously lags.  The performance is the same on my local machine as it is in AppHarbor.  Literally seconds go by before the page loads.  The view code is just generated from the class that is its model...very simple.</p>
  
  <p>Thoughts?</p>
  
  <p><strong>Update 1:</strong>  In poking about a bit more I am finding that the lag is somewhere in the closing of the response stream.  I have added a specific call to "close" the stream...and this is where the lag is felt most.  This was previously left to the using statement to handle the death of the response object.  More poking is required...</p>
  
  <p><strong>Update 2:</strong>  I didn't give up on this as I think I should be able to read the object from S3 directly into memory without any issues but in the interest of time I swapped my implementation to write to the local system from S3 then load the local file into memory.  Pretty lame..but good enough until I solve this issue!  I will keep poking for a better solution.  Although with the file local I can implement a "fail gracefully" solution in the off chance that S3 stops responding...file based cache!  :)</p>