58771

Get object from AWS S3 as a stream

I am trying to to figure out whether it is possbile to return some sort of stream (possibly a memory stream?) of an object I get from my AWS S3 bucket.

The S3 bucket contains a lot of different type of images, documents etc. All those should be used on my website. However, I do not want to display the path to my AWS S3 bucket. That is why I am trying to create a stream and display the images and downloadable documents on the fly rather than with a full path. Does this make sense? :-)

I am using the C#/.NET AWS SDK.

Looking forward to hear about any ideas and directions pointed to!

public FileStream GetFile(string keyName) { using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast2)) { GetObjectRequest request = new GetObjectRequest { BucketName = bucketName, Key = keyName }; using (GetObjectResponse response = client.GetObject(request)) using (Stream responseStream = response.ResponseStream) using (StreamReader reader = new StreamReader(responseStream)) { // The following outputs the content of my text file: Console.WriteLine(reader.ReadToEnd()); // Do some magic to return content as a stream } } }

Answer1:

In .NET 4, you can use Stream.CopyTo to copy the content of the ResponseStream (that is a Amazon.Runtime.Internal.Util.MD5Stream) to a MemoryStream.

GetObjectResponse getObjRespone = client.GetObject(Bucket, objectName); MemoryStream stream = new MemoryStream(); getObjRespone.ResponseStream.CopyTo(stream); return stream;

Where client.GetObject(Bucket, objectName) is an alternative to calling GetObject with the request you are creating.

Recommend

  • Javascript working on Firefox but not in Chrome and IE6
  • download a zip file from a url
  • How do you create a sitemap index in Django?
  • how to prevent download when the pdf load to iframe?
  • Python code to establish an ssh to remote server and then connect to MYSQL
  • Jquery sending key/value paired array to PHP
  • Yii2: How to show checked values in CheckboxList
  • Alsa api: how to use mmap in c?
  • Optimal way to set pixel data?
  • Facebook Logout Confusion
  • how to download csv with fusion charts in codeigniter
  • How do I split this string into an array?
  • PDF: How to Optimize Filesize & Convert to PNG (embedded fonts problem)
  • Visualizing a large matrix in matlab
  • Perspective projection, 4 points
  • Can my PDF ping my server when it is opened?
  • Retrieving specified columns from a list of csv files to create a data data frame in R
  • Filter strings with regex before casting to numeric
  • jQuery Orbit - How to make a Random Slideshow?
  • Low TTL with Leveled Compaction, should I reduce gc_grace_seconds to improve read performance withou
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Does Mobilefirst provide a provision to access web services directly?
  • Android application: how to use the camera and grab the image bytes?
  • How to revert to previous XCode version?
  • Loading .coffee files via a view in Rails
  • What and where is mdimport
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • How to get Eclipse Oxygen to run on Java 9
  • print() is showing quotation marks in results
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Android fill_parent issue
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Cross-Platform Protobuf Serialization
  • Validaiting emails with Net.Mail MailAddress
  • Do I've to free mysql result after storing it?
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • How to get Windows thread pool to call class member function?