如何使用C#在S3中删除文件?

时间:2019-06-22 17:22:08

标签: c#

我正在尝试使用C#代码删除S3中的文件:

string _bucketName = "mybucket-ap-southeast-1-123627123717";
string _filename "ronaldo.png";

AmazonS3Config _config = new AmazonS3Config();
_config.ServiceURL = HOSTNAME + _bucketName;

IAmazonS3 _client = new AmazonS3Client(ACCESS_KEY, ACCESS_SECRET, _config);
await _client.DeleteObjectAsync(
    new Amazon.S3.Model.DeleteObjectRequest { BucketName = _bucketName, Key = _fileName });

该代码不会引发任何异常或错误,但是当我检查并刷新AWS S3管理控制台时,该文件仍然存在!我错过了什么?

1 个答案:

答案 0 :(得分:0)

  IAmazonS3 s3Client = new AmazonS3Client(_appConfiguration["AWS:AccessKey"], _appConfiguration["AWS:SecretKey"], RegionEndpoint.USEast1);

  var deleteObjectRequest = new DeleteObjectRequest { BucketName = "BucketName ", Key = "key"};
  s3Client.DeleteObjectAsync(deleteObjectRequest).Wait();