Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Work with an Amazon S3 response stream after response has been disposed

I'm using the Amazon SDK and I have a method that returns a Stream for an object stored in Amazon S3 service.

It contains something like this:

var request = new GetObjectRequest().WithBucketName(bucketName).WithKey(keyName);
using (var response = client.GetObject(request))
{
    return response.ResponseStream;
}

Obviously, when doing this, the stream is not readable from the calling method because the request object has been disposed and when this is done, it closes the stream.

I don't want to download the file to a MemoryStream or a FileStream.

If I don't use the using clause, the garbage collector will dispose the request object at some point so I can't just not use it.

What I'm asking is, is there a way to wrap or copy the Stream into another Stream and then return it without having to download the file?

I'm using .NET 3.5.

Edit: The method is inherited from an abstract class and the caller method doesn't know it is working with Amazon. So it HAS to return a Stream.

like image 520
spakinz Avatar asked Jun 02 '11 11:06

spakinz


2 Answers

You can't work with the stream after it's disposed, but you can postpone disposing the response object until after the response stream has been used. There are three options I can suggest.

  1. Return Response. One option is to return the response object to the caller. The caller can access the contained response stream and then dispose the response when done. This is the easiest change but requires the caller to change as well.

  2. Wrap the stream. Instead of returning the response stream directly, create a new object that extends Stream and wraps the response stream but also has a reference to the response itself. Then when your wrapper is disposed, you can internally dispose the response object. This requires only changing your called code as long as the type being returned is just Stream.

  3. Callback. Don't return anything from the method and use a callback mechanism instead. Accept an Action<Stream> as a parameter and call this callback with the stream. That way the callers code is called and you still have control over disposing the response and stream. This is the safest mechanism since you're not relying on the caller to dispose anything, but requires the most changes.

like image 156
Samuel Neff Avatar answered Sep 27 '22 19:09

Samuel Neff


There is a method OpenStream in TransferUtility class which returns a stream object.

public Stream OpenStream( String bucketName, String key )

I looked through the source code of AWSSDK, and found in the SDK the OpenStream just returns the response stream directly. (It doesn't use "using" statement on response object.)

like image 26
Jun Y. Avatar answered Sep 27 '22 18:09

Jun Y.