Menu
Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)

Managing Websites with the AWS SDK for PHP

This topic guides you through using classes from the AWS SDK for PHP to configure and manage an Amazon S3 bucket for website hosting. For more information about the Amazon S3 website feature, see Hosting a Static Website on Amazon S3.

Note

This topic assumes that you are already following the instructions for Using the AWS SDK for PHP and Running PHP Examples and have the AWS SDK for PHP properly installed.

The following tasks guide you through using the PHP SDK classes to configure and manage an Amazon S3 bucket for website hosting.

Configuring a Bucket for Website Hosting

1

Create an instance of an Amazon S3 client by using the Aws\S3\S3Client class factory() method.

2

To configure a bucket as a website, execute the Aws\S3\S3Client::putBucketWebsite() method. You need to provide the bucket name and the website configuration information, including the index document and the error document names. If you don't provide these document names, this method adds the index.html and error.html default names to the website configuration. You must verify that these documents are present in the bucket.

3

To retrieve existing bucket website configuration, execute the Aws\S3\S3Client::getBucketWebsite() method.

4

To delete website configuration from a bucket, execute the Aws\S3\S3Client::deleteBucketWebsite() method, passing the bucket name as a parameter. If you remove the website configuration, the bucket is no longer accessible from the website endpoints.


The following PHP code sample demonstrates the preceding tasks.

use Aws\S3\S3Client;

$bucket = '*** Your Bucket Name ***';
				
// 1. Instantiate the client.
$s3 = S3Client::factory();

// 2. Add website configuration.
$result = $s3->putBucketWebsite(array(
    'Bucket'        => $bucket,    
    'IndexDocument' => array('Suffix' => 'index.html'),
    'ErrorDocument' => array('Key' => 'error.html'),
));

// 3. Retrieve website configuration.
$result = $s3->getBucketWebsite(array(
    'Bucket' => $bucket,
));
echo $result->getPath('IndexDocument/Suffix');

// 4.) Delete website configuration.
$result = $s3->deleteBucketWebsite(array(
    'Bucket' => $bucket,
));

Example of Configuring an Bucket Amazon S3 for Website Hosting

The following PHP code example first adds a website configuration to the specified bucket. The create_website_config method explicitly provides the index document and error document names. The sample also retrieves the website configuration and prints the response. For more information about the Amazon S3 website feature, see Hosting a Static Website on Amazon S3.

For instructions on how to create and test a working sample, see Using the AWS SDK for PHP and Running PHP Examples.

<?php

// Include the AWS SDK using the Composer autoloader.
require 'vendor/autoload.php';

use Aws\S3\S3Client;

$bucket = '*** Your Bucket Name ***';

// Instantiate the client.
$s3 = S3Client::factory();

// 1.) Add website configuration.
$result = $s3->putBucketWebsite(array(
    'Bucket'        => $bucket,    
    'IndexDocument' => array('Suffix' => 'index.html'),
    'ErrorDocument' => array('Key' => 'error.html'),
));

// 2.) Retrieve website configuration.
$result = $s3->getBucketWebsite(array(
    'Bucket' => $bucket,
));
echo $result->getPath('IndexDocument/Suffix');

// 3.) Delete website configuration.
$result = $s3->deleteBucketWebsite(array(
    'Bucket' => $bucket,
));