如何使用将文件下载到带有响应的下载文件夹.是否将响应流写入文件?(亚马逊S3,C#)

本文关键字:响应 文件 S3 亚马逊 是否 文件下载 何使用 文件夹 下载 | 更新日期: 2023-09-27 18:25:58

我在S3中存储了一些PDF,我正在尝试创建一个链接来下载它们。这就是我迄今为止使用的方法:

public virtual ActionResult DownloadPDF(string filename)
{
    string secretKey = this.UnitOfWork.ApplicationSettingRepository.GetOrCreateByName<string>("StorageProvider_AmazonS3_SecretKey");
    string accessKey = this.UnitOfWork.ApplicationSettingRepository.GetOrCreateByName<string>("StorageProvider_AmazonS3_AccessKey");
    var bucket = this.UnitOfWork.ApplicationSettingRepository.GetOrCreateByName<string>("StorageProvider_AmazonS3_BucketName");
    var serviceUrl = this.UnitOfWork.ApplicationSettingRepository.GetOrCreateByName<string>("StorageProvider_AmazonS3_ServiceUrl");
    AmazonS3Config config = new AmazonS3Config();
    config.ServiceURL = serviceUrl;
    var client = Amazon.AWSClientFactory.CreateAmazonS3Client(
            accessKey,
            secretKey,
            config
            );
    GetObjectRequest request = new GetObjectRequest();
    request.BucketName = bucket;
    request.Key = "userfiles/MSD IMAGES/ProductDocumentation/" + filename;
    GetObjectResponse response = client.GetObject(request);
    response.WriteResponseStreamToFile("''Downloads''" + filename);
}

我从这里的文档中得到了这个方法,但我想下载下载到浏览器中显示,然后转到标准下载文件夹。我以前创建了这个下载csv文件的方法:

var csv = new StringBuilder();
csv.AppendLine("col1,col2,col3");
var bytes = Encoding.UTF8.GetBytes(csv.ToString());
var response = new FileContentResult(bytes, "text/csv");
response.FileDownloadName = fileName;
return response;

我试着对S3文件做同样的事情:

GetObjectResponse response = client.GetObject(request);
// response.WriteResponseStreamToFile("''Downloads''" + filename);
var bytes = Encoding.UTF8.GetBytes(response.ToString());
var download = new FileContentResult(bytes, "application/pdf");
download.FileDownloadName = filename;
return download;

它下载了一个文件,但pdf不起作用(无法加载)。如何下载我从S3获得的文件?

如何使用将文件下载到带有响应的下载文件夹.是否将响应流写入文件?(亚马逊S3,C#)

以下是我提出的工作解决方案:

GetObjectResponse response = client.GetObject(request);
using (Stream responseStream = response.ResponseStream)
{
    var bytes = ReadStream(responseStream);
    var download = new FileContentResult(bytes, "application/pdf");
    download.FileDownloadName = filename;
    return download;
}

public static byte[] ReadStream(Stream responseStream)
{
    byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}

您需要执行类似以下的操作

int byteCount
byte[] buffer = new byte[2048]; // read in chunks of 2KB
Stream responseStream= Response.GetResponseStream();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
while ((byteCount = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
    Response.OutputStream.Write(buffer, 0, byteCount);    
}
Response.Flush();
Response.Close();
Response.End();