DEV Community

Cover image for Upload File to AWS S3 using .NET Web API
Bervianto Leo Pratama
Bervianto Leo Pratama

Posted on

Upload File to AWS S3 using .NET Web API

Hello!

I want to bring some "guide" how to use AWS SDK .NET, especially using S3. Simple code that you can follow, please navigate to here, in there you will use .NET console.

Preparation

  • Please install .NET first. You can choose either .NET 5 or .NET Core 3.1.

https://dotnet.microsoft.com/download

(Optional) .NET 6 currently in the RC stage, if you want to try it.

https://dotnet.microsoft.com/download/dotnet/6.0

  • Prepare your project. You can use this command. (if you are already have the project, just skip this step)

dotnet new webapi -o AWSS3Example

Note: In case you can't find the webapi (when use .NET 6), you can use template from .NET 3.1 since in this article using existing project and the existing project using .NET 3.1 and only change the TargetFramework to .net6.0.

  • Add AWS SDK S3 to your project. You can use this command.

dotnet add AWSS3Example package AWSSDK.S3

dotnet add AWSS3Example package AWSSDK.Extensions.NETCore.Setup

Note: You can change AWSS3Example with your project name.

  • Prepare your AWS Environment and S3

I will not bring many steps. You can follow AWS documentation how to setup. As example, you can follow this (the step before setup project): https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/quick-start-s3-1-cross.html

Coding Time

  • I want to write the interface for writing file to S3, so if AWS SDK S3 have major changed, you only change the implementation of the interface. I will write the interface at AWSS3Example/Services/AWS/IAWSS3Service.cs.
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace AWSS3Example.Services.AWS
{
  public interface IAWSS3Service
  {
    Task<string> UploadFile(IFormFile formFile);
  }
}

Enter fullscreen mode Exit fullscreen mode
  • Write the implementation. Location AWSS3Example/Services/AWS/AWSS3Service.cs.
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Amazon.S3;
using Amazon.S3.Model;

namespace AWSS3Example.Services.AWS
{
  public class AWSS3Service : IAWSS3Service
  {
    private IAmazonS3 _s3Client;
    public AWSS3Service(IAmazonS3 amazonS3)
    {
      _s3Client = amazonS3;
    }
    public async Task<string> UploadFile(IFormFile formFile)
    {
      var location = $"uploads/{formFile.FileName}";
      using (var stream = formFile.OpenReadStream())
      {
        var putRequest = new PutObjectRequest
        {
          Key = location,
          BucketName = "upload-test-berv",
          InputStream = stream,
          AutoCloseStream = true,
          ContentType = formFile.ContentType
        };
        var response = await _s3Client.PutObjectAsync(putRequest);
        return location;
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
  • Write the controller. I write at AWSS3Example/Controllers/AWSS3Controller.cs.
using System.Threading.Tasks;
using AWSS3Example.Services.AWS;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

namespace AWSS3Example.Controllers
{
  [ApiController]
  [Route("api/[controller]")]
  public class SThreeController : ControllerBase
  {
    private readonly IAWSS3Service _awsS3Service;
    public SThreeController(IAWSS3Service awsS3Service)
    {
      _awsS3Service = awsS3Service;
    }

    [HttpPost("upload")]
    public async Task<IActionResult> Upload([FromForm] IFormFile file)
    {
      var result = await _awsS3Service.UploadFile(uploadFile.File);
      return Ok(new
      {
        path = result
      });
    }
  }
}

Enter fullscreen mode Exit fullscreen mode
  • Update the Startup.cs
using Amazon.S3;
using AWSS3Example.Services.AWS;
// ... the rest of another using
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace AWSS3Example
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // the rest of another register service ...
            services.AddScoped<IAWSS3Service, AWSS3Service>();
            services.AddAWSService<IAmazonS3>();
            // the rest of another register service ...
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            // the rest of your configure, no need to modify this...
        }
    }
}

Enter fullscreen mode Exit fullscreen mode
  • Run your project.

dotnet run --project AWSS3Example

  • Test your API

Note: In my case, I use different path, so please use the path that you define at Controller.

Postman Test

  • Check your S3

S3

Repository

You can check here for the repository.

GitHub logo bervProject / NETCoreAPIBoilerplate

Net Core Web API Boilerplate for My Project

NET Core API Boilerplate (BervProject.WebApi.Boilerplate)

Net Core Web API Boilerplate for My Project

Build Status

Github Action Azure Pipelines Codecov
Build&Dockerize Build Status codecov

LICENSE

MIT

MIT License
Copyright (c) 2019 Bervianto Leo Pratama's Personal Projects

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
Enter fullscreen mode Exit fullscreen mode

Congrats

Congrats. You are finished build API for upload file to AWS S3. Thank you. Hope you enjoy and if have another suggestion, feel free comment in here.

Unsplash

Image source unsplash.

Top comments (0)