AWS S3文件在下载时没有内容

时间:2018-01-22 23:47:06

标签: c# amazon-s3

我正在尝试将S3中的文件直接传递给用户'下载这样的流:

public HttpResponseMessage Download()
{
    var result = new HttpResponseMessage(HttpStatusCode.OK);
    var stream = new MemoryStream();

    using (var client = new AmazonS3Client(RegionEndpoint.USWest2))
    {
        var request = new GetObjectRequest
        {
            BucketName = "bucket",
            Key = "filename"
        };

        using (var response = client.GetObject(request))
        {
            response.ResponseStream.CopyTo(stream);
        }
    }

    result.Content = new StreamContent(stream);
    result.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);

    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = "file.pdf"
    };

    return result;
}

但文件下载为0字节。我假设文件已完全加载到ResponseStream.CopyTo行的内存中。

0 个答案:

没有答案