Upload, Download, Delete files on amazon S3 cloud in C#

Learn Upload, Download, Delete files on amazon S3 cloud in CSharp C# here.

Step By Step Tutorial to Upload, Download, Delete

files on amazon S3 cloud in CSharp C#

This Tutorial completely explains Upload, Download, Delete files on amazon S3 cloud in CSharp C# and step by step How to upload a file on amazon s3 cloud using C#? How to download a file from Amazon S3 Cloud using C#? And How to Delete a File from amazon s3 cloud using C#?

Amazon S3 (Simple Storage Service) Cloud:

Amazon S3 Cloud or Amazon Simple Storage Service is an online storage for the Internet. The Cloud Service is especially designed to make web scale computing or development easier for web developers.

The Amazon simple storage service has a very simple web services architecture or interface that developers can use easily to retrieve and store and any size of data, from anywhere, at any time on the web. The Cloud Service gives any developer access to the same fast, reliable and highly scalable, cheaper data storage architecture that Amazon Company uses to run its own global network of web sites. The storage service tries to maximize the benefits of scale and also to pass those benefits to web developers.

This tutorial explains step by step core concepts of Amazon S3 and Upload, Download, Delete files on amazon S3 cloud in CSharp, its application programming interface (API) and also explains How to upload a file on amazon s3 cloud using C#? How to download a file from Amazon S3 Cloud using C#? And How to Delete a File from amazon s3 cloud using C#?

Before You go forward, These three credentials you must need to know.

  • Your Amazon Bucket Name
  • Your Access Key ID
  • And the Secret Access Key

For These Credential you may go to your dashboard and create a new app and get these credentials.TheLink for Dashboard is here .

http://aws.amazon.com/console

Upload, Download, Delete files on amazon S3 cloud in CSharp

Put the above discussed credentials in your projects app.config file as Follows,

Uploading a file on Amazon S3 using C#:

Step1)

 

Create a New Class with any name you want “CAmazon”

Create amazon API  client Like this, select your region carefully.

IAmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(RegionEndpoint.USEast1);

Create a function to upload file on amazon s3 cloud.

public bool UploadFile(string localFilePath, string bucketName, string subDirectoryInBucket, string fileNameInS3)
{
TransferUtility utility = new TransferUtility(client);
// making a TransferUtilityUploadRequest instance
TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();

if (subDirectoryInBucket == "" || subDirectoryInBucket == null)
{
request.BucketName = bucketName; //no subdirectory just bucket name
}
else
{ // subdirectory and bucket name
request.BucketName = bucketName + @"/" + subDirectoryInBucket;
}
request.Key = fileNameInS3; //file name up in S3
request.FilePath = localFilePath; //local file name
utility.Upload(request); // commencing the transfer

return true;
}

Step2)

Call the function from main()

static void Main(string[] args)
{
//Upload a file
string fileToUpload = @"D:\TestFile.txt"; // test file
string myBucketName = "punch-scraped-data"; //your s3 bucket name goes here
string s3DirectoryName = null; // your directory name in se bucket
string s3FileName = @"TestFile.txt"; //file name in s3 bucket
CAmazon myUploader = new CAmazon(); //object of class that contains function
bool b = myUploader.UploadFile(fileToUpload, myBucketName, s3DirectoryName, s3FileName);
if (b == true)
{
Console.WriteLine("Uploaded Successfully");
Console.ReadLine();
}
else
{
Console.WriteLine("Not Uploaded");
Console.ReadLine();
}
}

Downloading a file from Amazon S3 using C#:

Step1)

Create a function in separate class named “CAmazon” to download file from amazon s3 cloud.

public bool DownloadFile(string file) //file is the file name in amazon s3 bucket
{
IAmazonS3 client2;
using (client2 = new AmazonS3Client(Amazon.RegionEndpoint.USEast1))
{
GetObjectRequest request = new GetObjectRequest
{
BucketName = "punch-scraped-data",
Key = file
};
using (GetObjectResponse response = client2.GetObject(request))
{
string dest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), file);
if (!File.Exists(dest))
{
response.WriteResponseStreamToFile(dest);
}
}
}
return true;
}

Step2)

Call the function from main()

static void Main(string[] args)
{
//Download a file
CAmazon myUploader = new CAmazon();
bool b = myUploader.DownloadFile("TestFile.txt");
if (b == true)
{
Console.WriteLine("downloaded Successfully");
Console.ReadLine();
}
else
{
Console.WriteLine("Not Downloaded");
Console.ReadLine();
}
}

 

Delete a file from Amazon S3 bucket using C#:

Step1)

Create a function in separate class named “CAmazon” to delete file from amazon s3 cloud.

public bool DeleteFile(string file)
{
string accessKeyID = System.Configuration.ConfigurationManager.AppSettings["AWSAccessKey"];
string secretAccessKeyID = System.Configuration.ConfigurationManager.AppSettings["AWSSecretKey"];
IAmazonS3 client3;
client3 = new AmazonS3Client(Amazon.RegionEndpoint.USEast1);
DeleteObjectRequest deleteObjectRequest =
new DeleteObjectRequest
{
BucketName = "punch-scraped-data",
Key = file
};
using (client3 = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID,Amazon.RegionEndpoint.USEast1))
{
client3.DeleteObject(deleteObjectRequest);
}
return true;
}

Step2)

Call the function from main()

static void Main(string[] args)
{
//Delete a File
CAmazon myUploader = new CAmazon();
bool b = myUploader.DeleteFile("TestFile.txt");
if (b == true)
{
Console.WriteLine("deleted Successfully");
Console.ReadLine();
}
else
{
Console.WriteLine("Not Deleted");
Console.ReadLine();
}
}

Download the Complete Code of Upload, Download, Delete Files on Amazon S3 Cloud in C# Just by Clicking the Below download Button.

 

 

 

Download the Complete Code of Upload, Download, Delete files on amazon S3 cloud in CSharp Just by Clicking the Below download Button.

One Reply to “Upload, Download, Delete files on amazon S3 cloud in C#”

Comments are closed.

Verified by MonsterInsights