全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

管理存储空间静态网站托管设置

最近更新时间:2026-11-03 19:37:39

设置静态网站托管配置

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

public void putBucketWebsiteConfiguration() {
    // 初始化 Ks3Client,参见“初始化”文档
    Ks3Client ks3Client = initKs3Client();
    try {
        BucketWebsiteConfiguration config = new BucketWebsiteConfiguration();

        // 配置默认首页
        IndexDocument indexDocument = new IndexDocument();
        indexDocument.setSuffix("index.html");
        indexDocument.setSupportSubDir(true);
        config.setIndexDocument(indexDocument);

        // 配置默认404页
        ErrorDocument errorDocument = new ErrorDocument();
        errorDocument.setKey("error.html");
        config.setErrorDocument(errorDocument);

        // 配置重定向规则
        List<RoutingRule> routingRules = new ArrayList<RoutingRule>();

        // 规则1:将 docs/ 前缀重定向到 documents/
        RoutingRule rule1 = new RoutingRule();
        RoutingRuleCondition condition1 = new RoutingRuleCondition();
        condition1.setKeyPrefixEquals("docs/");
        rule1.setCondition(condition1);
        RoutingRuleRedirect redirect1 = new RoutingRuleRedirect();
        redirect1.setReplaceKeyPrefixWith("documents/");
        rule1.setRedirect(redirect1);
        routingRules.add(rule1);

        // 规则2:将 404 错误重定向到其他站点
        RoutingRule rule2 = new RoutingRule();
        RoutingRuleCondition condition2 = new RoutingRuleCondition();
        condition2.setHttpErrorCodeReturnedEquals("404");
        rule2.setCondition(condition2);
        RoutingRuleRedirect redirect2 = new RoutingRuleRedirect();
        redirect2.setProtocol("https");
        redirect2.setHostName("www.example.com");
        redirect2.setHttpRedirectCode("302");
        redirect2.setReplaceKeyPrefixWith("report-404/");
        redirect2.setPassQueryString(true);
        rule2.setRedirect(redirect2);
        routingRules.add(rule2);

        config.setRoutingRules(routingRules);

        ks3Client.putBucketWebsiteConfiguration("<yourBucketName>", config);
        System.out.println("静态网站托管配置已设置成功");
    } catch (Ks3ServiceException e) {
        System.out.println("Ks3ServiceException occurred, which means the request was made to KS3, but KS3 returned an error response for some reason.");
        System.out.println("Error Message: " + e.getErrorMessage());
        System.out.println("Error Code: " + e.getErrorCode());
        System.out.println("Request ID: " + e.getRequestId());
    } catch (Ks3ClientException e) {
        System.out.println("Ks3ClientException occurred, which means the client encountered an internal error while trying to communicate with KS3.");
        System.out.println("Error Message: " + e.getMessage());
    } finally {
        // 不再使用时,关闭 Ks3Client
        ks3Client.shutdown();
    }
}

获取静态网站托管配置

以下代码用于获取静态网站托管配置:

public void getBucketWebsiteConfiguration() {
    // 初始化 Ks3Client,参见“初始化”文档
    Ks3Client ks3Client = initKs3Client();
    try {
        BucketWebsiteConfiguration config = ks3Client.getBucketWebsiteConfiguration("<yourBucketName>");

        // 输出默认首页配置
        if (config.getIndexDocument() != null) {
            System.out.println("IndexDocument Suffix: " + config.getIndexDocument().getSuffix());
            System.out.println("SupportSubDir: " + config.getIndexDocument().isSupportSubDir());
        }

        // 输出默认404页配置
        if (config.getErrorDocument() != null) {
            System.out.println("ErrorDocument Key: " + config.getErrorDocument().getKey());
            if (config.getErrorDocument().getHttpStatus() != null) {
                System.out.println("ErrorDocument HttpStatus: " + config.getErrorDocument().getHttpStatus());
            }
        }

        // 输出重定向规则
        if (config.getRoutingRules() != null) {
            for (int i = 0; i < config.getRoutingRules().size(); i++) {
                RoutingRule rule = config.getRoutingRules().get(i);
                System.out.println("RoutingRule[" + i + "]:");
                if (rule.getCondition() != null) {
                    System.out.println("  KeyPrefixEquals: " + rule.getCondition().getKeyPrefixEquals());
                    System.out.println("  HttpErrorCodeReturnedEquals: " + rule.getCondition().getHttpErrorCodeReturnedEquals());
                }
                if (rule.getRedirect() != null) {
                    System.out.println("  Protocol: " + rule.getRedirect().getProtocol());
                    System.out.println("  HostName: " + rule.getRedirect().getHostName());
                    System.out.println("  HttpRedirectCode: " + rule.getRedirect().getHttpRedirectCode());
                    System.out.println("  ReplaceKeyPrefixWith: " + rule.getRedirect().getReplaceKeyPrefixWith());
                    System.out.println("  ReplaceKeyWith: " + rule.getRedirect().getReplaceKeyWith());
                    System.out.println("  PassQueryString: " + rule.getRedirect().isPassQueryString());
                }
            }
        }
    } catch (Ks3ServiceException e) {
        System.out.println("Ks3ServiceException occurred, which means the request was made to KS3, but KS3 returned an error response for some reason.");
        System.out.println("Error Message: " + e.getErrorMessage());
        System.out.println("Error Code: " + e.getErrorCode());
        System.out.println("Request ID: " + e.getRequestId());
    } catch (Ks3ClientException e) {
        System.out.println("Ks3ClientException occurred, which means the client encountered an internal error while trying to communicate with KS3.");
        System.out.println("Error Message: " + e.getMessage());
    } finally {
        // 不再使用时,关闭 Ks3Client
        ks3Client.shutdown();
    }
}

删除静态网站托管配置

以下代码用于删除静态网站托管配置:

public void deleteBucketWebsiteConfiguration() {
    // 初始化 Ks3Client,参见“初始化”文档
    Ks3Client ks3Client = initKs3Client();
    try {
        ks3Client.deleteBucketWebsiteConfiguration("<yourBucketName>");
        System.out.println("静态网站托管配置删除成功");
    } catch (Ks3ServiceException e) {
        System.out.println("Ks3ServiceException occurred, which means the request was made to KS3, but KS3 returned an error response for some reason.");
        System.out.println("Error Message: " + e.getErrorMessage());
        System.out.println("Error Code: " + e.getErrorCode());
        System.out.println("Request ID: " + e.getRequestId());
    } catch (Ks3ClientException e) {
        System.out.println("Ks3ClientException occurred, which means the client encountered an internal error while trying to communicate with KS3.");
        System.out.println("Error Message: " + e.getMessage());
    } finally {
        // 不再使用时,关闭 Ks3Client
        ks3Client.shutdown();
    }
}
文档导读
纯净模式常规模式

纯净模式

点击可全屏预览文档内容
文档反馈