| Did this page help you? Yes No Tell us about it... |
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 |
|
2 |
To add website configuration to a bucket, execute the
To retrieve website configuration, execute the
To delete your bucket website configuration, execute the
|
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;
}
}