AWS SDK for Ruby
Developer Guide

Using a Amazon S3 Bucket to Host a Website

This example demonstrates how to use the AWS SDK for Ruby to:

  1. Create an Amazon S3 bucket.

  2. Get the bucket website's configuration.

  3. Add objects to the bucket.

  4. Set the bucket website's configuration.

  5. Access the bucket website's documents.

  6. Delete the bucket website.

  7. Delete the bucket.

For information about bucket website hosting, see Configure a Bucket for Website Hosting in the Amazon S3 Developer Guide.

For the complete code for this example, see Complete Example.

Prerequisites

To set up and run this example, you must first:

  1. Install the AWS SDK for Ruby. For more information, see Installing the AWS SDK for Ruby.

  2. Set the AWS access credentials that the AWS SDK for Ruby will use to verify your access to AWS services and resources. For more information, see Configuring the AWS SDK for Ruby.

Be sure the AWS credentials map to an AWS Identity and Access Management (IAM) entity with access to the AWS actions and resources described in this example.

This example assumes you have set the credentials in the AWS credentials profile file or in the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables on your local system.

Configure the SDK

For this example, add a require statement so that you can use the classes and methods provided by the AWS SDK for Ruby for Amazon S3. Then create an Aws::S3::Client object in the AWS Region where you want to create the bucket and the specified AWS profile. This code creates the Aws::S3::Client object in the us-east-2 region.

An additional variable is also declared for the bucket used in this example. To help ensure the bucket name is unique across all AWS accounts, an additional require statement is added, and the SecureRandom module's uuid method is called to generate a unique identifier. This identifier is inserted into the name of the bucket to be created later in this example.

require 'aws-sdk-s3' # v2: require 'aws-sdk' # Using Random UUIDs to Avoid Collisions when Testing require 'securerandom' bucket = "example-test-bucket-#{SecureRandom.uuid}" # Setup s3 = Aws::S3::Client.new(region: "us-west-2")

Create a Bucket

Call the create_bucket method, supplying the name of the bucket to create.

s3.create_bucket(bucket: bucket)

Get a Bucket Website's Configuration

Call the get_bucket_website method, supplying the name of the bucket. By default, a bucket is not configured as a website. To confirm this behavior, call the get_bucket_website method. This returns an error, because there is no website configuration for the bucket.

begin s3.get_bucket_website(bucket: bucket) rescue Aws::S3::Errors::NoSuchWebsiteConfiguration puts "No bucket website configuration present." end

Add an Object to a Bucket

Call the put_object method, supplying the name of the bucket and object, the object's contents, and the object's access permissions set. This example adds two webpages to the bucket.

s3.put_object( bucket: bucket, key: "index.html", body: "Hello, Amazon S3!", acl: "public-read" ) s3.put_object( bucket: bucket, key: "error.html", body: "Page not found!", acl: "public-read" )

Set a Bucket Website's Configuration

Call the put_bucket_website method, supplying the name of the bucket and the website configuration. For the website configuration, use an Aws::S3::Types::WebsiteConfiguration hash, supplying the website's index and error webpages.

s3.put_bucket_website( bucket: bucket, website_configuration: { index_document: { suffix: "index.html" }, error_document: { key: "error.html" } } )

Access a Bucket Website's Documents

Call the Ruby Net::HTTP.get method, supplying the address to the document in the bucket website.

index_path = "http://#{bucket}.s3-website-us-west-2.amazonaws.com/" error_path = "http://#{bucket}.s3-website-us-west-2.amazonaws.com/nonexistant.html" puts "Index Page Contents:\n#{Net::HTTP.get(URI(index_path))}\n\n" puts "Error Page Contents:\n#{Net::HTTP.get(URI(error_path))}\n\n"

Delete a Bucket Website

Call the delete_bucket_website method, supplying the name of the bucket.

s3.delete_bucket_website(bucket: bucket)

Delete a Bucket

Call the bucket method of an Aws::S3::Resource object, supplying the name of the bucket. This returns an Aws::S3::Bucket object. Then call the Aws::S3::Bucket object's delete method.

b = Aws::S3::Resource.new(region: "us-west-2").bucket(bucket) b.delete! # Recursively deletes objects as well.

Complete Example

Here is the complete code for this example.

require 'aws-sdk-s3' # v2: require 'aws-sdk' # Using Random UUIDs to Avoid Collisions when Testing require 'securerandom' bucket = "example-test-bucket-#{SecureRandom.uuid}" # Setup s3 = Aws::S3::Client.new(region: "us-west-2") s3.create_bucket(bucket: bucket) # When Bucket Has No Website Configuration begin s3.get_bucket_website(bucket: bucket) rescue Aws::S3::Errors::NoSuchWebsiteConfiguration puts "No bucket website configuration present." end # Adding Simple Pages & Website Configuration s3.put_object( bucket: bucket, key: "index.html", body: "Hello, Amazon S3!", acl: "public-read" ) s3.put_object( bucket: bucket, key: "error.html", body: "Page not found!", acl: "public-read" ) s3.put_bucket_website( bucket: bucket, website_configuration: { index_document: { suffix: "index.html" }, error_document: { key: "error.html" } } ) # Accessing as a Website index_path = "http://#{bucket}.s3-website-us-west-2.amazonaws.com/" error_path = "http://#{bucket}.s3-website-us-west-2.amazonaws.com/nonexistant.html" puts "Index Page Contents:\n#{Net::HTTP.get(URI(index_path))}\n\n" puts "Error Page Contents:\n#{Net::HTTP.get(URI(error_path))}\n\n" # Removing Website Configuration s3.delete_bucket_website(bucket: bucket) # Cleanup b = Aws::S3::Resource.new(region: "us-west-2").bucket(bucket) b.delete! # Recursively deletes objects as well.