Download/Delete file from AWS S3 Bucket in ASP.NET Core/MVC/C#

preview_player
Показать описание
How to Download/Delete file from AWS S3 Bucket in ASP.NET Core/MVC/C#.

To Create Bucket in AWS & Transfer / Upload file into AWS, Please check below link.

Sample code for Download/Delete
--------------------------------------------------------
using System;
using Amazon.S3.Transfer;
using Amazon;
using Amazon.S3.Model;
using System.Configuration;

namespace AWS
{
class Program
{
public static void Main(string[] args)
{
Program program = new Program();
program.DownloadFile();
program.DeleteFile();
}

//Download a file from aws s3 bucket into our app..
public void DownloadFile()
{
var accesskey = ConfigurationManager.AppSettings["AWSAccessKey"];
var secretkey = ConfigurationManager.AppSettings["AWSSecretAccessKey"];
RegionEndpoint bucketRegion = RegionEndpoint.APSouth1;
var bucketName = ConfigurationManager.AppSettings["AWSS3BucketName"];

using (TransferUtility transferUtility = new Amazon.S3.Transfer.TransferUtility(accesskey, secretkey, bucketRegion))
{
TransferUtilityDownloadRequest downloadRequest = new TransferUtilityDownloadRequest
{
BucketName = bucketName,
FilePath = filePath,
};
transferUtility.Download(downloadRequest);

}
}
//deleting a file in AWS S3 bucket.
public void DeleteFile()
{
var accesskey = ConfigurationManager.AppSettings["AWSAccessKey"];
var secretkey = ConfigurationManager.AppSettings["AWSSecretAccessKey"];
RegionEndpoint bucketRegion = RegionEndpoint.APSouth1;
var bucketName = ConfigurationManager.AppSettings["AWSS3BucketName"];

var client = new Amazon.S3.AmazonS3Client(accesskey, secretkey, bucketRegion);

DeleteObjectRequest request = new DeleteObjectRequest
{
BucketName = bucketName,

};
var response = client.DeleteObjectAsync(request).Result;
var IsDeleted = response.DeleteMarker;
}
}
}
Рекомендации по теме
Комментарии
Автор

Thank you man, that helped me a lot, you have a future in it. Keep up the hard work.

muhammadhannanyaqoob
Автор

Please help me with How to read an excel file and insert it into the table using c#(WCF service).

AppalanaiduKondala
Автор

I am not able to string filePath = "F:\\audiofromaws.wav"; not tet

sarveshkumarpandey
Автор

Sir Thank you, i am getting this error can you please help me
Amazon.S3.AmazonS3Exception: 'The specified bucket is not valid.'
WebException: The remote server returned an error: (400) Bad Request.

jayvlogs
Автор

hello, I'm getting the following error: The BucketName specified is null or empty!'
. But there are files inside the msm and the name is also correct

adrianahenriqueferreira