Menu
AWS SDK for C++
Developer Guide

Configuring an Amazon S3 Bucket as a Website

You can configure an Amazon S3 bucket to behave as a website. To do this, you need to set its website configuration.

Note

These code snippets assume that you understand the material in Getting Started Using the AWS SDK for C++ and have configured default AWS credentials using the information in Providing AWS Credentials.

Set a Bucket's Website Configuration

To set an Amazon S3 bucket's website configuration, call the S3Client's PutBucketWebsite function with a a PutBucketWebsiteRequest object containing the bucket name and its website configuration, provided in a WebsiteConfiguration object.

Setting an index document is required; all other parameters are optional.

Includes

Copy
#include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/IndexDocument.h> #include <aws/s3/model/ErrorDocument.h> #include <aws/s3/model/WebsiteConfiguration.h> #include <aws/s3/model/PutBucketWebsiteRequest.h>

Code

Copy
Aws::Client::ClientConfiguration config; config.region = user_region; Aws::S3::S3Client s3_client(config); Aws::S3::Model::IndexDocument index_doc; index_doc.SetSuffix(index_suffix); Aws::S3::Model::ErrorDocument error_doc; error_doc.SetKey(error_key); Aws::S3::Model::WebsiteConfiguration website_config; website_config.SetIndexDocument(index_doc); website_config.SetErrorDocument(error_doc); Aws::S3::Model::PutBucketWebsiteRequest request; request.SetBucket(bucket_name); request.SetWebsiteConfiguration(website_config); auto outcome = s3_client.PutBucketWebsite(request); if (outcome.IsSuccess()) { std::cout << "Done!" << std::endl; } else { std::cout << "PutBucketWebsite error: " << outcome.GetError().GetExceptionName() << std::endl << outcome.GetError().GetMessage() << std::endl; }

Note

Setting a website configuration does not modify the access permissions for your bucket. To make your files visible on the web, you will also need to set a bucket policy that allows public read access to the files in the bucket. For more information, see Managing Access to Amazon S3 Buckets Using Bucket Policies.

See the complete example.

Get a Bucket's Website Configuration

To get an Amazon S3 bucket's website configuration, call the S3Client's GetBucketWebsite function with a GetBucketWebsiteRequest containing the name of the bucket to retrieve the configuration for.

The configuration will be returned as a GetBucketWebsiteResult object within the outcome object. If there is no website configuration for the bucket, then null will be returned.

Includes

Copy
#include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/GetBucketWebsiteRequest.h>

Code

Copy
Aws::Client::ClientConfiguration config; config.region = user_region; Aws::S3::S3Client s3_client(config); Aws::S3::Model::GetBucketWebsiteRequest request; request.SetBucket(bucket_name); auto outcome = s3_client.GetBucketWebsite(request); if (outcome.IsSuccess()) { std::cout << " Index page: " << outcome.GetResult().GetIndexDocument().GetSuffix() << std::endl << " Error page: " << outcome.GetResult().GetErrorDocument().GetKey() << std::endl; } else { std::cout << "GetBucketWebsite error: " << outcome.GetError().GetExceptionName() << " - " << outcome.GetError().GetMessage() << std::endl; }

See the complete example.

Delete a Bucket's Website Configuration

To delete an Amazon S3 bucket's website configuration, call the S3Client's DeleteBucketWebsite function with a DeleteBucketWebsiteRequest: containing the name of the bucket to delete the configuration from.

Includes

Copy
#include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/DeleteBucketWebsiteRequest.h>

Code

Copy
Aws::Client::ClientConfiguration config; config.region = user_region; Aws::S3::S3Client s3_client(config); Aws::S3::Model::DeleteBucketWebsiteRequest request; request.SetBucket(bucket_name); auto outcome = s3_client.DeleteBucketWebsite(request); if (outcome.IsSuccess()) { std::cout << "Done!" << std::endl; } else { std::cout << "DeleteBucketWebsite error: " << outcome.GetError().GetExceptionName() << std::endl << outcome.GetError().GetMessage() << std::endl; }

See the complete example.

More Information