TOS 支持托管静态网站,您可以将静态网站的所有内容存储至 TOS 存储桶,配置静态网站规则后,即可通过存储桶域名访问该网站。
您可以通过 TOS Java SDK 的 putBucketWebsite
接口设置指定桶的网站配置规则。
注意
Content-Disposition:attachment
,即从浏览器访问网页类型文件时,将不会直接预览网站,而会将网站的内容下载到本地。以下代码展示两种场景,如何设置重定向所有的请求到另外一个站点,以及如何设置特定的重定向规则来重定向特定的请求。
import com.volcengine.tos.TOSV2; import com.volcengine.tos.TOSV2ClientBuilder; import com.volcengine.tos.TosClientException; import com.volcengine.tos.TosServerException; import com.volcengine.tos.comm.common.ProtocolType; import com.volcengine.tos.model.bucket.*; import java.util.ArrayList; import java.util.List; public class PutBucketWebsiteExample { public static void main(String[] args) { String endpoint = "your endpoint"; String region = "your region"; String accessKey = System.getenv("TOS_ACCESS_KEY"); String secretKey = System.getenv("TOS_SECRET_KEY"); String bucketName = "bucket-example"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ // 场景1 重定向所有的请求到另一个站点 RedirectAllRequestsTo redirectAllRequestsTo = new RedirectAllRequestsTo().setHostname("www.example.com").setProtocol("http"); PutBucketWebsiteInput input = new PutBucketWebsiteInput().setBucket(bucketName) .setRedirectAllRequestsTo(redirectAllRequestsTo); PutBucketWebsiteOutput output = tos.putBucketWebsite(input); System.out.println("putBucketWebsite succeed"); // 场景2 根据规则重定向请求 // RoutingRule 设置前缀匹配,获取重定向结果 RoutingRule rule = new RoutingRule() // 重定向规则的条件配置 .setCondition(new RoutingRuleCondition() // 指定重定向规则的对象键前缀匹配条件 .setKeyPrefixEquals("prefix") // 指定重定向规则的错误码匹配条件 .setHttpErrorCodeReturnedEquals(404)) // 重定向规则的具体重定向目标配置 .setRedirect(new RoutingRuleRedirect() // 指定重定向规则的目标协议 .setProtocol(ProtocolType.PROTOCOL_HTTP) // 指定重定向的目的地址 .setHostname("www.redirect.com")); List<RoutingRule> rules = new ArrayList<>(); rules.add(rule); IndexDocument indexDocument = new IndexDocument().setSuffix("index.html").setForbiddenSubDir(true); ErrorDocument errorDocument = new ErrorDocument().setKey("error.html"); input = new PutBucketWebsiteInput().setBucket(bucketName) .setRoutingRules(rules) .setIndexDocument(indexDocument) .setErrorDocument(errorDocument); output = tos.putBucketWebsite(input); System.out.println("putBucketWebsite succeed"); } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("putBucketWebsite failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("putBucketWebsite failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("Code: " + e.getCode()); System.out.println("Message: " + e.getMessage()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里 System.out.println("putBucketWebsite failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
您可以通过 TOS Java SDK 的 getBucketWebsite
接口获取指定桶的网站配置规则。
注意
要获取桶的网站配置规则,默认您必须为桶所有者。
以下代码展示如何获取桶的网站配置规则。
import com.volcengine.tos.TOSV2; import com.volcengine.tos.TOSV2ClientBuilder; import com.volcengine.tos.TosClientException; import com.volcengine.tos.TosServerException; import com.volcengine.tos.model.bucket.*; public class GetBucketWebsiteExample { public static void main(String[] args) { String endpoint = "your endpoint"; String region = "your region"; String accessKey = System.getenv("TOS_ACCESS_KEY"); String secretKey = System.getenv("TOS_SECRET_KEY"); String bucketName = "bucket-example"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ GetBucketWebsiteInput input = new GetBucketWebsiteInput().setBucket(bucketName); GetBucketWebsiteOutput output = tos.getBucketWebsite(input); System.out.println("getBucketWebsite succeed"); if (output.getRedirectAllRequestsTo() != null) { System.out.println("getBucketWebsite RedirectAllRequestsTo HostName:" + output.getRedirectAllRequestsTo().getHostname()); System.out.println("getBucketWebsite RedirectAllRequestsTo Protocol:" + output.getRedirectAllRequestsTo().getProtocol()); } if (output.getIndexDocument() != null) { System.out.println("getBucketWebsite IndexDocument ForbiddenSubDir:" + output.getIndexDocument().isForbiddenSubDir()); System.out.println("getBucketWebsite IndexDocument Suffix:" + output.getIndexDocument().getSuffix()); } if (output.getErrorDocument() != null) { System.out.println("getBucketWebsite ErrorDocument Key:" + output.getErrorDocument().getKey()); } if (output.getRoutingRules() != null && output.getRoutingRules().size() > 0) { for(int i = 0; i < output.getRoutingRules().size(); i++) { RoutingRule rule = output.getRoutingRules().get(i); System.out.println("No." + i + " rule"); if (rule.getCondition() != null) { System.out.println("getBucketWebsite RoutingRules Rule Condition HttpErrorCodeReturnedEquals:" + rule.getCondition().getHttpErrorCodeReturnedEquals()); System.out.println("getBucketWebsite RoutingRules Rule Condition KeyPrefixEquals:" + rule.getCondition().getKeyPrefixEquals()); } if (rule.getRedirect() != null) { System.out.println("getBucketWebsite RoutingRules Rule Redirect HostName:" + rule.getRedirect().getHostname()); System.out.println("getBucketWebsite RoutingRules Rule Redirect Protocol:" + rule.getRedirect().getProtocol()); System.out.println("getBucketWebsite RoutingRules Rule Redirect ReplaceKeyWith:" + rule.getRedirect().getReplaceKeyWith()); System.out.println("getBucketWebsite RoutingRules Rule Redirect ReplaceKeyPrefixWith:" + rule.getRedirect().getReplaceKeyPrefixWith()); System.out.println("getBucketWebsite RoutingRules Rule Redirect HttpRedirectCode:" + rule.getRedirect().getHttpRedirectCode()); } } } } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("getBucketWebsite failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("getBucketWebsite failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("Code: " + e.getCode()); System.out.println("Message: " + e.getMessage()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里 System.out.println("getBucketWebsite failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
您可以通过 TOS Java SDK 的 deleteBucketWebsite
接口删除指定桶的网站配置规则。
注意
要删除桶的网站配置规则,默认您必须为桶所有者。
以下代码展示如何删除桶的网站配置规则。
import com.volcengine.tos.TOSV2; import com.volcengine.tos.TOSV2ClientBuilder; import com.volcengine.tos.TosClientException; import com.volcengine.tos.TosServerException; import com.volcengine.tos.model.bucket.DeleteBucketWebsiteInput; import com.volcengine.tos.model.bucket.DeleteBucketWebsiteOutput; public class DeleteBucketWebsiteExample { public static void main(String[] args) { String endpoint = "your endpoint"; String region = "your region"; String accessKey = System.getenv("TOS_ACCESS_KEY"); String secretKey = System.getenv("TOS_SECRET_KEY"); String bucketName = "bucket-example"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ DeleteBucketWebsiteInput input = new DeleteBucketWebsiteInput().setBucket(bucketName); DeleteBucketWebsiteOutput output = tos.deleteBucketWebsite(input); System.out.println("deleteBucketWebsite succeed, " + output); } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("deleteBucketWebsite failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("deleteBucketWebsite failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("Code: " + e.getCode()); System.out.println("Message: " + e.getMessage()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里 System.out.println("deleteBucketWebsite failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
关于设置静态网站配置的更多信息,请参见设置静态网站。