Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the bytes of a GetObjectResponse from S3?

Tags:

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?

like image 926
Erica Stockwell-Alpert Avatar asked Dec 07 '15 22:12

Erica Stockwell-Alpert


People also ask

How do I get data from AWS S3?

In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it. The procedure for saving the object depends on the browser and operating system that you are using.

How do I find the size of an object on my Galaxy S3?

How to Find Bucket Size from the GUI. From the S3 Management Console, click on the bucket you wish to view. Under Management > Metrics > Storage, there's a graph that shows the total number of bytes stored over time. Additionally, you can view this metric in CloudWatch, along with the number of objects stored.

Does S3 support byte range?

Amazon S3 doesn't have any limits for the number of connections made to your bucket. Typical sizes for byte-range requests are 8 MB or 16 MB. If objects are PUT using a multipart upload, it's a good practice to GET them in the same part sizes (or at least aligned to part boundaries) for best performance.

What is checksum in S3?

Amazon S3 uses checksum values to verify the integrity of data that you upload to or download from Amazon S3. In addition, you can request that another checksum value be calculated for any object that you store in Amazon S3.


1 Answers

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();
    }
}
like image 177
Erica Stockwell-Alpert Avatar answered Sep 22 '22 04:09

Erica Stockwell-Alpert