This document provides an overview of APIs and SDK code samples related to static website.
API | Operation | Description |
---|---|---|
PUT Bucket website | Setting static website configuration | Sets static website configuration for a bucket |
GET Bucket website | Querying static website configuration | Queries the static website configuration of a bucket |
DELETE Bucket website | Deleting static website configuration | Deletes the static website configuration from a bucket |
For the parameters and method descriptions of all the APIs in the SDK, please see SDK API Reference.
This API is used to configure a bucket as a static website.
Objective-C
// Bucket name in the format: `BucketName-APPID`
NSString *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 for the index document. For example, if `index.html` is specified, then when the root directory of the bucket is accessed,
// `index.html` will automatically be returned; when the `article/` directory is accessed, `article/index.html` will automatically be returned
indexDocument.suffix = indexDocumentSuffix;
// Configure the index document
config.indexDocument = indexDocument;
// Configure the error document
QCloudWebisteErrorDocument *errDocument = [QCloudWebisteErrorDocument new];
errDocument.key = errorDocKey;
// Specify the object key for the general error document. When an error occurs and no error code in the redirect rule is hit, this object key will be returned
config.errorDocument = errDocument;
// Configuration for redirecting all requests
QCloudWebsiteRedirectAllRequestsTo *redir = [QCloudWebsiteRedirectAllRequestsTo new];
redir.protocol = derPro;
// Specify the target protocol for redirecting all requests; only `https` can be used
config.redirectAllRequestsTo = redir;
// Configuration for a single redirect rule
QCloudWebsiteRoutingRule *rule = [QCloudWebsiteRoutingRule new];
// Conditions for the redirect rule
QCloudWebsiteCondition *contition = [QCloudWebsiteCondition new];
contition.httpErrorCodeReturnedEquals = errorCode;
rule.condition = contition;
// Specific redirect destination in the redirect rule
QCloudWebsiteRedirect *webRe = [QCloudWebsiteRedirect new];
webRe.protocol = derPro;
// Specify the object key to redirect to by replacing the prefix matched in the original request.
// This can be set only if `Condition` is set to `KeyPrefixEquals`
webRe.replaceKeyPrefixWith = replaceKeyPrefixWith;
rule.redirect = webRe;
QCloudWebsiteRoutingRules *routingRules = [QCloudWebsiteRoutingRules new];
routingRules.routingRule = @[rule];
// Redirect rule configuration. Up to 100 `routingRules` can be set
config.rules = routingRules;
putReq.websiteConfiguration = config;
[putReq setFinishBlock:^(id outputObject, NSError *error) {
// `outputObject` contains all the HTTP response headers
NSDictionary* info = (NSDictionary *) outputObject;
}];
[[QCloudCOSXMLService defaultCOSXML] PutBucketWebsite:putReq];
Note:
For the complete sample, go to GitHub.
Swift
let req = QCloudPutBucketWebsiteRequest.init();
// Bucket name in the format: `BucketName-APPID`
req.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 for the index document. For example, if `index.html` is specified, then when the root directory of the bucket is accessed,
// `index.html` will automatically be returned; when the `article/` directory is accessed, `article/index.html` will automatically be returned
indexDocument.suffix = indexDocumentSuffix;
// Configure the index document
config.indexDocument = indexDocument;
// Configure the error document
let errDocument = QCloudWebisteErrorDocument.init();
errDocument.key = errorDocKey;
// Specify the object key for the general error document. When an error occurs and no error code in the redirect rule is hit, this object key will be returned
config.errorDocument = errDocument;
// Configuration for redirecting all requests
let redir = QCloudWebsiteRedirectAllRequestsTo.init();
// Specify the target protocol for redirecting all requests; only `https` can be used
redir.protocol = "https";
config.redirectAllRequestsTo = redir;
// Configuration for a single redirect rule
let rule = QCloudWebsiteRoutingRule.init();
// Conditions for the redirect rule
let contition = QCloudWebsiteCondition.init();
contition.httpErrorCodeReturnedEquals = Int32(errorCode);
rule.condition = contition;
// Specific redirect destination in the redirect rule
let webRe = QCloudWebsiteRedirect.init();
webRe.protocol = "https";
// Specify the object key to redirect to by replacing the prefix matched in the original request.
// This can be set only if `Condition` is set to `KeyPrefixEquals`
webRe.replaceKeyPrefixWith = replaceKeyPrefixWith;
rule.redirect = webRe;
let routingRules = QCloudWebsiteRoutingRules.init();
routingRules.routingRule = [rule];
// Redirect rule configuration. Up to 100 `routingRules` can be set
config.rules = routingRules;
req.websiteConfiguration = config;
req.finishBlock = {(result,error) in
if let result = result {
// “result” contains response headers
} else {
print(error!);
}
}
QCloudCOSXMLService.defaultCOSXML().putBucketWebsite(req);
Note:
For the complete sample, go to GitHub.
This API is used to query the static website configuration associated with a bucket.
Objective-C
QCloudGetBucketWebsiteRequest *getReq = [QCloudGetBucketWebsiteRequest new];
// Bucket name in the format: `BucketName-APPID`
getReq.bucket = @"examplebucket-1250000000";
[getReq setFinishBlock:^(QCloudWebsiteConfiguration * result,
NSError * error) {
// Redirect rule configuration. Up to 100 `routingRules` can be set
QCloudWebsiteRoutingRules *rules =result.rules;
// Index document
QCloudWebsiteIndexDocument *indexDocument = result.indexDocument;
// Error document
QCloudWebisteErrorDocument *errorDocument = result.errorDocument;
// Redirect all requests
QCloudWebsiteRedirectAllRequestsTo *redirectAllRequestsTo = result.redirectAllRequestsTo;
}];
[[QCloudCOSXMLService defaultCOSXML] GetBucketWebsite:getReq];
Note:
For the complete sample, go to GitHub.
Swift
let req = QCloudGetBucketWebsiteRequest.init();
// Bucket name in the format: `BucketName-APPID`
req.bucket = "examplebucket-1250000000";
req.setFinish {(result,error) in
if let result = result {
let rules = result.rules
} else {
print(error!);
}
}
QCloudCOSXMLService.defaultCOSXML().getBucketWebsite(req);
Note:
For the complete sample, go to GitHub.
This API is used to delete the static website configuration from a bucket.
Objective-C
QCloudDeleteBucketWebsiteRequest *delReq = [QCloudDeleteBucketWebsiteRequest new];
// Bucket name in the format: `BucketName-APPID`
delReq.bucket = @"examplebucket-1250000000";
[delReq setFinishBlock:^(id outputObject, NSError *error) {
// `outputObject` contains all the HTTP response headers
NSDictionary* info = (NSDictionary *) outputObject;
}];
[[QCloudCOSXMLService defaultCOSXML] DeleteBucketWebsite:delReq];
Note:
For the complete sample, go to GitHub.
Swift
let delReq = QCloudDeleteBucketWebsiteRequest.init();
// Bucket name in the format: `BucketName-APPID`
delReq.bucket = "examplebucket-1250000000";
delReq.finishBlock = {(result,error) in
if let result = result {
// “result” contains response headers
} else {
print(error!);
}
}
QCloudCOSXMLService.defaultCOSXML().deleteBucketWebsite(delReq);
Note:
For the complete sample, go to GitHub.
Was this page helpful?