Amazon Simple Storage Service
开发人员指南 (API 版本 2006-03-01)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

使用AWS SDK for Java删除多个对象

AWS SDK for Java提供了用于删除多个对象的 AmazonS3Client.deleteObjects() 方法。对于要删除的每个对象,请指定键名称。如果存储桶受版本控制,您可以选择以下选项:

  • 仅指定对象的键名称。Amazon S3 将向对象添加一个删除标记。

  • 指定要删除的对象的键名称和版本 ID。Amazon S3 将删除指定版本的对象。

下面的示例使用多对象删除 API 从不受版本控制的存储桶中删除对象。该示例将示例对象上传到存储桶,然后使用 AmazonS3Client.deleteObjects() 方法删除单个请求中的对象。在 DeleteObjectsRequest 中,该示例仅指定对象键名称,因为对象没有版本 ID。

有关删除对象的更多信息,请参阅删除对象。有关创建和测试有效示例的说明,请参阅测试 Amazon S3 Java 代码示例

import java.io.IOException; import java.util.ArrayList; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.DeleteObjectsRequest; import com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion; import com.amazonaws.services.s3.model.DeleteObjectsResult; public class DeleteMultipleObjectsNonVersionedBucket { public static void main(String[] args) throws IOException { String clientRegion = "*** Client region ***"; String bucketName = "*** Bucket name ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); // Upload three sample objects. ArrayList<KeyVersion> keys = new ArrayList<KeyVersion>(); for (int i = 0; i < 3; i++) { String keyName = "delete object example " + i; s3Client.putObject(bucketName, keyName, "Object number " + i + " to be deleted."); keys.add(new KeyVersion(keyName)); } System.out.println(keys.size() + " objects successfully created."); // Delete the sample objects. DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName) .withKeys(keys) .withQuiet(false); // Verify that the objects were deleted successfully. DeleteObjectsResult delObjRes = s3Client.deleteObjects(multiObjectDeleteRequest); int successfulDeletes = delObjRes.getDeletedObjects().size(); System.out.println(successfulDeletes + " objects successfully deleted."); } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }

下面的示例使用多对象删除 API 从受版本控制的存储桶中删除对象。它将执行以下操作:

  1. 创建示例对象然后删除它们,并为要删除的每个对象指定键名称和版本 ID。本操作仅删除指定对象版本。

  2. 通过仅指定键名称来创建示例对象然后删除它们。由于该示例不指定版本 ID,本操作将向每个对象添加一个删除标记而不删除任何特定对象版本。在添加删除标记后,这些对象不会显示在 AWS 管理控制台中。

  3. 通过指定删除标记的对象键和版本 ID 来移除删除标记。本操作将移除删除标记,从而使对象再次显示在 AWS 管理控制台中。

import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.BucketVersioningConfiguration; import com.amazonaws.services.s3.model.DeleteObjectsRequest; import com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion; import com.amazonaws.services.s3.model.DeleteObjectsResult; import com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject; import com.amazonaws.services.s3.model.PutObjectResult; public class DeleteMultipleObjectsVersionEnabledBucket { private static AmazonS3 S3_CLIENT; private static String VERSIONED_BUCKET_NAME; public static void main(String[] args) throws IOException { String clientRegion = "*** Client region ***"; VERSIONED_BUCKET_NAME = "*** Bucket name ***"; try { S3_CLIENT = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); // Check to make sure that the bucket is versioning-enabled. String bucketVersionStatus = S3_CLIENT.getBucketVersioningConfiguration(VERSIONED_BUCKET_NAME).getStatus(); if(!bucketVersionStatus.equals(BucketVersioningConfiguration.ENABLED)) { System.out.printf("Bucket %s is not versioning-enabled.", VERSIONED_BUCKET_NAME); } else { // Upload and delete sample objects, using specific object versions. uploadAndDeleteObjectsWithVersions(); // Upload and delete sample objects without specifying version IDs. // Amazon S3 creates a delete marker for each object rather than deleting // specific versions. DeleteObjectsResult unversionedDeleteResult = uploadAndDeleteObjectsWithoutVersions(); // Remove the delete markers placed on objects in the non-versioned create/delete method. multiObjectVersionedDeleteRemoveDeleteMarkers(unversionedDeleteResult); } } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } private static void uploadAndDeleteObjectsWithVersions() { System.out.println("Uploading and deleting objects with versions specified."); // Upload three sample objects. ArrayList<KeyVersion> keys = new ArrayList<KeyVersion>(); for (int i = 0; i < 3; i++) { String keyName = "delete object without version ID example " + i; PutObjectResult putResult = S3_CLIENT.putObject(VERSIONED_BUCKET_NAME, keyName, "Object number " + i + " to be deleted."); // Gather the new object keys with version IDs. keys.add(new KeyVersion(keyName, putResult.getVersionId())); } // Delete the specified versions of the sample objects. DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME) .withKeys(keys) .withQuiet(false); // Verify that the object versions were successfully deleted. DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(multiObjectDeleteRequest); int successfulDeletes = delObjRes.getDeletedObjects().size(); System.out.println(successfulDeletes + " objects successfully deleted"); } private static DeleteObjectsResult uploadAndDeleteObjectsWithoutVersions() { System.out.println("Uploading and deleting objects with no versions specified."); // Upload three sample objects. ArrayList<KeyVersion> keys = new ArrayList<KeyVersion>(); for (int i = 0; i < 3; i++) { String keyName = "delete object with version ID example " + i; S3_CLIENT.putObject(VERSIONED_BUCKET_NAME, keyName, "Object number " + i + " to be deleted."); // Gather the new object keys without version IDs. keys.add(new KeyVersion(keyName)); } // Delete the sample objects without specifying versions. DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME).withKeys(keys) .withQuiet(false); // Verify that delete markers were successfully added to the objects. DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(multiObjectDeleteRequest); int successfulDeletes = delObjRes.getDeletedObjects().size(); System.out.println(successfulDeletes + " objects successfully marked for deletion without versions."); return delObjRes; } private static void multiObjectVersionedDeleteRemoveDeleteMarkers(DeleteObjectsResult response) { List<KeyVersion> keyList = new ArrayList<KeyVersion>(); for (DeletedObject deletedObject : response.getDeletedObjects()) { // Note that the specified version ID is the version ID for the delete marker. keyList.add(new KeyVersion(deletedObject.getKey(), deletedObject.getDeleteMarkerVersionId())); } // Create a request to delete the delete markers. DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME).withKeys(keyList); // Delete the delete markers, leaving the objects intact in the bucket. DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(deleteRequest); int successfulDeletes = delObjRes.getDeletedObjects().size(); System.out.println(successfulDeletes + " delete markers successfully deleted"); } }