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

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

设置静态网站托管

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

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
      /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
 
    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret,  conf);
   
    /* 设置静态网站托管 */
    SetBucketWebsiteRequest request(BucketName);
    request.setIndexDocument("index.html");
    request.setErrorDocument("error.html");

    auto outcome = client.SetBucketWebsite(request);
  
    if (!outcome.isSuccess()) {
        /* 异常处理 */
        std::cout << "SetBucketWebsite fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

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

查看静态网站托管配置

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

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
      /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /* 获取静态网站托管配置 */
    GetBucketWebsiteRequest request(BucketName);
    auto outcome = client.GetBucketWebsite(request);

    if (outcome.isSuccess()) {
        std::cout << "GetBucketWebsite success,IndexDocument: " << outcome.result().IndexDocument() <<
        " ,ErrorDocument: " << outcome.result().ErrorDocument() << std::endl;
    }
    else { 
        /* 异常处理 */
        std::cout << "GetBucketWebsite fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

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

删除静态网站托管配置

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

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
     /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
 
    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret,  conf);
 
 
    /* 删除静态网站托管配置 */
    DeleteBucketWebsiteRequest request(BucketName);

    auto outcome = client.DeleteBucketWebsite(request);
  
    if (!outcome.isSuccess()) {
        /* 异常处理 */
        std::cout << "DeleteBucketWebsite fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

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