Uploading a Video to S3 .net Core
In previous commodity, I take mentioned about file upload in asp.internet core mvc (single or multiple), now in this article, I take provided step by step procedure to upload file on Amazon Web Service (AWS) S3 bucket using C# in ASP.NET Core MVC.
Pace ane: Create a ASP.Net Core MVC new project in your Visual Studio 2019 or 2017, I am using 2019 version so opening it then selecting Create new Project, select "ASP.NET Core MVC" template, so click "Adjacent"
Name your project as shown above and click Adjacent, go on the settings as it is, we are using .Cyberspace 5.0 version every bit you can see in the below paradigm
Click "Create" and allow Visual Studio Generate necessary files.
Footstep 2: Install Amazon S3 Nuget packet, since nosotros will be uploading files on AWS S3, we will install it'south SDK, which we will make things easier for us, and so navigate to "Tools" -> "Nuget Packet Manager" -> "Manage Nuget packages for solution" -> Select "Browse" and then search for "AWSSDK.S3", you will see the package equally shown below in the image and and so install on your project/solution, past clicking "Install" push button
Step 3: We need to Create bucket on Amazon S3, if you lot have already created bucket, you can note downwardly it's name and region, otherwise, Open up Amazon Console , Sign in and Search for "S3", open "S3" and then Select "Create" -> Enter "New Bucket" name and select "AWS region", equally shown below
Notation: your bucket name must be unique overall
Pace four: Now, we demand to go Amazon S3 Secrey primal and admission cardinal, which you can create using IAM roles in Amazon Panel
- Open up the IAM panel at https://console.aws.amazon.com/iam/
- On the navigation carte, choose "Users".
- Cull your IAM user name or Create new User, Give permission to users if you are creating new User, check below images for new User
Assign Roles, I have assigned Full S3 access function
- In one case, y'all have created new User or select User.
- Open the Security credentials tab, and and so choose Create access key.
- To see the new access cardinal, choose Prove. Your credentials resemble the following:
- Access cardinal ID: AWS1234567890
- Underground access key: wjair/K7MDaENaaaG/K7MDasdadsENG
Step five: Now, we accept access and cloak-and-dagger key, saucepan proper noun and region, at present, navigate to your HomeController.cs file and utilize the below code
[HttpPost] public async Job<IActionResult> UploadFileToS3(IFormFile file) { // access fundamental id and undercover key id, can exist generated by navigating to IAM roles in AWS and so add new user, select permissions //for this example, try giving S3 full permissions using (var client = new AmazonS3Client("yourAccesKey", "yourSecretKey", RegionEndpoint.USWest2)) { using (var newMemoryStream = new MemoryStream()) { file.CopyTo(newMemoryStream); var uploadRequest = new TransferUtilityUploadRequest { InputStream = newMemoryStream, Cardinal = file.FileName, // filename BucketName = "aws-s3-qawithexperts" // bucket name of S3 }; var fileTransferUtility = new TransferUtility(customer); await fileTransferUtility.UploadAsync(uploadRequest); } } ViewBag.Success = "File Uploaded on S3"; return View("Index"); } So, complete HomeController.cs lawmaking will look similar below
using Amazon; using Amazon.S3; using Amazon.S3.Transfer; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Organisation.IO; using System.Threading.Tasks; namespace AmazonS3FileUpload.Controllers { public class HomeController : Controller { public IActionResult Index() { render View(); } [HttpPost] public async Task<IActionResult> UploadFileToS3(IFormFile file) { // access key id and surreptitious central id, tin be generated by navigating to IAM roles in AWS so add new user, select permissions //for this case, effort giving S3 total permissions using (var client = new AmazonS3Client("yourAccesKey", "yourSecretKey", RegionEndpoint.USWest2)) { using (var newMemoryStream = new MemoryStream()) { file.CopyTo(newMemoryStream); var uploadRequest = new TransferUtilityUploadRequest { InputStream = newMemoryStream, Key = file.FileName, // filename BucketName = "aws-s3-qawithexperts" // saucepan name of S3 }; var fileTransferUtility = new TransferUtility(customer); look fileTransferUtility.UploadAsync(uploadRequest); } } ViewBag.Success = "File Uploaded on S3"; return View("Index"); } } } Step vi: Navigate to "Alphabetize.cshtml" and use the below lawmaking, which is course created to submit file to controller method "UploadFiletoS3"
@{ ViewData["Title"] = "Abode Page"; } <grade enctype="multipart/course-data" method="mail service" asp-controller="Home" asp-activity="UploadFileToS3"> <dl> <dt> <label>Upload File to S3</characterization> </dt> <dd> <input type="file" name="file" id="file"> </dd> </dl> <input form="btn" type="submit" value="Upload" /> </form> @if(ViewBag.Success != cipher) { <div>@ViewBag.Success</div> } That's it, we are done, I have provided below gif image which sample file uploading to S3 using C# code in ASP.NET Cadre MVC
In the above image, yous can meet there is no file on S3 Saucepan initially, merely after selecting file on .NET Cadre folio, information technology is uploaded on S3 and can be viewed in image.
You lot may too like to read:
Majority Insert in ASP.NET Cadre MVC using Entity Framework Core
Treeview in ASP.NET Core MVC
Model Validation in ASP.NET Cadre MVC
Creating GridView in ASP.Cyberspace Cadre MVC with Paging
Grade Submit in ASP.Internet Core MVC
Resize paradigm in C# (Panel application example)
Encrypt and Decrypt files in C#
Source: https://qawithexperts.com/article/asp-net/file-upload-on-aws-s3-using-c-in-aspnet-core/394
0 Response to "Uploading a Video to S3 .net Core"
Post a Comment