Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

使用AWS SDK for Java管理网站

以下任务引导您使用 Java 类管理对存储桶的网站配置。有关 Amazon S3 网站功能的更多信息,请参阅在 Amazon S3 上托管静态网站

管理网站配置

1

创建 AmazonS3 类的实例。

2

要将网站配置添加到存储桶,请执行 AmazonS3.setBucketWebsiteConfiguration 方法。您需要提供存储桶名称和网站配置信息,包括索引文档和错误文档名称。您必须提供索引文档,但错误文档是可选的。通过创建 BucketWebsiteConfiguration 对象,提供网站配置信息。

要检索网站配置,请通过提供存储桶名称来执行 AmazonS3.getBucketWebsiteConfiguration 方法。

要删除存储桶网站配置,请通过提供存储桶名称来执行 AmazonS3.deleteBucketWebsiteConfiguration 方法。在删除网站配置之后,无法再从网站终端节点使用存储桶。有关更多信息,请参阅 网站终端节点

以下 Java 代码示例演示了上述任务。

Copy
AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); // Add website configuration. s3Client.setBucketWebsiteConfiguration(bucketName, new BucketWebsiteConfiguration(indexDoc , errorDoc)); // Get website configuration. BucketWebsiteConfiguration bucketWebsiteConfiguration = s3Client.getBucketWebsiteConfiguration(bucketName); // Delete website configuration. s3Client.deleteBucketWebsiteConfiguration(bucketName);

以下 Java 代码示例将网站配置添加到指定的存储桶、对其进行检索并删除网站配置。有关如何创建和测试有效示例的说明,请参阅 测试 Java 代码示例

Copy
import java.io.IOException; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.BucketWebsiteConfiguration; public class WebsiteConfiguration { private static String bucketName = "*** bucket name ***"; private static String indexDoc = "*** index document name ***"; private static String errorDoc = "*** error document name ***"; public static void main(String[] args) throws IOException { AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); 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; } }