I generate a list of filenames that I need to download from S3. Everything works fine but it breaks when it encounters a file that does not exist. When I try to list the contents of the bucket, I get an access denied error even though this should work fine, according to the vendor... so I gave up on trying to list contents.
Instead, is there a way I can just catch the error for the async method when it tries to download something that doesn t exist without it crashing my program? I d like to just move onto the next file in the list if one fails.
The error I get when I try a file that doesn t exist:
Amazon.S3.AmazonS3Exception: Access Denied --->
Amazon.Runtime.Internal.HttpErrorResponseException: The remote server returned an error: (403)
Forbidden. ---> System.Net.WebException: The remote server returned an error: (403) Forbidden. at
System.Net.HttpWebRequest.GetResponse() at Amazon.Runtime.Internal.HttpRequest.GetResponse() ---
End of inner exception stack trace
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Compression;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.IO;
using System.Threading;
namespace AlgoSeekDecompressor
{
class Program
{
static void Main(string[] args)
{
var list = new List<string>()
{
"Apple.csv",
"Banana.csv",
"Canteloupe.csv",
};
var downloadPath = @"E:My_Folder";
Task.Run(async () => { await DownloadFilesAsync(list, downloadPath); }).Wait();
Console.WriteLine("Done downloading");
}
static async Task DownloadFilesAsync(List<string> list, string downloadPath)
{
var accessKey = "#################";
var secretAccessKey = "#########################";
var client = new AmazonS3Client(
accessKey,
secretAccessKey,
RegionEndpoint.USEast1
);
foreach (var file in list)
{
var bucketName = "amazon_s3_bucket";
//There is a prefix that is the first letter of the filename
var filename = file.Substring(0, 1).ToUpper() + "/" + file + ".csv.gz";
var request = new GetObjectRequest()
{
BucketName = bucketName,
Key = filename,
RequestPayer = RequestPayer.Requester
};
if (!Directory.Exists(downloadPath))
{
Directory.CreateDirectory(downloadPath);
Console.WriteLine("Directory {0} does not exist - creating directory", downloadPath);
}
var filePath = Path.Combine(downloadPath, filename);
var appendToFile = false;
using (var response = await client.GetObjectAsync(request))
{
await response.WriteResponseStreamToFileAsync(filePath, appendToFile, CancellationToken.None);
}
}
}
}
}