Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)
Print this pageEmail this pageGo to the ForumsView the PDFShare this page on TwitterShare this page on FacebookBookmark this page on DeliciousSubmit this page to RedditSubmit this page to DiggDid this page help you?  Yes  No   Tell us about it...

Managing Websites with the AWS SDK for Java

The following tasks guide you through using the Java classes to manage website configuration to your bucket. For more information about the Amazon S3 website feature, see Hosting Websites on Amazon S3.

Managing Website Configuration

1

Create an instance of the AmazonS3 class by providing your AWS credentials.

2

To add website configuration to a bucket, execute the AmazonS3.setBucketWebsiteConfiguration method. You need to provide the bucket name and the website configuration information including the index document and the error document names. You must provide the index document, however, the error document is optional. You provide website configuration information by creating a BucketWebsiteConfiguration object.

To retrieve website configuration, execute the AmazonS3.getBucketWebsiteConfiguration method by providing the bucket name.

To delete your bucket website configuration, execute the AmazonS3.deleteBucketWebsiteConfiguration method by providing the bucket name. After you remove the website configuration, the bucket is no longer available via the website endpoint. For more information, see see Website Endpoints.


The following Java code sample demonstrates the preceding tasks.

AWSCredentials myCredentials = new BasicAWSCredentials(
                                 myAccessKeyID, mySecretKey);
AmazonS3 s3client = new AmazonS3Client(myCredentials);
// Add website configuration.
s3Client.setBucketWebsiteConfiguration(bucketName, 
    		new BucketWebsiteConfiguration(indexDoc , errorDoc));
 
// Get website configuration.
BucketWebsiteConfiguration bucketWebsiteConfiguration = 
		       s3Client.getBucketWebsiteConfiguration(bucketName);
	
// Delete website configuration.
s3Client.deleteBucketWebsiteConfiguration(bucketName);

Example

The following Java code example adds a website configuration to the specified bucket, retrieves it and deletes the website configuration. For instructions on how to create and test a working sample, see Testing the Java Code Examples.

import java.io.IOException;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.BucketWebsiteConfiguration;


public class S3Sample {
	private static String bucketName = "mvphp1";
	private static String indexDoc = "index.html";
	private static String errorDoc = "error.html";
	
	public static void main(String[] args) throws IOException {
        AmazonS3 s3Client = new AmazonS3Client(new PropertiesCredentials(
           S3Sample.class.getResourceAsStream("AwsCredentials.properties")));
   
        try {
        	// Get existing website configuration, if any.
            getWebsiteConfig(s3Client);
    		
    		// Set new website configuration.
    		s3Client.setBucketWebsiteConfiguration(bucketName, 
    		   new BucketWebsiteConfiguration(indexDoc, errorDoc));
    		
    		// Verify (Get website configuration again).
            getWebsiteConfig(s3Client);
            
            // Delete
            s3Client.deleteBucketWebsiteConfiguration(bucketName);

       		// Verify (Get website configuration again)
              getWebsiteConfig(s3Client);
            
  
            
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which" +
            		" means your request made it " +
                    "to Amazon S3, but was rejected with an error response" +
                    " for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which means"+
            		" the client encountered " +
                    "a serious internal problem while trying to " +
                    "communicate with Amazon S3, " +
                    "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        }
    }

	private static BucketWebsiteConfiguration getWebsiteConfig(
	                                              AmazonS3 s3Client) {
		System.out.println("Get website config");   
		
		// 1. Get website config.
		BucketWebsiteConfiguration bucketWebsiteConfiguration = 
			  s3Client.getBucketWebsiteConfiguration(bucketName);
		if (bucketWebsiteConfiguration == null)
		{
			System.out.println("No website config.");
		}
		else
		{
		     System.out.println("Index doc:" + 
		       bucketWebsiteConfiguration.getIndexDocumentSuffix());
		     System.out.println("Error doc:" + 
		       bucketWebsiteConfiguration.getErrorDocument());
		}
		return bucketWebsiteConfiguration;
	}
}