我正在尝试删除散布在不同区域的大量存储桶。我很难删除存储桶中的所有对象。
此命令失败:
var objects = client.ListObjectsAsync(new ListObjectsRequest() { BucketName = bucket.BucketName }).Result;
有错误:
您要访问的存储桶必须使用指定的端点进行寻址。请将所有以后的请求发送到该端点
我猜这是因为我的客户端在不同的区域初始化了。
如何动态地从存储桶设置区域获取对象?
这是我的完整代码:
var client = new AmazonS3Client(credentials, RegionEndpoint.USEast1);
var buckets = client.ListBucketsAsync().Result;
foreach (var bucket in buckets.Buckets)
{
if (!keepList.Contains(bucket.BucketName))
{
var location = client.GetBucketLocationAsync(new GetBucketLocationRequest() { BucketName = bucket.BucketName }).Result;
var objects = client.ListObjectsV2Async(new ListObjectsV2Request() { BucketName = bucket.BucketName }).Result;
while (true)
{
if (objects.IsTruncated)
{
objects = client.ListObjectsV2Async(new ListObjectsV2Request() { BucketName = bucket.BucketName, ContinuationToken = objects.NextContinuationToken}).Result;
}
else
{
break;
}
}
var response = client.DeleteBucketAsync(new DeleteBucketRequest()
{
BucketName = bucket.BucketName,
BucketRegion = location.Location
}).Result;
}
}
答案 0 :(得分:0)
我找不到解决方法,我不得不将字符串静态映射到新的客户端实例以进行删除。
public static void DeleteFromRegion(string location, string bucketName, AWSCredentials credentials)
{
AmazonS3Client client = null;
ListObjectsResponse objects = null;
switch (location)
{
case "us-west-1":
client = new AmazonS3Client(credentials, RegionEndpoint.USWest1);
objects = client.ListObjectsAsync(new ListObjectsRequest() { BucketName = bucketName }).Result;
DeleteObjects(client, objects, bucketName);
break;
case "us-west-2":
client = new AmazonS3Client(credentials, RegionEndpoint.USWest2);
objects = client.ListObjectsAsync(new ListObjectsRequest() { BucketName = bucketName }).Result;
DeleteObjects(client, objects, bucketName);
break;
case "":
case "us-east-1":
client = new AmazonS3Client(credentials, RegionEndpoint.USEast1);
objects = client.ListObjectsAsync(new ListObjectsRequest() { BucketName = bucketName }).Result;
DeleteObjects(client, objects, bucketName);
break;
case "us-east-2":
client = new AmazonS3Client(credentials, RegionEndpoint.USEast2);
objects = client.ListObjectsAsync(new ListObjectsRequest() { BucketName = bucketName }).Result;
DeleteObjects(client, objects, bucketName);
break;
case "eu-west-1":
client = new AmazonS3Client(credentials, RegionEndpoint.EUWest1);
objects = client.ListObjectsAsync(new ListObjectsRequest() { BucketName = bucketName }).Result;
DeleteObjects(client, objects, bucketName);
break;
case "eu-west-2":
client = new AmazonS3Client(credentials, RegionEndpoint.EUWest2);
objects = client.ListObjectsAsync(new ListObjectsRequest() { BucketName = bucketName }).Result;
DeleteObjects(client, objects, bucketName);
break;
case "eu-west-3":
client = new AmazonS3Client(credentials, RegionEndpoint.EUWest3);
objects = client.ListObjectsAsync(new ListObjectsRequest() { BucketName = bucketName }).Result;
DeleteObjects(client, objects, bucketName);
break;
case "eu-north-1":
client = new AmazonS3Client(credentials, RegionEndpoint.EUNorth1);
objects = client.ListObjectsAsync(new ListObjectsRequest() { BucketName = bucketName }).Result;
DeleteObjects(client, objects, bucketName);
break;
case "eu-south-1":
client = new AmazonS3Client(credentials, RegionEndpoint.EUSouth1);
objects = client.ListObjectsAsync(new ListObjectsRequest() { BucketName = bucketName }).Result;
DeleteObjects(client, objects, bucketName);
break;
default:
throw new Exception("Region not recognized");
}
}
public static void DeleteObjects(AmazonS3Client client, ListObjectsResponse objects, string bucketName)
{
while (true)
{
var kv = new List<KeyVersion>();
foreach (var o in objects.S3Objects)
{
kv.Add(new KeyVersion{ Key = o.Key });
}
if (objects.S3Objects.Count == 0) break;
client.DeleteObjectsAsync(new DeleteObjectsRequest() { BucketName = bucketName, Objects = kv }).Wait();
if (objects.IsTruncated)
{
objects = client.ListObjectsAsync(new ListObjectsRequest() { BucketName = bucketName, Marker = objects.NextMarker }).Result;
}
else
{
break;
}
}
}