You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Manage Bucket Configuration
Managing Website Configuration (C++ SDK)
Copy page
Download PDF
Managing Website Configuration (C++ SDK)
TOS supports hosting static websites. You can store all static website content in a TOS bucket. After configuring static website rules, the website can be accessed via the bucket domain name.​
Set Website Configuration Rules​
warning​
  • Only one static website hosting rule can be created in a storage bucket.​
  • After configuring the static website, you must bind a custom domain name for it to take effect. For detailed instructions, please refer to Bind Custom Domain Name.​
  • For security and compliance purposes, effective October 18, 2022, when you access web-type files (with mimetype 'text/html' and extensions including HTM, HTML, JSP, PLG, HTX, STM) via the default domain of a storage bucket, the Response Header will automatically include Content-Disposition:attachment. As a result, when these files are accessed through a browser, the website will not be previewed directly; instead, the website's content will be downloaded locally.​
Sample Code​
The following code is used to set the website configuration rules for the bucket examplebucket.​
​
#include "TosClientV2.h"​
using namespace VolcengineTos;​
​
int main(void){​
​
​
std::string region = "Your Region";​
std::string accessKey = std::getenv("TOS_ACCESS_KEY");​
std::string secretKey = std::getenv("TOS_SECRET_KEY");​
​
std::string bucketName = "examplebucket";​
​
​
InitializeClient();​
​
TosClientV2 client(region, accessKey, secretKey);​
​
PutBucketWebsiteInput input(bucketName);​
​
// RedirectAllRequestsTo redirectAllRequestsTo("www.volcengine.com", "https");​
// input.setRedirectAllRequestsTo(redirectAllRequestsTo);​
​
​
IndexDocument indexDocument("index.html", true);​
​
​
ErrorDocument errorDocument("error.html");​
​
​
RoutingRuleCondition condition("website", 404);​
​
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;​
}​
​
Retrieve Website Configuration Rules​
warning​
To obtain the website configuration rules for a bucket, you must, by default, be the bucket owner.​
Sample Code​
The following code is used to retrieve the website configuration rules for the bucket 'examplebucket'.​
​
#include "TosClientV2.h"​
using namespace VolcengineTos;​
​
int main(void){​
​
​
std::string region = "Your Region";​
std::string accessKey = std::getenv("TOS_ACCESS_KEY");​
std::string secretKey = std::getenv("TOS_SECRET_KEY");​
​
std::string bucketName = "examplebucket";​
​
​
InitializeClient();​
​
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;​
}​
​
Delete Website Configuration Rules​
warning​
To delete the website configuration rules of a bucket, you must be the bucket owner by default.​
Sample Code​
The following code is used to delete the website configuration rules for the bucket examplebucket.​
​
#include "TosClientV2.h"​
using namespace VolcengineTos;​
​
int main(void){​
​
​
std::string region = "Your Region";​
std::string accessKey = std::getenv("TOS_ACCESS_KEY");​
std::string secretKey = std::getenv("TOS_SECRET_KEY");​
​
std::string bucketName = "examplebucket";​
​
​
InitializeClient();​
​
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;​
}​
​
Relevant Documents​
For more information about configuring static website settings, see Configuring a Static Website.​
Last updated: 2025.09.25 14:22:46