Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in AWS by (5.6k points)

 I have a .net web API that downloads objects from Amazon S3 to the disk using AWS SDK lib.

using Amazon.S3;

using Amazon.S3.Model;

using System.IO;

private void DownloadObject()

{

    BasicAWSCredentials awsCredentials = new Amazon.Runtime.BasicAWSCredentials("MyAccessKey", "MySecretKey");

    IAmazonS3 client = new Amazon.S3.AmazonS3Client(awsCreden‌​tials, Amazon.RegionEndpoint.USEast1);

    GetObjectRequest request = new GetObjectRequest

    {

        BucketName = "mybucket",

        Key = "test.pdf"

    };

    using (GetObjectResponse response = await client.GetObjectAsync(request))

    {

        using (Stream responseStream = response.ResponseStream)

        {

           using (StreamReader reader = new StreamReader(responseStream))

           {

               string responseBody = await reader.ReadToEndAsync();

               File.WriteAllText("C:\\test.pdf", responseBody);

           }

        }

    }

}

 Now when I download the files, the file size is too big and If I open a pdf all pages are blank and if I open png file I can't open them.

Anyone can help me to resolve this issue? 

1 Answer

0 votes
by (12.4k points)

use the below code and try to work hope this works for you.

using (GetObjectResponse response = await client.GetObjectAsync(request))

{

    using (Stream responseStream = response.ResponseStream)

    using (FileStream outFile = File.Create("C:\\test.pdf"))

    {

        responseStream.CopyTo(outFile);

    }

}

Do checkout AWS Certification to learn more about AWS.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer

Browse Categories

...