TechieClues TechieClues
Updated date Aug 27, 2022
This article explains how to delete the files from the AWS S3 bucket using ASP.NET Core

 In this article, we will see how to delete the file from AWS S3 Bucket using ASP.NET Core. Please read the below articles before continuing to this article,

How to delete files from AWS S3 Bucket using ASP.NET Core?

Step 1: 

We have created the S3 bucket in AWS and uploaded the file as shown below,

Step 2: 

Create a new controller, right-click "Controller" folder -> Add -> Controller as shown below,

Enter the controller name in the below dialog and click Add.

Step 3:

Add the below code in the new controller "DeleteS3File" as shown below,

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace AwsFileOperationsCore.Controllers
{
    [Route("api/delete")]
    [ApiController]
    public class DeleteS3File : Controller
    {      
        [HttpPost]
        public async Task <IActionResult> DeleteFile(string fileName)
        {
            try
            {
                // You can generate the 'access key id' and 'secret key id, when you create the IAM user in AWS.
                // Region endpoint should be same as the bucket region
                using (var amazonS3client = new AmazonS3Client("ACCESS_KEY_ID", "SECRET_KEY_ID", RegionEndpoint.APSoutheast2))
                {
                    var transferUtility = new TransferUtility(amazonS3client);
                    // Delete the object using DeleteObjectAsync method
                    await transferUtility.S3Client.DeleteObjectAsync(new DeleteObjectRequest()
                    {
                        // Bucket Name
                        BucketName = "my-bucket-1425",
                        // File Name
                        Key = fileName
                    });
                  
                    return Ok("Success");
                }
             
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || 
                    amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    throw new Exception("Please check the AWS Credentials.");
                }
                else
                {
                    throw new Exception(amazonS3Exception.Message);
                }
            }
        }
    }
}

You have to copy the Access key ID and Secret access key from the AWS console (see this post for creating the user, access key id, and secret access key) and paste it into the above place. The region endpoint should be the same as your S3 bucket region. Also, don't forget to update the bucket name.

  • DeleteObjectAsync is used to delete the specified objects from the AWS S3 bucket.
  • TransferUtility provides a simple API for uploading content to or downloading content from Amazon S3. 

Step 4: 

Open postman and enter the below URL and select POST and execute,

https://localhost:<PORT_NUMBER>/api/delete?fileName=<FILE_NAME>

Once you executed the above URL in the postman, the specified file will be deleted in the AWS S3 bucket.

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!