One of the best tricks to improve performance of your website is to reduce the server load.
So how can you reduce server processing time?
If you observe closely, most of your page load time is getting consumed by images and videos of that page. Instead of keeping all resources on one server, you can put some resources such as heavy images and videos on other server. This other server will be treated as CDN server and this technique is called CDN.
This post has everything from soup to nuts about uploading media files such as video or image on Amazon S3.
What is Amazon S3?
Amazon S3 has a simple web services interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, fast, inexpensive data storage infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize benefits of scale and to pass those benefits on to developers.
So how can you reduce server processing time?
If you observe closely, most of your page load time is getting consumed by images and videos of that page. Instead of keeping all resources on one server, you can put some resources such as heavy images and videos on other server. This other server will be treated as CDN server and this technique is called CDN.
This post has everything from soup to nuts about uploading media files such as video or image on Amazon S3.
What is Amazon S3?
Amazon Simple Storage Service is storage for the Internet. It is designed to make web-scale computing easier for developers.
I borrowed above definition from Amazon's official website. If you are looking for more details about it, then you can refer this link.
Enough theory, let's start some practicals...
Step-1: Installation
Install AWSSDK NuGet package from Visual Studio.
Step-2: Code Snippet
Once you install AWSSDK, you can now use Amazon S3 APIs to upload images or videos to the Amazon server. But for that you will need below three things.
- Amazon S3 Bucket URL
- Amazon S3 Access Key
- Amazon S3 Secret Key
In my case, I have created a dashboard screen from where user can upload any video to Amazon S3.
Here is the code to upload selected video to Amazon S3 bucket.
protected void btnSubmit_Click(object sender, EventArgs e) { try { if (videoFile.HasFile) { IAmazonS3 client; AmazonS3Config a = new AmazonS3Config(); using (client = AWSClientFactory.CreateAmazonS3Client(ConfigurationManager.AppSettings["AWSAccessKey"], ConfigurationManager.AppSettings["AWSSecretKey"], RegionEndpoint.APSoutheast1)) { if (ConfigurationManager.AppSettings["AmazonS3BucketName"] != null && ConfigurationManager.AppSettings["AmazonS3BucketURL"] != null) { string bucketName = ConfigurationManager.AppSettings["AmazonS3BucketName"]; string AmazonS3BucketURL = ConfigurationManager.AppSettings["AmazonS3BucketURL"]; PutObjectRequest request = new PutObjectRequest(); request.BucketName = bucketName; request.CannedACL = S3CannedACL.PublicRead; request.Key = videoFile.FileName; request.InputStream = videoFile.PostedFile.InputStream; PutObjectResponse response = client.PutObject(request); Video objVideo = new Video(); objVideo.Title = txtTitle.Text; objVideo.Description = txtDesc.Text; objVideo.Url = AmazonS3BucketURL + videoFile.FileName; objVideo.CategoryId = Int32.Parse(lstCat.SelectedItem.Value); objVideo.IsActive = chkIsActive.Checked; objVideo.Language = lstLang.SelectedItem.Value; PepsiBeINUclBAL objBAL = new PepsiBeINUclBAL(); int i = objBAL.InsertVideoDashBoard(objVideo); if (i >= 1) { lblMsg.Text = "Video added sucessfully"; } else { lblMsg.Text = "Oops.. some error occured while submitting video."; } } } } } catch (Exception ex) { lblMsg.Text = "Oops.. some error occured while submitting video."; } }
Step-3: AWS Key Configuration
Define all required Amazon S3 keys and URLs in web.config file, so that you can change them later on if you want.
Step-4: Amazon S3 File Browser
Finally you will be thinking how to confirm that media is successfully uploaded on Amazon S3 bucket and looking for URL of the same.
It's simple. Amazon S3 URL for the uploaded media will be like this:
AmazonS3BucketURL + "/" + [Uploaded Media File Name]
For exanple:
https://youramazonaccountprefix.s3.amazonaws.com/video1.mp4
If you are looking for a way to browse all media files uploaded so far on your Amazon S3 bucket, then Amazon S3 Browser can help you on that. You can download Amazon S3 Browser from here
That's it!
Hope this article will give you everything to get start with Amazon S3 upload.
No comments:
Post a Comment