How to download entire folder present inside s3 bucket using .net sdk.Tried with below code, it throws invalid key.I need to download all files present inside nested pesudo folder present inside bucket and removing file download limitations to 1000 which is default.
public static void DownloadFile()
{
var client = new AmazonS3Client(keyId, keySecret, bucketRegion);
ListObjectsV2Request request = new ListObjectsV2Request
{
BucketName = bucketName + "/private/TargetFolder",
MaxKeys = 1000
};
try
{
ListObjectsV2Response bucketResponse = client.ListObjectsV2(request);
foreach (S3Object o in bucketResponse.S3Objects)
{
var getRequest = new GetObjectRequest
{
BucketName = bucketResponse.Name + "/private/TargetFolder",
Key = bucketResponse.Name +"/private/TargetFolder/"+ o.Key
};
var response = client.GetObject(getRequest);
response.WriteResponseStreamToFile(downloadLocation + "\\" + o.Key);
var responseCode = response.HttpStatusCode;
if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
Console.WriteLine($"Success downloaded : {o.Key}");
}
else if (response.HttpStatusCode == System.Net.HttpStatusCode.RequestTimeout)
{
Console.WriteLine("Request Timeout error.");
}
else if (response.HttpStatusCode == System.Net.HttpStatusCode.ServiceUnavailable)
{
Console.WriteLine("Service Unavailable.");
}
else if (response.HttpStatusCode == System.Net.HttpStatusCode.InternalServerError)
{
Console.WriteLine("Internal Server error.");
}
else
{
Console.WriteLine("Please check the provided AWS Credentials.");
}
}
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
Console.WriteLine("Please check the provided AWS Credentials.");
}
else
{
Console.WriteLine(amazonS3Exception.Message);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
Thanks in advance!
Use the s3 cp command with the --recursive parameter to download an S3 folder to your local file system. The s3 cp command takes the S3 source folder and the destination directory as inputs and downloads the folder.
Use the cp command to download a folder inside a bucket from S3 to local. recursive option will download all files and folders if you have a recursive folder/file structure.
Unfortunately AWS S3 Console doesn't have an option to download all the content of an S3 bucket at the moment, but there are other options like AWS CLI to do so. For instance: aws s3 sync s3://all_my_stuff_bucket . This command will start downloading all the objects in all_my_stuff_bucket to the current directory.
In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it.
If you are always getting 0 in S3Objects.Count, try without using Delimiter property:
public async Task DownloadDirectoryAsync()
{
var bucketRegion = RegionEndpoint.USEast2;
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var client = new AmazonS3Client(credentials, bucketRegion);
var bucketName = "bucketName";
var request = new ListObjectsV2Request
{
BucketName = bucketName,
Prefix = "directorey/",
MaxKeys = 1000
};
var response = await client.ListObjectsV2Async(request);
var utility = new TransferUtility(s3Client);
var downloadPath = "c:\\your_folder";
foreach (var obj in response.S3Objects)
{
utility.Download($"{downloadPath}\\{obj.Key}", bucketName, obj.Key);
}
}
And of course, you need s3:ListBucket permission
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