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

使用AWS SDK for Java删除对象

您可以从存储桶删除对象。如果已对存储桶启用版本控制,您将获得以下选项:

  • 通过指定版本 ID 来删除特定对象版本。

  • 删除对象而不指定版本 ID,在这种情况下,S3 将向对象添加一个删除标记。

有关版本控制的更多信息,请参阅对象版本控制

例 示例 1:删除对象 (不受版本控制的存储桶)

下面的 示例将从存储桶删除对象。该示例假定存储桶不受版本控制且对象没有任何版本 ID。在删除请求中,您仅指定对象键而不是版本 ID。有关创建和测试有效示例的说明,请参阅测试 Amazon S3 Java 代码示例

import java.io.IOException; 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.DeleteObjectRequest; public class DeleteObjectNonVersionedBucket { public static void main(String[] args) throws IOException { String clientRegion = "*** Client region ***"; String bucketName = "*** Bucket name ***"; String keyName = "*** Key name ****"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); s3Client.deleteObject(new DeleteObjectRequest(bucketName, keyName)); } 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(); } } }

例 示例 2:删除对象 (受版本控制的存储桶)

以下示例将删除受版本控制的存储桶中的对象。该示例通过指定对象键名称和版本 ID 来删除特定对象版本。本示例执行以下操作:

  1. 向存储桶添加示例对象。Amazon S3 将返回新添加对象的版本 ID。该示例将在删除请求中使用此版本 ID。

  2. 通过指定对象键名称和版本 ID 来删除对象版本。如果对象没有其他版本,则 Amazon S3 将完全删除对象。否则,Amazon S3 仅删除指定版本。

    注意

    可以通过发送 ListVersions 请求来获取对象的版本 ID。

import java.io.IOException; 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.DeleteVersionRequest; import com.amazonaws.services.s3.model.PutObjectResult; public class DeleteObjectVersionEnabledBucket { public static void main(String[] args) throws IOException { String clientRegion = "*** Client region ***"; String bucketName = "*** Bucket name ***"; String keyName = "*** Key name ****"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); // Check to ensure that the bucket is versioning-enabled. String bucketVersionStatus = s3Client.getBucketVersioningConfiguration(bucketName).getStatus(); if(!bucketVersionStatus.equals(BucketVersioningConfiguration.ENABLED)) { System.out.printf("Bucket %s is not versioning-enabled.", bucketName); } else { // Add an object. PutObjectResult putResult = s3Client.putObject(bucketName, keyName, "Sample content for deletion example."); System.out.printf("Object %s added to bucket %s\n", keyName, bucketName); // Delete the version of the object that we just created. System.out.println("Deleting versioned object " + keyName); s3Client.deleteVersion(new DeleteVersionRequest(bucketName, keyName, putResult.getVersionId())); System.out.printf("Object %s, version %s deleted\n", keyName, putResult.getVersionId()); } } 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(); } } }