All Products
Search
Document Center

Object Storage Service:Static website hosting (C++ SDK)

Last Updated:Mar 20, 2026

Configure an Object Storage Service (OSS) bucket to serve a static website. Once enabled, OSS automatically redirects visitors to your default homepage or default 404 page when they access the bucket.

Prerequisites

Before you begin, make sure that you have:

  • An OSS bucket

  • The required RAM permissions for each operation: For details on granting these permissions, see Grant custom access policies to a RAM user.

    OperationPermission
    Set static website hostingoss:PutBucketWebsite
    Retrieve the configurationoss:GetBucketWebsite
    Delete the configurationoss:DeleteBucketWebsite
  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables set with your AccessKey ID and AccessKey secret

Usage notes

  • The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For a full list of endpoints, see Regions and endpoints.

  • The examples create an OSSClient instance using an OSS endpoint. To create an OSSClient using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.

Set static website hosting

SetBucketWebsiteRequest accepts two parameters: the index document and the error document. The index document is the default homepage, and the error document is the default 404 page.

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

int main(void)
{
    /* Initialize OSS account information. */

    /* Set yourEndpoint to the endpoint of the region where the bucket is located.
       For example, for the China (Hangzhou) region: https://oss-cn-hangzhou.aliyuncs.com */
    std::string Endpoint = "yourEndpoint";
    /* Set yourRegion to the region where the bucket is located.
       For example, for the China (Hangzhou) region: cn-hangzhou */
    std::string Region = "yourRegion";
    /* Specify the bucket name, for example, examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run the sample code,
       make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Set static website hosting. */
    SetBucketWebsiteRequest request(BucketName);
    /* Set the default homepage (index document) to index.html. */
    request.setIndexDocument("index.html");
    /* Set the default 404 page (error document) to error.html. */
    request.setErrorDocument("error.html");

    auto outcome = client.SetBucketWebsite(request);

    if (!outcome.isSuccess()) {
        /* Handle exceptions. */
        std::cout << "SetBucketWebsite fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

View the static website hosting configuration

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

int main(void)
{
    /* Initialize OSS account information. */

    /* Set yourEndpoint to the endpoint of the region where the bucket is located.
       For example, for the China (Hangzhou) region: https://oss-cn-hangzhou.aliyuncs.com */
    std::string Endpoint = "yourEndpoint";
    /* Set yourRegion to the region where the bucket is located.
       For example, for the China (Hangzhou) region: cn-hangzhou */
    std::string Region = "yourRegion";
    /* Specify the bucket name, for example, examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run the sample code,
       make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Retrieve the static website hosting configuration. */
    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 {
        /* Handle exceptions. */
        std::cout << "GetBucketWebsite fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Delete the static website hosting configuration

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

int main(void)
{
    /* Initialize OSS account information. */

    /* Set yourEndpoint to the endpoint of the region where the bucket is located.
       For example, for the China (Hangzhou) region: https://oss-cn-hangzhou.aliyuncs.com */
    std::string Endpoint = "yourEndpoint";
    /* Set yourRegion to the region where the bucket is located.
       For example, for the China (Hangzhou) region: cn-hangzhou */
    std::string Region = "yourRegion";
    /* Specify the bucket name, for example, examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run the sample code,
       make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Delete the static website hosting configuration. */
    DeleteBucketWebsiteRequest request(BucketName);

    auto outcome = client.DeleteBucketWebsite(request);

    if (!outcome.isSuccess()) {
        /* Handle exceptions. */
        std::cout << "DeleteBucketWebsite fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

References