AWS Code Sample

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above. demonstrates how to set the website configuration for an S3 bucket.

/* Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package aws.example.s3; import com.amazonaws.AmazonServiceException; import com.amazonaws.regions.Regions; import; import; import; /** * Set the website configuration for an S3 bucket. * * This code expects that you have AWS credentials set up per: * */ public class SetWebsiteConfiguration { public static void setWebsiteConfig( String bucket_name, String index_doc, String error_doc) { BucketWebsiteConfiguration website_config = null; if (index_doc == null) { website_config = new BucketWebsiteConfiguration(); } else if (error_doc == null) { website_config = new BucketWebsiteConfiguration(index_doc); } else { website_config = new BucketWebsiteConfiguration(index_doc, error_doc); } final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build(); try { s3.setBucketWebsiteConfiguration(bucket_name, website_config); } catch (AmazonServiceException e) { System.out.format( "Failed to set website configuration for bucket '%s'!\n", bucket_name); System.err.println(e.getErrorMessage()); System.exit(1); } } public static void main(String[] args) { final String USAGE = "\n" + "SetWebsiteConfiguration - set the website configuration for an S3 bucket\n\n" + "Usage: SetWebsiteConfiguration <bucket> [indexdoc] [errordoc]\n\n" + "Where:\n" + " bucket - the bucket to set the website configuration on\n" + " indexdoc - (optional) the index document, ex. 'index.html'\n" + " If not specified, 'index.html' will be set.\n" + " errordoc - (optional) the error document, ex. 'notfound.html'\n" + " If not specified, no error doc will be set.\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1); } final String bucket_name = args[0]; final String index_doc = (args.length > 1) ? args[1] : "index.html"; final String error_doc = (args.length > 2) ? args[2] : null; setWebsiteConfig(bucket_name, index_doc, error_doc); } }

Sample Details

Service: s3

Author: soo-aws

Type: full-example

On this page: