How To Upload, Download, Delete Files In AWS S3 With .NET Core

How To Upload, Download, Delete Files In AWS S3 With .NET Core

Learn how to upload, download, and delete objects from AWS S3 in .NET Core.

Recently I have been working on a project that works with a lot of files. I ended up choosing Amazon's S3 for storing the files for various reasons, but I won't get into that for this article.

While doing research on working with S3 in .NET Core I didn't find much beyond a couple of articles that work with an older version of the AWS SDK, and some stack overflow posts. After figuring out some of the functionality I needed, I decided to write an article to help others get started with the AWS SDK and S3.

Pre-requisites

  • You should have an AWS account.
  • You should have a bucket set up in S3.
  • You should know your AWS access key and secret

Amazon S3 creating a bucket

Understanding and getting your AWS credentials

Dependencies

To get started we have to install some dependencies. There is only one dependency if all you need from the AWS SDK is functionality for S3.

  • AWSSDK.S3

s3-package.PNG

You can install this package from NuGet. At the time of writing this article, the current version is 3.7.0.13.

BasicAWSCredentials

To authenticate yourself with the AWS SDK we need to create an instance of BasicAWSCredentials.

We can create a Singleton on startup to get the credentials through dependency injection and read our keys from the appsettings.json file.

s3-startup.PNG

Upload a file

public class AmazonS3
{
    private readonly BasicAWSCredentials _awsCredentials;
    public AmazonS3(BasicAWSCredentials awsCredentials)
    {
        _awsCredentials = awsCredentials;
    }

    public async Task UploadFileAsync(IFormFile file, string fileName)
    {
        // Set RegionEndpoint to your AWS region.
        AmazonS3Config config = new()
        {
            RegionEndpoint = Amazon.RegionEndpoint.USEast2
        };

        using var client = new AmazonS3Client(_awsCredentials, config);

        await using var memStream = new MemoryStream();
        await file.CopyToAsync(memStream);

        var uploadRequest = new TransferUtilityUploadRequest
        {
            InputStream = memStream,
            Key = fileName,
            BucketName = "your-bucket-name",
            CannedACL = S3CannedACL.Private
        };

        var fileTransferUtil = new TransferUtility(client);
        await fileTransferUtil.UploadAsync(uploadRequest);
    }
}

The code for uploading a file is quite simple once you understand a bit about the AWS SDK and how S3 works.

First, we set up a configuration for what region we are in. Next, we create an S3 client and copy our file over to a MemoryStream. Finally, we pass our stream, file name, S3 bucket name, and our access control permissions to a TransferUtilityUploadRequest and call UploadAsync to upload the file to S3.

If you aren't sure about access control permissions (or ACL) you can read more here: Access Control List Overview

Get a file

public class AmazonS3
{
    private readonly BasicAWSCredentials _awsCredentials;
    public AmazonS3(BasicAWSCredentials awsCredentials)
    {
        _awsCredentials = awsCredentials;
    }

    public async Task<GetObjectResponse> GetFileAsync(string fileName)
    {
         // Set RegionEndpoint to your AWS region.
        AmazonS3Config config = new()
        {
            RegionEndpoint = Amazon.RegionEndpoint.USEast2
        };

        using var client = new AmazonS3Client(_awsCredentials, config);
        return await client.GetObjectAsync("your-bucket-name", fileName);
    }
}

To get a file from S3 the only thing we need is the name of the file. Just like before we set a configuration for the region we're in and create an instance of AmazonS3Client. We can then call GetObjectAsync with the name of our bucket and the name of the file.

The response is an instance of GetObjectResponse. This object has a ResponseStream property that is a stream of the file, which you can use to write the file to disk (or whatever else you need to do).

Delete a file

public class AmazonS3
{
    private readonly BasicAWSCredentials _awsCredentials;
    public AmazonS3(BasicAWSCredentials awsCredentials)
    {
        _awsCredentials = awsCredentials;
    }

    public async Task DeleteFileAsync(string fileName)
    {
         // Set RegionEndpoint to your AWS region.
        AmazonS3Config config = new()
        {
            RegionEndpoint = Amazon.RegionEndpoint.USEast2
        };

        using var client = new AmazonS3Client(_awsCredentials, config);
        await client.DeleteObjectAsync("your-bucket-name", fileName);
    }
}

Deleting a file is the same as retrieving a file as we did above but this time we make a call to DeleteObjectAsync. This method returns an instance of DeleteObjectResponse that gives us some information about the delete request but in this case, we just ignore the response.

Conclusion

AWS S3 is a highly scalable and performant solution for storing simple objects like files. Working with it in .NET Core is quite simple once you get the basics of the AWS SDK.

If you want to learn more: