19

I'm retrieving a file from Amazon S3. I want to convert the file to bytes so that I can download it as follows:

var download = new FileContentResult(bytes, "application/pdf");
download.FileDownloadName = filename;
return download;

I have the file here:

var client = Amazon.AWSClientFactory.CreateAmazonS3Client(
        accessKey,
        secretKey,
        config
        );
GetObjectRequest request = new GetObjectRequest();
GetObjectResponse response = client.GetObject(request);          

I know about response.WriteResponseStreamToFile() but I want to download the file to the regular downloads folder. If I convert the GetObjectResponse to bytes, I can return the file. How can I do this?

Erica Stockwell-Alpert
  • 3,957
  • 9
  • 44
  • 107

4 Answers4

22

Here's the solution I found for anyone else who needs it:

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();
    }
}
Erica Stockwell-Alpert
  • 3,957
  • 9
  • 44
  • 107
13

Just another option:

Stream rs;    
using (IAmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client())
{
    GetObjectRequest getObjectRequest = new GetObjectRequest();
    getObjectRequest.BucketName = "mybucketname";
    getObjectRequest.Key = "mykey";

    using (var getObjectResponse = client.GetObject(getObjectRequest))
    {
              getObjectResponse.ResponseStream.CopyTo(rs);
    }
}    
Alex
  • 699
  • 7
  • 17
  • The 'ResponseStream.CopyTo' is cleaner than to have to create method 'ReadStream(Stream responseStream)' as in other answer. Also you can use 'CopyToAsync' and make method async. – Riga Jan 10 '17 at 11:49
3

I struggled to get the cleaner method offered by Alex to work (not sure what I'm missing), but I wanted to do it w/o the extra ReadStream method offered by Erica (although it worked)... here is what I wound up doing:

var s3Client = new AmazonS3Client(AccessKeyId, SecretKey, Amazon.RegionEndpoint.USEast1);
    using (s3Client)
    {
        MemoryStream ms = new MemoryStream();
        GetObjectRequest getObjectRequest = new GetObjectRequest();
        getObjectRequest.BucketName = BucketName;
        getObjectRequest.Key = awsFileKey;

        using (var getObjectResponse = s3Client.GetObject(getObjectRequest))
        {
            getObjectResponse.ResponseStream.CopyTo(ms);
        }
        var download = new FileContentResult(ms.ToArray(), "image/png"); //"application/pdf"
        download.FileDownloadName = ToFilePath;
        return download;
    }
puddleglum
  • 965
  • 11
  • 18
2

Stream now has asynchronous methods. In C# 8, you can do this:

public async Task<byte[]> GetAttachmentAsync(string objectPointer)
{
    var objReq = new GetObjectRequest
    {
        BucketName = "bucket-name",
        Key = objectPointer,    // the file name
    };

    using var objResp = await _s3Client.GetObjectAsync(objReq);
    using var ms = new MemoryStream();
    await objResp.ResponseStream.CopyToAsync(ms, _ct);  // _ct is a CancellationToken
    return ms.ToArray();
}

This won't block any threads while the IO occurs.

rianjs
  • 7,317
  • 5
  • 21
  • 36