Sahil
Sahil

Reputation: 511

Error when downloading file from amazon s3

I am trying to download files from amazon s3. I am using .NET sdk for that. I have written down follwong code:

GetObjectRequest request = new GetObjectRequest()
    .WithKey("abc/xyz.jpg")
    .WithBucketName(appConfig);

using (GetObjectResponse response = s3Client.GetObject(request))
{
    int numBytesToRead = (int)response.ContentLength;
    if (numBytesToRead > 0)
    {
        int numBytesRead = 0;
        byte[] buffer = new byte32768;
        using (FileStream fstream = new FileStream(@"C:\abc\xyz.jpg", FileMode.Create))
        {
            using (Stream responseStream = response.ResponseStream)
            {
                do
                {
                    numBytesRead = responseStream.Read(buffer, 0, buffer.Length);
                    fstream.Write(buffer, 0, numBytesRead);
                }
                while (numBytesRead > 0);
            }
        }
    }
}

With this code I am able to download file. But now I want to download multiple files. For that first I am listing down all the files which are available on s3 and then store it in arraylist and after that call my download function for each file.

After running list file method when I call download function after using (GetObjectResponse response = s3Client.GetObject(request)) I get exception.

{"Access Denied"}

Stack trace:
at Amazon.S3.AmazonS3Client.processRequestError(String actionName, HttpWebRequest request, WebException we, HttpWebResponse errorResponse, String requestAddr, WebHeaderCollection& respHdrs, Type t, Exception& cause)
at Amazon.S3.AmazonS3Client.handleHttpWebErrorResponse(S3Request userRequest, WebException we, HttpWebRequest request, HttpWebResponse httpResponse, Exception& cause, HttpStatusCode& statusCode)
at Amazon.S3.AmazonS3Client.getResponseCallback[T](IAsyncResult result)
at Amazon.S3.AmazonS3Client.endOperation[T](IAsyncResult result)
at Amazon.S3.AmazonS3Client.EndGetObject(IAsyncResult asyncResult)
at Amazon.S3.AmazonS3Client.GetObject(GetObjectRequest request)
at ScreenshotsUploader.ScreenshotsUploader.downloadFile(AmazonS3 s3Client, NameValueCollection appConfig) in c:\Users\xyz\Documents\Visual Studio 2012\Projects\ScreenshotsUploader\ScreenshotsUploader\ScreenshotsUploader.cs:line 220
at ScreenshotsUploader.ScreenshotsUploader.GetServiceOutput() in c:\Users\xyz\Documents\Visual Studio 2012\Projects\ScreenshotsUploader\ScreenshotsUploader\ScreenshotsUploader.cs:line 55

Status code:

System.Net.HttpStatusCode.Forbidden

What do I need to do to fix this?

I have also tried different approach to download file and got the same error:

Second approach:

string fileToDownload = @"xyzjpg";
string saveToo = @"C:\xyz.jpg";
TransferUtilityDownloadRequest request = new TransferUtilityDownloadRequest().WithBucketName(appConfig).WithKey(fileToDownload).WithFilePath(saveToo);
TransferUtility tu = new TransferUtility(s3Client);
tu.Download(request);

Thanks in advance.

Upvotes: 6

Views: 10725

Answers (1)

Dipanki Jadav
Dipanki Jadav

Reputation: 390

public Stream DownloadS3Object(string awsBucketName, string keyName)
{

    using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client())
    {
        Stream imageStream = new MemoryStream();
        GetObjectRequest request = new GetObjectRequest { BucketName = awsBucketName, Key = keyName };
        using (GetObjectResponse response = client.GetObject(request))
        {
            response.ResponseStream.CopyTo(imageStream);
        }
        imageStream.Position = 0;
        // Clean up temporary file.
        // System.IO.File.Delete(dest);
        return imageStream;
    }
}

pass the value in function get stream path save it in folder with use of below.

SaveStreamToFile(foldername + "/", MainStreamAwsPath);

and than you can apply simple c# code to download that folder.

Upvotes: 5

Related Questions