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.
You can download an object from an S3 bucket in any of the following ways: Select the object and choose Download or choose Download as from the Actions menu if you want to download the object to a specific folder. If you want to download a specific version of the object, select the Show versions button.
The "403 Forbidden" error can occur due to the following reasons: Permissions are missing for s3:PutObject to add an object or s3:PutObjectAcl to modify the object's ACL. You don't have permission to use an AWS Key Management Service (AWS KMS) key. There is an explicit deny statement in the bucket policy.
If you're getting Access Denied errors on public read requests that are allowed, check the bucket's Amazon S3 Block Public Access settings. Review the S3 Block Public Access settings at both the account and bucket level. These settings can override permissions that allow public read access.
If you're trying to host a static website using Amazon S3, but you're getting an Access Denied error, check the following requirements: Objects in the bucket must be publicly accessible. S3 bucket policy must allow access to the s3:GetObject action. The AWS account that owns the bucket must also own the object.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With