Set the website configuration for an Amazon S3 bucket using an Amazon SDK - Amazon Simple Storage Service
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

Set the website configuration for an Amazon S3 bucket using an Amazon SDK

The following code examples show how to set the website configuration for an S3 bucket.

C++
SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

bool AwsDoc::S3::PutWebsiteConfig(const Aws::String &bucketName, const Aws::String &indexPage, const Aws::String &errorPage, const Aws::Client::ClientConfiguration &clientConfig) { Aws::S3::S3Client client(clientConfig); Aws::S3::Model::IndexDocument indexDocument; indexDocument.SetSuffix(indexPage); Aws::S3::Model::ErrorDocument errorDocument; errorDocument.SetKey(errorPage); Aws::S3::Model::WebsiteConfiguration websiteConfiguration; websiteConfiguration.SetIndexDocument(indexDocument); websiteConfiguration.SetErrorDocument(errorDocument); Aws::S3::Model::PutBucketWebsiteRequest request; request.SetBucket(bucketName); request.SetWebsiteConfiguration(websiteConfiguration); Aws::S3::Model::PutBucketWebsiteOutcome outcome = client.PutBucketWebsite(request); if (!outcome.IsSuccess()) { std::cerr << "Error: PutBucketWebsite: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Success: Set website configuration for bucket '" << bucketName << "'." << std::endl; } return outcome.IsSuccess(); }
Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

public static void setWebsiteConfig( S3Client s3, String bucketName, String indexDoc) { try { WebsiteConfiguration websiteConfig = WebsiteConfiguration.builder() .indexDocument(IndexDocument.builder().suffix(indexDoc).build()) .build(); PutBucketWebsiteRequest pubWebsiteReq = PutBucketWebsiteRequest.builder() .bucket(bucketName) .websiteConfiguration(websiteConfig) .build(); s3.putBucketWebsite(pubWebsiteReq); System.out.println("The call was successful"); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • For API details, see PutBucketWebsite in Amazon SDK for Java 2.x API Reference.

JavaScript
SDK for JavaScript V3
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Create the client.

// Create service client module using ES6 syntax. import { S3Client } from "@aws-sdk/client-s3"; // Set the AWS Region. const REGION = "us-east-1"; // Create an Amazon S3 service client object. const s3Client = new S3Client({ region: REGION }); export { s3Client };

Set the website configuration.

// Import required AWS SDK clients and commands for Node.js. import { PutBucketWebsiteCommand } from "@aws-sdk/client-s3"; import { s3Client } from "./libs/s3Client.js"; // Helper function that creates an Amazon S3 service client module. // Create the parameters for the bucket export const bucketParams = { Bucket: "BUCKET_NAME" }; export const staticHostParams = { Bucket: bucketParams, WebsiteConfiguration: { ErrorDocument: { Key: "", }, IndexDocument: { Suffix: "", }, }, }; export const run = async () => { // Insert specified bucket name and index and error documents into parameters JSON // from command line arguments staticHostParams.Bucket = bucketParams; staticHostParams.WebsiteConfiguration.IndexDocument.Suffix = "INDEX_PAGE"; // The index document inserted into parameters JSON. staticHostParams.WebsiteConfiguration.ErrorDocument.Key = "ERROR_PAGE"; // The error document inserted into parameters JSON. // Set the new website configuration on the selected bucket. try { const data = await s3Client.send(new PutBucketWebsiteCommand(staticHostParams)); console.log("Success", data); return data; } catch (err) { console.log("Error", err); } }; run();
Ruby
SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

require "aws-sdk-s3" # Wraps Amazon S3 bucket website actions. class BucketWebsiteWrapper attr_reader :bucket_website # @param bucket_website [Aws::S3::BucketWebsite] A bucket website object configured with an existing bucket. def initialize(bucket_website) @bucket_website = bucket_website end # Sets a bucket as a static website. # # @param index_document [String] The name of the index document for the website. # @param error_document [String] The name of the error document to show for 4XX errors. # @return [Boolean] True when the bucket is configured as a website; otherwise, false. def set_website(index_document, error_document) @bucket_website.put( website_configuration: { index_document: { suffix: index_document }, error_document: { key: error_document } } ) true rescue Aws::Errors::ServiceError => e puts "Couldn't configure #{@bucket_website.bucket.name} as a website. Here's why: #{e.message}" false end end def run_demo bucket_name = "doc-example-bucket" index_document = "index.html" error_document = "404.html" wrapper = BucketWebsiteWrapper.new(Aws::S3::BucketWebsite.new(bucket_name)) return unless wrapper.set_website(index_document, error_document) puts "Successfully configured bucket #{bucket_name} as a static website." end run_demo if $PROGRAM_NAME == __FILE__

For a complete list of Amazon SDK developer guides and code examples, see Using this service with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.