Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awsdocs
GitHub Repository: awsdocs/amazon-s3-developer-guide
Path: blob/master/code_examples/java_examples/S3Examples/DeleteObjectNonVersionedBucket.java
4084 views
1
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)
3
4
import java.io.IOException;
5
6
import com.amazonaws.AmazonServiceException;
7
import com.amazonaws.SdkClientException;
8
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
9
import com.amazonaws.services.s3.AmazonS3;
10
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
11
import com.amazonaws.services.s3.model.DeleteObjectRequest;
12
13
public class DeleteObjectNonVersionedBucket {
14
15
public static void main(String[] args) throws IOException {
16
String clientRegion = "*** Client region ***";
17
String bucketName = "*** Bucket name ***";
18
String keyName = "*** Key name ****";
19
20
try {
21
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
22
.withCredentials(new ProfileCredentialsProvider())
23
.withRegion(clientRegion)
24
.build();
25
26
s3Client.deleteObject(new DeleteObjectRequest(bucketName, keyName));
27
}
28
catch(AmazonServiceException e) {
29
// The call was transmitted successfully, but Amazon S3 couldn't process
30
// it, so it returned an error response.
31
e.printStackTrace();
32
}
33
catch(SdkClientException e) {
34
// Amazon S3 couldn't be contacted for a response, or the client
35
// couldn't parse the response from Amazon S3.
36
e.printStackTrace();
37
}
38
}
39
}
40