You need to enable JavaScript to run this app.
导航

管理网站配置(C++ SDK)

最近更新时间2024.02.04 18:31:05

首次发布时间2023.03.15 15:05:53

TOS 支持托管静态网站,您可以将静态网站的所有内容存储至 TOS 存储桶,配置静态网站规则后,即可通过存储桶域名访问该网站。

设置网站配置规则

注意

  • 一个存储桶中只能创建一条静态网站托管规则。
  • 设置静态网站后,必须绑定自定义域名才能生效,具体操作,请参见绑定自定义域名
  • 出于安全合规考虑,从 2022年10月18日开始,如果您使用存储桶的默认域名访问网页类型文件(mimetype为text/html,扩展名包括 HTM、HTML、JSP、PLG、HTX、STM),Response Header中会自动加上 Content-Disposition:attachment,即从浏览器访问网页类型文件时,将不会直接预览网站,而会将网站的内容下载到本地。

示例代码

以下代码用于设置桶 examplebucket 的网站配置规则。

#include "TosClientV2.h"
using namespace VolcengineTos;

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";

    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);

    PutBucketWebsiteInput input(bucketName);
    // 设置将所有请求重定向到指定 hostName
    // RedirectAllRequestsTo redirectAllRequestsTo("www.volcengine.com", "https");
    // input.setRedirectAllRequestsTo(redirectAllRequestsTo);

    // 设置默认主页为 index.html
    // 设置访问子目录时,是否禁止转到子目录下的默认主页,true 为转到根目录下的默认主页,false 为转到子目录下的默认主页
    IndexDocument indexDocument("index.html", true);

    // 设置错误页面为 error.html
    ErrorDocument errorDocument("error.html");

    // 设置路由规则
    // 设置匹配条件:只有匹配此前缀 website 才能匹配此规则。访问指定 Object 时,返回此 status 为 404 才能匹配此规则
    RoutingRuleCondition condition("website", 404);
    // 设置跳转规则:
    // 方法支持 HTTP 和 HTTPS
    // 跳转时的域名为 www.volcengine.com
    // 跳转时 Object 名称的前缀将替换成 voc,可以为空
    // 跳转时返回的状态码为 301
    RoutingRuleRedirect redirect(ProtocolType::Http, "www.volcengine.com", "voc", 301);
    RoutingRule rule(condition, redirect);

    input.setIndexDocument(indexDocument);
    input.setErrorDocument(errorDocument);
    input.setRoutingRules({rule});

    auto output = client.putBucketWebsite(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "PutBucketWebsite failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
    std::cout << "PutBucketWebsite success." << std::endl;

    // 释放网络等资源
    CloseClient();
    return 0;
}

获取网站配置规则

注意

要获取桶的网站配置规则,默认您必须为桶所有者。

示例代码

以下代码用于获取桶 examplebucket 的网站配置规则。

#include "TosClientV2.h"
using namespace VolcengineTos;

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";

    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);
    GetBucketWebsiteInput input(bucketName);

    auto output = client.getBucketWebsite(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "GetBucketWebsite failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
    std::cout << "GetBucketWebsite success." << std::endl;
    auto res = output.result();
    if (res.getRedirectAllRequestsTo() != nullptr){
        auto redirectAllRequestsTo= *res.getRedirectAllRequestsTo();
        std::cout << "host name:" << redirectAllRequestsTo.getHostName() << std::endl;
        std::cout << "protocol:" << redirectAllRequestsTo.getProtocol() << std::endl;
    }
    if (res.getIndexDocument() != nullptr){
        auto indexDocument = *res.getIndexDocument();
        std::cout << "indexDocument suffix:" << indexDocument.getSuffix() << std::endl;
        std::cout << "indexDocument forbidden subdir:" << indexDocument.isForbiddenSubDir() << std::endl;
    }
    if (res.getErrorDocument() != nullptr){
        auto errorDocument = *res.getErrorDocument();
        std::cout << "error document key:" << errorDocument.getKey() << std::endl;
    }
    for (const auto& rule : output.result().getRoutingRules()) {
        auto condition = rule.getCondition();
        auto redirect = rule.getRedirect();
        std::cout << "Condition:" << std::endl;
        std::cout << "KeyPrefixEquals:" << condition.getKeyPrefixEquals() << std::endl;
        std::cout << "HttpErrorCodeReturnedEquals:" << condition.getHttpErrorCodeReturnedEquals() << std::endl;
        std::cout << "Redirect:" << std::endl;
        std::cout << "HostName:" << redirect.getHostName() << std::endl;
        std::cout << "ReplaceKeyPrefixWith:" << redirect.getReplaceKeyPrefixWith() << std::endl;
        std::cout << "ProtocolType:" << redirect.getStringFormatProtocol() << std::endl;
        std::cout << "HttpRedirectCode:" << redirect.getHttpRedirectCode() << std::endl;
        std::cout << "ReplaceKeyWith:" << redirect.getReplaceKeyWith() << std::endl;
    }

    // 释放网络等资源
    CloseClient();
    return 0;
}

删除网站配置规则

注意

要删除桶的网站配置规则,默认您必须为桶所有者。

示例代码

以下代码用于删除桶 examplebucket 的网站配置规则。

#include "TosClientV2.h"
using namespace VolcengineTos;

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";

    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);
    DeleteBucketWebsiteInput input(bucketName);
    auto output = client.deleteBucketWebsite(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "DeleteBucketWebsite failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
    std::cout << "DeleteBucketWebsite success." << std::endl;
    // 释放网络等资源
    CloseClient();
    return 0;
}

相关文档

关于设置静态网站配置的更多信息,请参见设置静态网站