0 votes
1 view
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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...