从Amazon S3流式传输文件 [英] Streaming files from amazon s3

查看:1204
本文介绍了从Amazon S3流式传输文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试从Amazon s3流文件时遇到问题.基本上,我将文件存储在amazom s3上,由于需要验证用户身份,因此我无法直接访问这些文件.我试图找到一种流式传输文件的方法,而无需将每个文件从亚马逊下载到我的服务器上,然后再从我的服务器下载到最终客户端.我只希望能够直接流式传输文件,但是.NET中的大多数流读取器类似乎都不是这样做的.如果我错了,请纠正我.

I have a problem trying to stream files from amazon s3. Basically, I have files stored on amazom s3, I can't provide direct access to these files as users need to be authenticated. I'm trying to find a way to stream files without downloading each file from amazon onto my server and then from my server to the end client. I just want to be able to stream the file direct, but it seems most of the stream reader classes in .NET are not cable of doing this. Please correct me if I am wrong.

推荐答案

您可以通过打开到Amazon S3文件的流,然后通过服务器通过服务器将文件从Amazon S3流传输到客户端,而无需将文件下载到服务器.从中写入客户端流(逐个缓冲区).

You can stream the file from Amazon S3 to the client through your server without downloading the file to your server, by opening a stream to the Amazon S3 file then read from it and write on the client stream (buffer by buffer).

示例代码:

byte[] buffer = new byte[BUFFER_SIZE];                
GetObjectRequest getObjRequest = new GetObjectRequest().WithBucketName(Bucket_Name).WithKey(Object_Key);

using (GetObjectResponse getObjRespone = amazonS3Client.GetObject(getObjRequest))
using (Stream amazonStream = getObjRespone.ResponseStream)
{
    int bytesReaded = 0;        
    Response.AddHeader("Content-Length", getObjRespone.ContentLength.ToString());

    while ((bytesReaded = amazonStream.Read(buffer, 0, buffer.Length)) > 0 && Response.IsClientConnected)
    {
        Response.OutputStream.Write(buffer, 0, bytesReaded);
        Response.OutputStream.Flush();
        buffer = new byte[BUFFER_SIZE];
    }
}

这篇关于从Amazon S3流式传输文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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