Path: blob/master/code_examples/java_examples/S3Examples/WebsiteConfiguration.java
4084 views
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)23import java.io.IOException;45import com.amazonaws.AmazonServiceException;6import com.amazonaws.SdkClientException;7import com.amazonaws.auth.profile.ProfileCredentialsProvider;8import com.amazonaws.services.s3.AmazonS3;9import com.amazonaws.services.s3.AmazonS3ClientBuilder;10import com.amazonaws.services.s3.model.BucketWebsiteConfiguration;1112public class WebsiteConfiguration {1314public static void main(String[] args) throws IOException {15String clientRegion = "*** Client region ***";16String bucketName = "*** Bucket name ***";17String indexDocName = "*** Index document name ***";18String errorDocName = "*** Error document name ***";1920try {21AmazonS3 s3Client = AmazonS3ClientBuilder.standard()22.withRegion(clientRegion)23.withCredentials(new ProfileCredentialsProvider())24.build();2526// Print the existing website configuration, if it exists.27printWebsiteConfig(s3Client, bucketName);2829// Set the new website configuration.30s3Client.setBucketWebsiteConfiguration(bucketName, new BucketWebsiteConfiguration(indexDocName, errorDocName));3132// Verify that the configuration was set properly by printing it.33printWebsiteConfig(s3Client, bucketName);3435// Delete the website configuration.36s3Client.deleteBucketWebsiteConfiguration(bucketName);3738// Verify that the website configuration was deleted by printing it.39printWebsiteConfig(s3Client, bucketName);40}41catch(AmazonServiceException e) {42// The call was transmitted successfully, but Amazon S3 couldn't process43// it, so it returned an error response.44e.printStackTrace();45}46catch(SdkClientException e) {47// Amazon S3 couldn't be contacted for a response, or the client48// couldn't parse the response from Amazon S3.49e.printStackTrace();50}51}5253private static void printWebsiteConfig(AmazonS3 s3Client, String bucketName) {54System.out.println("Website configuration: ");55BucketWebsiteConfiguration bucketWebsiteConfig = s3Client.getBucketWebsiteConfiguration(bucketName);56if (bucketWebsiteConfig == null) {57System.out.println("No website config.");58} else {59System.out.println("Index doc: " + bucketWebsiteConfig.getIndexDocumentSuffix());60System.out.println("Error doc: " + bucketWebsiteConfig.getErrorDocument());61}62}63}6465