Amazon Simple Storage Service
개발자 안내서 (API 버전 2006-03-01)

AWS SDK for Java를 사용한 여러 객체 삭제

AWS SDK for Java는 여러 객체를 삭제하기 위한 AmazonS3Client.deleteObjects() 메서드를 제공합니다. 삭제하려는 각 객체에 대해 키 이름을 지정합니다. 버전 관리를 사용하는 버킷의 경우, 다음 옵션을 사용할 수 있습니다.

  • 객체의 키 이름만 지정합니다. 그러면 Amazon S3가 해당 객체에 삭제 마커를 추가합니다.

  • 삭제할 객체의 키 이름과 버전 ID를 모두 지정합니다. 그러면 Amazon S3가 지정된 객체 버전을 삭제합니다.

다음 코드 예제는 Multi-Object Delete API를 사용하여 버전 관리 미사용 버킷에서 객체를 삭제합니다. 이 예제에서는 샘플 객체를 버킷에 업로드한 다음 AmazonS3Client.deleteObjects() 메서드를 사용하여 단일 요청으로 객체를 삭제합니다. 이 예제에서는 객체에 버전 ID가 없으므로 DeleteObjectsRequest에서 객체 키 이름만 지정합니다.

객체 삭제에 대한 자세한 내용은 객체 삭제 단원을 참조하십시오. 실제 예제를 작성 및 테스트하는 방법에 대한 자세한 내용은 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(); } } }

다음 코드 예제는 Multi-Object Delete API를 사용하여 버전 관리를 사용하는 버킷에서 객체를 삭제합니다. 이 예제에서는 작업을 수행합니다.

  1. 샘플 객체를 만든 후 삭제할 각 객체의 키 이름과 버전 ID를 지정하여 삭제합니다. 이 작업에서는 지정된 객체 버전만 삭제합니다.

  2. 샘플 객체를 만든 후 키 이름만 지정하여 삭제합니다. 이 예제에서는 버전 ID를 지정하지 않으므로 이 작업은 특정 객체 버전을 삭제하지 않고 각 객체에 삭제 마커를 추가합니다. 삭제 마커가 추가된 객체는 AWS Management 콘솔에 표시되지 않습니다.

  3. 삭제 마커의 객체 키와 버전 ID를 지정하여 삭제 마커를 제거합니다. 이 작업에서는 삭제 마커를 삭제합니다. 그러면 해당 객체가 AWS Management 콘솔에 다시 표시됩니다.

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"); } }