Download Files from S3 bucket in Amazon S3 bucket
public static async Task DownloadS3FolderFileslistAsync()
{
string bucketName = ConfigurationManager.AppSettings["AWSBucket"].ToString(); // your aws s3 bucket name
string downloadLocation = ConfigurationManager.AppSettings["download"]; // your aws s3 bucket down loadLocation
AmazonS3Client s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1);
Console.WriteLine("initializing.......step2");
TransferUtility fileTransferUtility = new TransferUtility();
//ListObjectsRequest request = new ListObjectsRequest();
//ListObjectsResponse response = s3Client.ListObjects(request.BucketName = bucketName, request.Prefix = foldername);
//AmazonS3Client s3Client = new AmazonS3Client();
// to list out all the elements inside of a director [To know more][1]
string prefix = "FolderName/";
ListObjectsRequest request = new ListObjectsRequest
{
BucketName = bucketName,
Prefix = prefix,
// Use this to exclude your directory name
// StartAfter = prefix
};
// ListObjectsResponse response = s3Client.ListObjects(request);
ListObjectsResponse response = s3Client.ListObjects(request.BucketName = bucketName, request.Prefix = prefix);
foreach (S3Object obj in response.S3Objects)
{
try
{
Console.WriteLine("folder name " + obj.Key);
string filename = downloadLocation + obj.Key.Split('/').Reverse().First();
Console.WriteLine("DestinationFolder" + Path.GetDirectoryName(downloadLocation+obj.Key));
if(!(Directory.Exists(Path.GetDirectoryName(downloadLocation + obj.Key))))
{
Directory.CreateDirectory(Path.GetDirectoryName(downloadLocation + obj.Key));
Console.WriteLine("dir name created successfully is {0}", Path.GetDirectoryName(downloadLocation + obj.Key));
}
else
{
Console.WriteLine("DestinationFolder files" + downloadLocation + obj.Key);
FileStream fstream = File.Create(downloadLocation + obj.Key);
fstream.Close();
fileTransferUtility.Download(downloadLocation + obj.Key, bucketName, obj.Key);
Console.WriteLine("Transferred Successfully {0}", downloadLocation + obj.Key);
}
}
catch (Exception Excep)
{
Console.WriteLine(Excep.Message, Excep.InnerException);
}
}
}
Upload Files to S3 Bucket
public static void UploadfilesToS3Bucket()
{
string bucketName = ConfigurationManager.AppSettings["AWSBucket"].ToString(); // your aws s3 bucket name
string keyName = ConfigurationManager.AppSettings["keyName"]; //your file name
string filePath = ConfigurationManager.AppSettings["filename"]; //@"C:\path\to\your\file.txt";
// Set up your AWS credentials
//BasicAWSCredentials credentials = new BasicAWSCredentials("ACCESS_KEY", "SECRET_KEY");
// Create a new Amazon S3 client
AmazonS3Client s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1);
try
{
// Upload the file to Amazon S3
TransferUtility fileTransferUtility = new TransferUtility(s3Client);
fileTransferUtility.Upload(filePath, bucketName, keyName);
Console.WriteLine("Upload completed!");
Console.ReadLine();
}
catch (AmazonS3Exception e)
{
Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
}
}
this was good post
ردحذف