处置响应后,使用Amazon S3响应流 [英] Work with an Amazon S3 response stream after response has been disposed

查看:146
本文介绍了处置响应后,使用Amazon S3响应流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Amazon SDK,并且有一种方法可以为存储在Amazon S3服务中的对象返回流.

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

它包含以下内容:

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.

我不想将文件下载到MemoryStream或FileStream.

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

如果我不使用using子句,则垃圾收集器会在某个时候处理请求对象,所以我不能只是不使用它.

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.

我要问的是,有没有一种方法可以将Stream包装或复制到另一个Stream中,然后无需下载文件就将其返回?

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?

我正在使用.NET 3.5.

I'm using .NET 3.5.

:该方法是从抽象类继承的,而调用方方法不知道它可以在Amazon上使用.因此它必须返回一个Stream.

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.

推荐答案

在处置流之后,您将无法使用它,但是可以推迟处置响应对象,直到使用了响应流之后.我可以建议三个选项.

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. 返回响应..一种选择是将响应对象返回给调用方.调用者可以访问所包含的响应流,然后在完成后处置响应.这是最简单的更改,但也要求调用者也进行更改.

  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.

包装流.创建直接扩展Stream并包装响应流但还具有对响应本身的引用的新对象,而不是直接返回响应流.然后,在处理包装时,可以在内部处理响应对象.只要返回的类型只是Stream.

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.

回调..不要从方法中返回任何内容,而应使用回调机制.接受Action<Stream>作为参数,并使用流调用此回调.这样,调用方代码就会被调用,您仍然可以控制响应和流的处理方式.这是最安全的机制,因为您不需要依靠调用方来处理任何事情,而是需要进行最多的更改.

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.

这篇关于处置响应后,使用Amazon S3响应流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆