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/WebsiteConfiguration.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.BucketWebsiteConfiguration;
12
13
public class WebsiteConfiguration {
14
15
public static void main(String[] args) throws IOException {
16
String clientRegion = "*** Client region ***";
17
String bucketName = "*** Bucket name ***";
18
String indexDocName = "*** Index document name ***";
19
String errorDocName = "*** Error document name ***";
20
21
try {
22
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
23
.withRegion(clientRegion)
24
.withCredentials(new ProfileCredentialsProvider())
25
.build();
26
27
// Print the existing website configuration, if it exists.
28
printWebsiteConfig(s3Client, bucketName);
29
30
// Set the new website configuration.
31
s3Client.setBucketWebsiteConfiguration(bucketName, new BucketWebsiteConfiguration(indexDocName, errorDocName));
32
33
// Verify that the configuration was set properly by printing it.
34
printWebsiteConfig(s3Client, bucketName);
35
36
// Delete the website configuration.
37
s3Client.deleteBucketWebsiteConfiguration(bucketName);
38
39
// Verify that the website configuration was deleted by printing it.
40
printWebsiteConfig(s3Client, bucketName);
41
}
42
catch(AmazonServiceException e) {
43
// The call was transmitted successfully, but Amazon S3 couldn't process
44
// it, so it returned an error response.
45
e.printStackTrace();
46
}
47
catch(SdkClientException e) {
48
// Amazon S3 couldn't be contacted for a response, or the client
49
// couldn't parse the response from Amazon S3.
50
e.printStackTrace();
51
}
52
}
53
54
private static void printWebsiteConfig(AmazonS3 s3Client, String bucketName) {
55
System.out.println("Website configuration: ");
56
BucketWebsiteConfiguration bucketWebsiteConfig = s3Client.getBucketWebsiteConfiguration(bucketName);
57
if (bucketWebsiteConfig == null) {
58
System.out.println("No website config.");
59
} else {
60
System.out.println("Index doc: " + bucketWebsiteConfig.getIndexDocumentSuffix());
61
System.out.println("Error doc: " + bucketWebsiteConfig.getErrorDocument());
62
}
63
}
64
}
65