删除桶
DeleteBucket操作用于删除bucket,删除一个bucket前,需要先删除该bucket中的全部对象(包括object versions和delete markers)。
代码示例:
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class DeleteBucketExample
{
public static async Task DeleteBucket()
{
var accessKey = "YOUR_ACCESS_KEY";
var secretKey = "YOUR_SECRET_KEY";
var endpoint = "YOUR_ENDPOINT";
var bucketName = "EXAMPLE_BUCKET";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var deleteBucketRequest = new DeleteBucketRequest()
{
BucketName = bucketName
};
var result = await s3Client.DeleteBucketAsync(deleteBucketRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.NoContent)
{
Console.WriteLine("fail to delete bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
Console.WriteLine("delete bucket {0} success.", bucketName);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
DeleteBucketRequest可设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | bucket的名称。 | 是 |
响应结果:
HTTP状态码 | 响应码 | 描述 |
---|---|---|
204 | NoContent | 删除bucekt成功。 |
403 | Forbidden | 用户没有权限执行操作。 |
404 | NotFound | 用户删除的bucekt不存在。 |
409 | Conflict | 删除的bucekt不为空。 |