Static website hosting (Java SDK V2)
Static website hosting allows a bucket to serve content as a static website. You can use the OSS Java SDK V2 to set, query, and delete the static website hosting configuration of a bucket, including the index document and error document.
How it works
Static website hosting allows you to use a bucket as a static website. You can manage the following settings by using the OSS Java SDK V2:
Index document (IndexDocument): the default page returned when a visitor accesses the website root or a directory under it, typically
index.html.Error document (ErrorDocument): the default error page returned when a visitor requests an object that does not exist, for example
error.html.
After you configure static website hosting, bind a custom domain to the bucket and access the website through the domain. For more information, see Bind a custom domain.
Usage notes
The sample code in this topic uses the region ID
cn-hangzhouof the China (Hangzhou) region. Replace it with the region ID based on your actual region.The sample code in this topic reads access credentials from environment variables. Make sure that your AccessKey credentials are configured before you run the code.
Administrative permissions on the bucket are required to configure static website hosting.
Upload the index document and error document to the bucket in advance. Otherwise, 404 errors are returned when visitors access these pages.
You can also enable, modify, or disable static website hosting in the OSS console.
The following permissions are required:
oss:PutBucketWebsiteto set the configuration,oss:GetBucketWebsiteto view it, andoss:DeleteBucketWebsiteto delete it.Static website hosting is not supported for region-less buckets.
Static website hosting does not redirect requests to an index document in a subdirectory. When you access a subdirectory, OSS returns the index document of the root directory.
In addition to the index document and error document, you can use routing rules (RoutingRules) to configure mirroring and redirection. For a mirroring example, see "Set mirroring rules" below.
Sample code
Set the static website hosting configuration
Query the static website hosting configuration
Delete the static website hosting configuration
References
For the synchronous sample code, see PutBucketWebsite.java, GetBucketWebsite.java, and DeleteBucketWebsite.java.
For the asynchronous sample code, see PutBucketWebsiteAsync.java, GetBucketWebsiteAsync.java, and DeleteBucketWebsiteAsync.java.