Feature Overview
This document provides an overview of APIs and SDK code samples related to static websites.
API | Operation | Description |
Setting a static website | Configures a static website for a bucket | |
Querying Static Website Configuration | Queries the static website configuration of a bucket | |
Deleting Static Website Configuration | Deletes the static website configuration of a bucket |
SDK API References
Setting a static website
Note
This API is used to configure a static website for a bucket.
Sample code
Objective-C
// Bucket name in the format of BucketName-APPID, which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucketNSString *bucket = @"examplebucket-1250000000";NSString *indexDocumentSuffix = @"index.html";NSString *errorDocKey = @"error.html";NSString *derPro = @"https";int errorCode = 451;NSString * replaceKeyPrefixWith = @"404.html";QCloudPutBucketWebsiteRequest *putReq = [QCloudPutBucketWebsiteRequest new];putReq.bucket = bucket;QCloudWebsiteConfiguration *config = [QCloudWebsiteConfiguration new];QCloudWebsiteIndexDocument *indexDocument = [QCloudWebsiteIndexDocument new];// Specify the object key suffix of the index document. For example, if index.html is specified, then when the root directory of the bucket is accessed,// the content of index.html will automatically be returned; when the article/ directory is accessed, the content of article/index.html will automatically be returned.indexDocument.suffix = indexDocumentSuffix;// Index document configurationconfig.indexDocument = indexDocument;// Error document configurationQCloudWebisteErrorDocument *errDocument = [QCloudWebisteErrorDocument new];errDocument.key = errorDocKey;// Specify the object key of the general error document. When an error occurs and no error code in the redirect rule is hit for redirect, the content of this object key will be returnedconfig.errorDocument = errDocument;// Configuration for redirecting all requestsQCloudWebsiteRedirectAllRequestsTo *redir = [QCloudWebsiteRedirectAllRequestsTo new];redir.protocol = derPro;// Specify the target protocol for redirecting all requests; only HTTPS can be usedconfig.redirectAllRequestsTo = redir;// Configuration for a single redirect ruleQCloudWebsiteRoutingRule *rule = [QCloudWebsiteRoutingRule new];// Conditions for the redirect ruleQCloudWebsiteCondition *contition = [QCloudWebsiteCondition new];contition.httpErrorCodeReturnedEquals = errorCode;rule.condition = contition;// Specific redirect destination of the redirect ruleQCloudWebsiteRedirect *webRe = [QCloudWebsiteRedirect new];webRe.protocol = derPro;// Specify the object key of the redirect destination specified in the rule. The prefix part matched in the original request will be replaced.// This can be set only if the Condition is set to KeyPrefixEqualswebRe.replaceKeyPrefixWith = replaceKeyPrefixWith;rule.redirect = webRe;QCloudWebsiteRoutingRules *routingRules = [QCloudWebsiteRoutingRules new];routingRules.routingRule = @[rule];// Redirect rule configuration. Up to 100 RoutingRule values can be setconfig.rules = routingRules;putReq.websiteConfiguration = config;[putReq setFinishBlock:^(id outputObject, NSError *error) {// outputObject contains all response HTTP headersNSDictionary* info = (NSDictionary *) outputObject;}];[[QCloudCOSXMLService defaultCOSXML] PutBucketWebsite:putReq];
Note
Swift
let req = QCloudPutBucketWebsiteRequest.init();// Bucket name in the format of BucketName-APPID, which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucketreq.bucket = "examplebucket-1250000000";let indexDocumentSuffix = "index.html";let errorDocKey = "error.html";let errorCode = 451;let replaceKeyPrefixWith = "404.html";let config = QCloudWebsiteConfiguration.init();let indexDocument = QCloudWebsiteIndexDocument.init();// Specify the object key suffix of the index document. For example, if index.html is specified, then when the root directory of the bucket is accessed,// the content of index.html will automatically be returned; when the article/ directory is accessed, the content of article/index.html will automatically be returned.indexDocument.suffix = indexDocumentSuffix;// Index document configurationconfig.indexDocument = indexDocument;// Error document configurationlet errDocument = QCloudWebisteErrorDocument.init();errDocument.key = errorDocKey;// Specify the object key of the general error document. When an error occurs and no error code in the redirect rule is hit for redirect, the content of this object key will be returnedconfig.errorDocument = errDocument;// Configuration for redirecting all requestslet redir = QCloudWebsiteRedirectAllRequestsTo.init();// Specify the target protocol for redirecting all requests; only HTTPS can be usedredir.protocol = "https";config.redirectAllRequestsTo = redir;// Configuration for a single redirect rulelet rule = QCloudWebsiteRoutingRule.init();// Conditions for the redirect rulelet contition = QCloudWebsiteCondition.init();contition.httpErrorCodeReturnedEquals = Int32(errorCode);rule.condition = contition;// Specific redirect destination of the redirect rulelet webRe = QCloudWebsiteRedirect.init();webRe.protocol = "https";// Specify the object key of the redirect destination specified in the rule. The prefix part matched in the original request will be replaced.// This can be set only if the Condition is set to KeyPrefixEqualswebRe.replaceKeyPrefixWith = replaceKeyPrefixWith;rule.redirect = webRe;let routingRules = QCloudWebsiteRoutingRules.init();routingRules.routingRule = [rule];// Redirect rule configuration. Up to 100 RoutingRule values can be setconfig.rules = routingRules;req.websiteConfiguration = config;req.finishBlock = {(result,error) inif let result = result {// The result contains the response header information} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().putBucketWebsite(req);
Note
Querying Static Website Configuration
Note
This API (
GET Bucket website) is used to query the static website configuration associated with a bucket.Sample code
Objective-C
QCloudGetBucketWebsiteRequest *getReq = [QCloudGetBucketWebsiteRequest new];// Bucket name in the format of BucketName-APPID, which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucketgetReq.bucket = @"examplebucket-1250000000";[getReq setFinishBlock:^(QCloudWebsiteConfiguration * result,NSError * error) {// Set up to 100 redirection rules using RoutingRuleQCloudWebsiteRoutingRules *rules =result.rules;// Index documentQCloudWebsiteIndexDocument *indexDocument = result.indexDocument;// Error documentQCloudWebisteErrorDocument *errorDocument = result.errorDocument;// Redirect all requestsQCloudWebsiteRedirectAllRequestsTo *redirectAllRequestsTo = result.redirectAllRequestsTo;}];[[QCloudCOSXMLService defaultCOSXML] GetBucketWebsite:getReq];
Note
Swift
let req = QCloudGetBucketWebsiteRequest.init();// Bucket name in the format of BucketName-APPID, which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucketreq.bucket = "examplebucket-1250000000";req.setFinish {(result,error) inif let result = result {let rules = result.rules} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().getBucketWebsite(req);
Note
Deleting Static Website Configuration
Note
This API (
DELETE Bucket website) is used to delete the static website configuration of a bucket.Sample code
Objective-C
QCloudDeleteBucketWebsiteRequest *delReq = [QCloudDeleteBucketWebsiteRequest new];// Bucket name in the format of BucketName-APPID, which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucketdelReq.bucket = @"examplebucket-1250000000";[delReq setFinishBlock:^(id outputObject, NSError *error) {// outputObject contains all response HTTP headersNSDictionary* info = (NSDictionary *) outputObject;}];[[QCloudCOSXMLService defaultCOSXML] DeleteBucketWebsite:delReq];
Note
Swift
let delReq = QCloudDeleteBucketWebsiteRequest.init();// Bucket name in the format of BucketName-APPID, which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucketdelReq.bucket = "examplebucket-1250000000";delReq.finishBlock = {(result,error) inif let result = result {// The result contains the response header information} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().deleteBucketWebsite(delReq);
Note