Working With AWS S3

Introduction 

 
One of the most important factors of using Cloud is STORAGE. Storage that we use as we need is one of the biggest factors in using the cloud. There are many storage services offered by AWS, however, S3 is the most popular among them all.
 
In this article, we are going to discuss the below points:
  1. What is cloud storage?
  2. What is AWS S3?
  3. AWS S3 Benefits
  4. Practical Example

What is Cloud Storage?

 
Cloud storage is the service (Web service in case of AWS) offered by cloud providers where you can store and access data on the internet. It is more scalable, reliable, fast, and secure than on-premises storage.
 
There are many storage services offered by AWS, listed below:
  • S3 – Simple Storage Service
  • EFS – Elastic File System
  • Storage Gateway
  • Snowball
  • Snowball Age
  • EBS – Elastic Block Storage
  • Glacier
  • Snowmobile
Companies once had a very tough time maintaining and managing data. Managing data consists of running the application, giving access to the customers, backups, etc. There were huge challenges, like procurement of hardware and software, a skilled team to maintain data, and scalability and data security are the main challenges. Almost all of the above challenges were solved by the AWS S3 storage service.
 
S3 – “Simple Storage Service” is a service that gives the capability to store and access any amount of data at any time or place. It also gives the developer access or facility to highly reliable, scalable, fast and inexpensive data storage.
 

AWS S3 Benefits 

 
You have 2 options to use this service:
  • Pay for what you use
  • Pay on a monthly basis 
Benefits
  • Scalability
  • Low cost
  • Durability
  • Availability
  • Flexibility
  • Security
  • Simple Data Transfer
It is designed for developers, and gives easy access over the internet. Developers can do all the activities through code or IDE through the ASW console. ASW S3 provides 99.99% availability and durability.
 
Practical Example 
 
Now let’s see how developers can use the S3 service to store and get data.
 
Step 1 
 
First, you need to create an S3 Bucket. Log in to your AWS Console and create an S3 bucket. It’s very easy and only a few steps.
 
Step 2 
 
To access the S3 bucket you need Access Key ID and Secret Access Key. For that, you need to go to Access Management inside the IAM service and create New Access Key. You can use this link directly below is the screenshot for your reference. Also, note down your Access Key and Secret Key.
 
 
Step 3 
 
Install package of AWS S3 through code or Visual Studio Install-Package AWSSDK.S3 -Version 3.5.8.7
 
Step 4 
 
Install package of AWS Core through code or Visual Studio Install-Package AWSSDK.Core -Version 3.5.3.1
 
Step 5 
 
By adding the above packages, you will be able to few namespaces of AWS which you will need to access the S3 bucket to store and retrieve data, as shown below: 
  1. using Amazon;    
  2. using Amazon.S3;    
  3. using Amazon.S3.Transfer;    
  4. using Amazon.S3.IO;    
Step 6 
 
Add 3 keys in your Web.config AWSAccessKey, AWSSecretKey, and AWSBucketName and set their respective values.
Step 7 
 
Now we are going to upload the file to the S3 bucket:
  1. IAmazonS3 client = new AmazonS3Client(AWSAccessKey, AWSSecretKey, RegionEndpoint.APSoutheast2);  
  2. TransferUtility utility = new TransferUtility(client);  
  3.                 TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();  
  4.   
  5. //This way you can create object of directory and check whether directory is exist or not in your S3 bucket  
  6. S3DirectoryInfo di = new S3DirectoryInfo(client, AWSBucketName, path);  
  7.       if (!di.Exists)  
  8.       {  
  9.            di.Create();  
  10.       }  
  11. //To upload file I used Base64String you can use MemoryStream directly.  
  12. byte[] newBytes = Convert.FromBase64String(fileBase64String);  
  13. MemoryStream ms = new MemoryStream(newBytes, 0, newBytes.Length);  
  14. ms.Write(newBytes, 0, newBytes.Length);  
  15. request.InputStream = ms;  
  16. utility.Upload(request);  
  17. //This way you can upload the file in your S3 bucket folder  
Step 8
 
Now we are going to download from S3 bucket. There is very small code to download the file, as shown below:
  1. IAmazonS3 client = new AmazonS3Client(AWSAccessKey, AWSSecretKey, RegionEndpoint.APSoutheast2);  
  2.                     TransferUtility fileTransferUtility = new TransferUtility(client);  
  3.                     fileTransferUtility.Download(FileLocation, AWSBucketName, path);  
  4.                     fileTransferUtility.Dispose();  

This way we can get access S3 storage service to store and retrieve data easily.

--Happy Coding-- 


Similar Articles
Logiciel Softtech Pvt. Ltd.
We help you transform your business ideas with custom products, migrate from legacy applications.