Я могу удалить непустую корзину, используя aws-java-sdk 1.0
, но не с помощью aws-java-sdk 2.0. Вот код:
Не работает код с aws-java-sdk 2.0 для удаления непустого сегмента s3:
{
boolean isTruncated = true;
while(isTruncated) {
ListObjectsV2Iterable objectListing = s3Client.listObjectsV2Paginator(ListObjectsV2Request.builder()
.bucket(bucketName)
.build());
for (ListObjectsV2Response response : objectListing) {
/*
* There could be many pages in object list response is just one page
* */
for (S3Object s3Object : response.contents()) {
s3Client.deleteObject(DeleteObjectRequest.builder()
.bucket(bucketName)
.key(s3Object.key())
.build());
}
isTruncated = response.isTruncated();
}
}
// Delete all object versions (required for versioned buckets).
ListObjectVersionsIterable versionList = s3Client.listObjectVersionsPaginator(ListObjectVersionsRequest.builder()
.bucket(bucketName)
.build());
isTruncated = true;
while (isTruncated) {
for (ListObjectVersionsResponse response : versionList) {
for (ObjectVersion version : response.versions()) {
s3Client.deleteObject(DeleteObjectRequest.builder()
.bucket(bucketName)
.key(version.key())
.versionId(version.versionId())
.build());
}
isTruncated = response.isTruncated();
}
}
s3Client.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build());
}
Приведенный выше код не может удалить интервал, выдает исключение:
software.amazon.awssdk.services.s3.model.S3Exception: корзина, которую вы пытались удалить, не пуста. Вы должны удалить все версии в корзине. (Сервис: S3, Код состояния: 409, Идентификатор запроса: F5D76141223DC5EA)
Рабочий код с aws-java-sdk 1.0 для удаления непустого сегмента:
ObjectListing objectListing = s3Client.listObjects(bucketName);
while (true) {
Iterator<S3ObjectSummary> objIter = objectListing.getObjectSummaries().iterator();
while (objIter.hasNext()) {
s3Client.deleteObject(bucketName, objIter.next().getKey());
}
// If the bucket contains many objects, the listObjects() call
// might not return all of the objects in the first listing. Check to
// see whether the listing was truncated. If so, retrieve the next page of objects
// and delete them.
if (objectListing.isTruncated()) {
objectListing = s3Client.listNextBatchOfObjects(objectListing);
} else {
break;
}
}
// Delete all object versions (required for versioned buckets).
VersionListing versionList = s3Client.listVersions(new ListVersionsRequest().withBucketName(bucketName));
while (true) {
Iterator<S3VersionSummary> versionIter = versionList.getVersionSummaries().iterator();
while (versionIter.hasNext()) {
S3VersionSummary vs = versionIter.next();
s3Client.deleteVersion(bucketName, vs.getKey(), vs.getVersionId());
}
if (versionList.isTruncated()) {
versionList = s3Client.listNextBatchOfVersions(versionList);
} else {
break;
}
}
// After all objects and object versions are deleted, delete the bucket.
s3Client.deleteBucket(bucketName);