删除对象
您可以使用 deleteObject 接口删除单个对象,以下代码展示如何删除一个对象:
String bucketName = "<your-bucket-name>";
String objectKey = "<your-object-key>";
try {
s3.deleteObject(bucketName, objectKey);
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
}
您可以使用 deleteObjects 接口删除多个对象,提供两种返回模式:详细(verbose)模式和简单(quiet)模式,详细模式包括成功与失败的结果,简单模式只返回失败的结果,默认为详细模式。以下代码展示如何删除多个对象:
String bucketName = "<your-bucket-name>";
String objectKey1 = "<your-object-key1>";
String objectKey2 = "<your-object-key2>";
DeleteObjectsRequest deleteObjectRequest = new DeleteObjectsRequest(bucketName);
List<KeyVersion> list = new ArrayList<KeyVersion>();
//在开启多版本功能后,可以在KeyVersion中携带verion id,删除指定版本的对象
KeyVersion key1 = new KeyVersion(objectKey1);
KeyVersion key2 = new KeyVersion(objectKey2);
list.add(key1);
list.add(key2);
deleteObjectRequest.setKeys(list);
//true是简单模式,false是详细模式
deleteObjectRequest.setQuiet(true);
DeleteObjectsResult result = s3.deleteObjects(deleteObjectRequest);