文档中心 > 静态网站托管
静态网站托管

最近更新时间:2021-09-18

您可以将桶配置成静态网站托管模式。配置生效后,访问网站相当于访问桶,并且能够自动跳转至指定的索引页面和错误页面。

更多关于静态网站托管的介绍,请参见用户指南中的配置静态网站托管


设置静态网站托管

以下代码用于设置静态网站托管:

public void testBucketWebsite(){

        try {

            // Set the new website configuration.

            s3.setBucketWebsiteConfiguration(bucketName, new BucketWebsiteConfiguration("index.html", "error.html"));


            // Verify that the configuration was set properly by printing it.

            printWebsiteConfig(s3, bucketName);


            // Delete the website configuration.

            s3.deleteBucketWebsiteConfiguration(bucketName);


            // Verify that the website configuration was deleted by printing it.

            printWebsiteConfig(s3, bucketName);

        } catch(AmazonServiceException e){

            System.err.println(e.getErrorMessage());

        }

    }


查看静态网站托管配置

以下代码用于查看静态网站托管配置:

private static void printWebsiteConfig(AmazonS3 s3Client, String bucketName) {

        System.out.println("Website configuration: ");

        BucketWebsiteConfiguration bucketWebsiteConfig = s3Client.getBucketWebsiteConfiguration(bucketName);

        if (bucketWebsiteConfig == null) {

            System.out.println("No website config.");

        } else {

            System.out.println("Index doc: " + bucketWebsiteConfig.getIndexDocumentSuffix());

            System.out.println("Error doc: " + bucketWebsiteConfig.getErrorDocument());

        }

    }