Determine Who Uploaded a File to S3
Introduction
In this article, we are going to explore nearly how to upload, download and delete the file(s) from AWS S3 and check if the file(south) exists or not in AWS S3 using .Cyberspace Core Web API.
AWS S3
The Amazon Simple Storage Service (Amazon S3) is an object storage service offering industry-leading scalability, data availability, security, and performance. Please refer to the link for more details near the AWS S3 service.
Topics Covered
This article covers the following topics,
- Create a sample ASP.NET Core Web API to,
- Upload file to AWS S3
- Download file from AWS S3
- Delete files from AWS S3
- Bank check If the file is available or non in the AWS S3
- Exam the Web API
Prerequisites
- Download and install Visual Studio 2019.
- Download and install the AWS Toolkit for Visual Studio 2019.
- The user should accept an active AWS Account.
- The user should have programmatic access keys. Please refer to the link for getting the access keys from AWS account.
Create a sample ASP.NET Core Spider web API
Follow the below steps to create an ASP.Net Core Web API using Visual Studio 2019.
Step 1
Open Visual Studio 2019, click Create a new projection.
Step two
Select ASP.NET Core Web Application projection template and click Next.
Stride 3
Enter the project name as DotNetCore_AWS_Demo and Click Next.
Step 4
Select .Cyberspace Cadre 3.1 and Empty project and Click Create.
Step five
Install AWSSDK.S3 package using the Nuget Bundle Director.
Step 6
Create Controller with name AwsS3Controller.
Pace 7
Create IAppConfiguration interface and class name as AppConfiguration.
Step 8
Update the course with post-obit code. This class is used to configure the S3 bucket and AWS access fundamental details.
IAppConfiguration.cs
public interface IAppConfiguration { string AwsAccessKey { get; set; } cord AwsSecretAccessKey { go; set up; } string AwsSessionToken { get; set; } string BucketName { go; set; } string Region { get; set; } }
AppConfiguration .cs
public class AppConfiguration : IAppConfiguration { // Keep the following details in appsettings.config file or DB or Enivironment variable // Become those values from it and assign to the below varibales. Based on the arroyo , modify the below code. public AppConfiguration() { BucketName = ""; Region = ""; AwsAccessKey = ""; AwsSecretAccessKey = ""; AwsSessionToken = ""; } public string BucketName { go; gear up; } public string Region { get; gear up; } public string AwsAccessKey { get; set; } public string AwsSecretAccessKey { go; ready; } public string AwsSessionToken { get; set; } }
Step 9
Register AppConfiguration class in ConfigureServices() method in the Startup class.
services.AddSingleton<IAppConfiguration, AppConfiguration>()
Step x
In the AwsS3Controller controller, add together the constructor for injecting the IAppConfiguration dependency in order to fetch the configuration values.
[ApiController] [Route("documents")] public class AwsS3Controller : ControllerBase { individual readonly IAppConfiguration _appConfiguration; private readonly IAws3Services _aws3Services; public AwsS3Controller(IAppConfiguration appConfiguration) { _appConfiguration = appConfiguration; } }
Step 11
Create IAws3Services interface and Aws3Servicesgrade and in the constructor, create object for AmazonS3Client to access the AWS S3 bucket.
IAws3Services.cs
public interface IAws3Services { Task<byte[]> DownloadFileAsync(string file); Task<bool> UploadFileAsync(IFormFile file); Task<bool> DeleteFileAsync(string fileName, string versionId = ""); }
Aws3Services.cs
public form Aws3Services : IAws3Services { private readonly string _bucketName; private readonly IAmazonS3 _awsS3Client; public Aws3Services(string awsAccessKeyId, string awsSecretAccessKey, string awsSessionToken, cord region, string bucketName) { _bucketName = bucketName; _awsS3Client = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, awsSessionToken, RegionEndpoint.GetBySystemName(region)); } }
Download File from S3
Create GetDocumentFromS3 action (GET method) in the AwsS3Controller controller and pass the filename every bit a parameter and so that the filename will check and retrieve the bodily file from the S3 saucepan. The different results of the GetDocumentFromS3 activity method are,
- If the filename parameter is null or empty, and then Bad Request(400) error response will be returned to the user.
- If the file is available on the S3 so the file will be downloaded from S3 (status code is 200).
- If the file is non available in the S3 then Not Plant (404) error response volition exist returned.
AwsS3Controller.cs
[HttpGet("{documentName}")] public IActionResult GetDocumentFromS3(string documentName) { try { if (cord.IsNullOrEmpty(documentName)) return ReturnMessage("The 'documentName' parameter is required", (int)HttpStatusCode.BadRequest); _aws3Services = new Aws3Services(appConfiguration.AwsAccessKey, appConfiguration.AwsSecretAccessKey, appConfiguration.AwsSessionToken, appConfiguration.Region, appConfiguration.BucketName); var document = _aws3Services.DownloadFileAsync(documentName).Result; return File(document, "awarding/octet-stream", documentName); } catch (Exception ex) { render ValidateException(ex); } }
Create a new role called DownloadFileAsync in the Aws3Servicesclass. We are going to utilise AmazonS3Client'due south GetObjectAsync method to download the required file. GetObjectAsync method is used to retrieve objects from Amazon S3. Please refer to the link for more details about GetObjectAsync .
Create GetObjectRequest object, which has details of S3 bucket and file name. Pass the GetObjectRequest to GetObjectAsync method to download the file. If the file is downloaded successfully, then return the file to the user else render not plant exception to the user.
Aws3Services .cs
public async Task<byte[]> DownloadFileAsync(string file) { MemoryStream ms = zippo; endeavour { GetObjectRequest getObjectRequest = new GetObjectRequest { BucketName = _bucketName, Cardinal = file }; using (var response = await _awsS3Client.GetObjectAsync(getObjectRequest)) { if (response.HttpStatusCode == HttpStatusCode.OK) { using (ms = new MemoryStream()) { await response.ResponseStream.CopyToAsync(ms); } } } if (ms is null || ms.ToArray().Length < one) throw new FileNotFoundException(cord.Format("The certificate '{0}' is not found", file)); return ms.ToArray(); } catch (Exception) { throw; } }
Upload File to S3
Create UploadDocumentToS3 activeness (POST method) in the AwsS3Controller controller and laissez passer the actual file (type is IFormFile) as parameter. The different results of the UploadDocumentToS3 action method are,
- If the file is cypher or empty, then Bad Request(400) error response will be returned to the user.
- If the file is valid then uploaded the file to S3 (condition code is 200).
AwsS3Controller.cs
[HttpPost] public IActionResult UploadDocumentToS3(IFormFile file) { try { if (file is naught || file.Length <= 0) render ReturnMessage("file is required to upload", (int)HttpStatusCode.BadRequest); _aws3Services = new Aws3Services(appConfiguration.AwsAccessKey, appConfiguration.AwsSecretAccessKey, appConfiguration.AwsSessionToken, appConfiguration.Region, appConfiguration.BucketName); var result = _aws3Services.UploadFileAsync(file); return ReturnMessage(string.Empty, (int)HttpStatusCode.Created); } catch (Exception ex) { return ReturnMessage(ex.Message, (int)HttpStatusCode.InternalServerError); } }
Create a new function UploadFileAsync in the Aws3Servicesgrade. We are going to use TransferUtility for uploading the file to S3.
TransferUtility provides a simple API for uploading content to or downloading content from Amazon S3. Please refer to the link for more details nearly TransferUtility.
Create TransferUtilityUploadRequest object, which has details of the saucepan and the file to be uploaded. Pass the TransferUtilityUploadRequest object to TransferUtility's UploadAsync method to upload the file.
Aws3Services .cs
public async Task<bool> UploadFileAsync(IFormFile file) { try { using (var newMemoryStream = new MemoryStream()) { file.CopyTo(newMemoryStream); var uploadRequest = new TransferUtilityUploadRequest { InputStream = newMemoryStream, Key = file.FileName, BucketName = _bucketName, ContentType = file.ContentType }; var fileTransferUtility = new TransferUtility(_awsS3Client); await fileTransferUtility.UploadAsync(uploadRequest); return true; } } catch (Exception) { throw; } }
Delete Files in the S3
Create DeletetDocumentFromS3 action (DELETE method) in the AwsS3Controller controller and pass the filename equally parameter. The dissimilar results of the UploadDocumentToS3 action method are,
- If the filename parameter is aught or empty, so Bad Request(400) error response will exist returned to the user.
- If the file is available in the S3 then deleted the file and render the success response (status code is 200).
- If the file is not bachelor in the S3 then Non Found (404) error response volition be returned.
AwsS3Controlle.cs
[HttpDelete("{documentName}")] public IActionResult DeletetDocumentFromS3(cord documentName) { endeavour { if (string.IsNullOrEmpty(documentName)) return ReturnMessage("The 'documentName' parameter is required", (int)HttpStatusCode.BadRequest); _aws3Services = new Aws3Services(appConfiguration.AwsAccessKey, appConfiguration.AwsSecretAccessKey, appConfiguration.AwsSessionToken, appConfiguration.Region, appConfiguration.BucketName); _aws3Services.DeleteFileAsync(documentName); return ReturnMessage(cord.Format("The document '{0}' is deleted successfully", documentName)); } grab (Exception ex) { render ValidateException(ex); } }
Create a new office chosen DeleteFileAsync in the Aws3Services grade. File name and the version id (If S3 bucket is enabled versioning, then pass version id else pass empty) are parameter for this function.
We are going to use AmazonS3Client'southward DeleteObjectAsync method to delete the file in the S3. Please refer to the link for more details well-nigh DeleteObjectAsync.
Create DeleteObjectRequest object, which has details of S3 bucket and file proper noun. Pass the DeleteObjectRequest to DeleteObjectAsync method to perform a delete functioning.
Note: Earlier going to delete, we need to cheque if the file is available or not. If not, so return "Not Institute" exception otherwise delete the file. To check the file availability is an optional i. Depending on the use case, nosotros tin can add this functionality to it.
Aws3Services.cs
[HttpDelete("{documentName}")] public IActionResult DeletetDocumentFromS3(cord documentName) { effort { if (string.IsNullOrEmpty(documentName)) return ReturnMessage("The 'documentName' parameter is required", (int)HttpStatusCode.BadRequest); _aws3Services = new Aws3Services(appConfiguration.AwsAccessKey, appConfiguration.AwsSecretAccessKey, appConfiguration.AwsSessionToken, appConfiguration.Region, appConfiguration.BucketName); _aws3Services.DeleteFileAsync(documentName); return ReturnMessage(cord.Format("The certificate '{0}' is deleted successfully", documentName)); } grab (Exception ex) { return ValidateException(ex); } } private async Job DeleteFile(string fileName, string versionId) { DeleteObjectRequest request = new DeleteObjectRequest { BucketName = _bucketName, Primal = fileName }; if (!cord.IsNullOrEmpty(versionId)) request.VersionId = versionId; wait _awsS3Client.DeleteObjectAsync(request); } public bool IsFileExists(string fileName, string versionId) { attempt { GetObjectMetadataRequest asking = new GetObjectMetadataRequest() { BucketName = _bucketName, Cardinal = fileName, VersionId = !cord.IsNullOrEmpty(versionId) ? versionId : zero }; var response = _awsS3Client.GetObjectMetadataAsync(asking).Result; render true; } catch (Exception ex) { if (ex.InnerException != null && ex.InnerException is AmazonS3Exception awsEx) { if (string.Equals(awsEx.ErrorCode, "NoSuchBucket")) render imitation; else if (string.Equals(awsEx.ErrorCode, "NotFound")) render false; } throw; } }
Exam the Web API
Now let's run the application and test the above functionality through Swagger. Swagger (OpenAPI) is a language-agnostic specification for describing REST APIs. Swagger UI offers a spider web-based UI that provides data about the service, using the generated OpenAPI specification.
To enable the swagger nosotros need to follow the below steps.
Pace 1
Install Swashbuckle.AspNetCore package using the NuGet Bundle Manager
Step 2
Add the Swagger generator to the services drove in the Startup.ConfigureServices method.
public void ConfigureServices(IServiceCollection services) { services .AddSingleton<IAppConfiguration, AppConfiguration>() .AddSwaggerGen() .AddControllers(); }
In the Startup.Configure method, enable the middleware for serving the generated JSON document and the Swagger UI.
app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "DotNet_AWS"); });
Step 3
Hit F5 to run the API locally and To launch the Swagger just hit the http://localhost:<port_number>/swagger/index.html URL in the browser. You will get the below Swagger UI.
Just expand the required operation and click "Try it out" button.
Enter the input values and click the "Execute" button.
Download the File (Become Method)
Upload the File (Post Method)
Delete the File (Delete Method)
Summary
In this article, you learned virtually how to implement the post-obit functionality using .NET Core Web API.
- Download the file from S3
- Upload the file to S3
- Delete the file in the S3
Source: https://www.c-sharpcorner.com/article/uploaddownloaddelete-files-from-aws-s3-using-net-core-api/
Post a Comment for "Determine Who Uploaded a File to S3"