Static Website

Last updated: 2020-03-20 09:26:39

PDF

Note

This document provides an overview of API on static websites and sample SDK code.

API Operation name pedagogical operation
PUT Bucket website Setting up a Static Website Set Bucket's static website configuration
GET Bucket website Query static site configuration Query Bucket's static website configuration
DELETE Bucket website Delete static site configuration Delete Bucket's static website configuration

Setting up a Static Website

Function description

PUT Bucket website is used to configure a static website for Bucket.

Method prototype

When you start using COS, you need to create a Bucket under the specified account to facilitate the use and management of objects, and specify the region to which Bucket belongs. The user who created Bucket becomes the holder of Bucket by default. If Access and Permission are not specified when Bucket is created, it defaults to Permission, a private read-write (private). The specific steps are as follows:

  1. Instantiate QCloudPutBucketWebsiteRequest
  2. Call the PutBucketWebsite method in the QCloudCOSXMLService object to make the request.
  3. Get the specific content from the outputObject in the finishBlock of the callback.

Request Sample

NSString *bucket = @"examplebucket-1250000000";
   NSString * regionName = @"ap-chengdu";

   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];
   indexDocument.suffix = indexDocumentSuffix;
   config.indexDocument = indexDocument;

   QCloudWebisteErrorDocument *errDocument = [QCloudWebisteErrorDocument new];
   errDocument.key = errorDocKey;
   config.errorDocument = errDocument;


   QCloudWebsiteRedirectAllRequestsTo *redir = [QCloudWebsiteRedirectAllRequestsTo new];
   redir.protocol  = @"https";
   config.redirectAllRequestsTo = redir;


   QCloudWebsiteRoutingRule *rule = [QCloudWebsiteRoutingRule new];
   QCloudWebsiteCondition *contition = [QCloudWebsiteCondition new];
   contition.httpErrorCodeReturnedEquals = errorCode;
   rule.condition = contition;

   QCloudWebsiteRedirect *webRe = [QCloudWebsiteRedirect new];
   webRe.protocol = @"https";
   webRe.replaceKeyPrefixWith = replaceKeyPrefixWith;
   rule.redirect = webRe;

   QCloudWebsiteRoutingRules *routingRules = [QCloudWebsiteRoutingRules new];
   routingRules.routingRule = @[rule];
   config.rules = routingRules;
   putReq.websiteConfiguration  = config;


   [putReq setFinishBlock:^(id outputObject, NSError *error) {

   }];

   [[QCloudCOSXMLService defaultCOSXML] PutBucketWebsite:putReq];

Swift code example:

let req = QCloudPutBucketWebsiteRequest.init();
req.bucket = "examplebucket-1250000000";

let indexDocumentSuffix = "index.html";
let errorDocKey = "error.html";
let derPro = "https";
let errorCode = 451;
let replaceKeyPrefixWith = "404.html";

let config = QCloudWebsiteConfiguration.init();

let indexDocument = QCloudWebsiteIndexDocument.init();
indexDocument.suffix = indexDocumentSuffix;
config.indexDocument = indexDocument;

let errDocument = QCloudWebisteErrorDocument.init();
errDocument.key = errorDocKey;
config.errorDocument = errDocument;


let redir = QCloudWebsiteRedirectAllRequestsTo.init();
redir.protocol  = "https";
config.redirectAllRequestsTo = redir;


let rule = QCloudWebsiteRoutingRule.init();
let contition = QCloudWebsiteCondition.init();
contition.httpErrorCodeReturnedEquals = Int32(errorCode);
rule.condition = contition;

let webRe = QCloudWebsiteRedirect.init();
webRe.protocol = "https";
webRe.replaceKeyPrefixWith = replaceKeyPrefixWith;
rule.redirect = webRe;

let routingRules = QCloudWebsiteRoutingRules.init();
routingRules.routingRule = [rule];
config.rules = routingRules;
req.websiteConfiguration  = config;

req.finishBlock = {(result,error) in

    if error != nil{
        print(error!);
    }else{
        print( result!);
    }

}
QCloudCOSXMLService.defaultCOSXML().putBucketWebsite(req);

Parameter description

QCloudPutBucketWebsiteRequest request parameter description

Parameter name Description Type Required/Optional
Bucket Set the Bucket of the static website in BucketName-APPID format. For more information, please see Naming convention NSString * Yes
WebsiteConfiguration Set configuration information for static websites QCloudWebsiteConfiguration * Yes

QCloudWebsiteConfiguration parameter description

Parameter name Description Type Required/Optional
Rules Set redirection rules. Set a maximum of 100 RoutingRule QCloudWebsiteRoutingRules * Yes
IndexDocument Index document QCloudWebsiteIndexDocument * Yes
ErrorDocument Error document QCloudWebisteErrorDocument * No
RedirectAllRequestsTo Redirect all requests QCloudWebsiteRedirectAllRequestsTo * No

QCloudWebsiteRoutingRules parameter description

Parameter name Description Type Required/Optional
RoutingRule Set single redirection rules, including prefix matching redirection and error code redirection NSArray < QCloudWebsiteRoutingRule * > * No

QCloudWebsiteIndexDocument parameter description

Parameter name Description Type Required/Optional
Suffix Specify an indexed document NSString * Yes

QCloudWebisteErrorDocument parameter description

Parameter name Description Type Required/Optional
Key Specify generic error return NSString * No

QCloudWebsiteRedirectAllRequestsTo parameter description

Parameter name Description Type Required/Optional
Protocol Protocol who specifies the site-wide redirection can only be set to https NSString * No

Returns the error code description

When the SDK request fails, the error returned is not empty and includes the error code, error description, and other necessary debugging information to help developers quickly solve the problem.

Error codes (encapsulated in the returned error) include those returned by the device due to network problems and those returned by COS.

  • For Device's own error codes due to network reasons, they are all negative and four digits, for example,-1001. This kind of error code is defined by Apple. For more information, please see the definition in the NSURLError.h header file in the Foundation framework, or Apple official documentation Description.
  • The error code returned by COS is based on the status code of HTTP, that is, 404503. For such error codes, see API Error Codes The document seeks a solution.
  • For the error codes customized by SDK, they are all 5 digits and all are positive numbers, such as 10000, 20000, and so on. For such error codes, see SDK Error Codes The document seeks a solution.

Query static site configuration

Function description

GET Bucket website is used to query static website configuration information with Bucket and Associate.

Method prototype

When you start using COS, you need to create a Bucket under the specified account to facilitate the use and management of objects, and specify the region to which Bucket belongs. The user who created Bucket becomes the holder of Bucket by default. If Access and Permission are not specified when Bucket is created, it defaults to Permission, a private read-write (private). The specific steps are as follows:

  1. Instantiate QCloudGetBucketWebsiteRequest
  2. Call the GetBucketWebsite method in the QCloudCOSXMLService object to make the request.
  3. Get the specific content from the result in the finishBlock of the callback.

Request Sample

QCloudGetBucketWebsiteRequest *getReq = [QCloudGetBucketWebsiteRequest new];
getReq.bucket = @"examplebucket-1250000000";
[getReq setFinishBlock:^(QCloudWebsiteConfiguration *  result, NSError * error) {

}];
[[QCloudCOSXMLService defaultCOSXML] GetBucketWebsite:getReq];

Swift code example:

let req = QCloudGetBucketWebsiteRequest.init();
req.bucket = "examplebucket-1250000000";

req.setFinish {(result,error) in

    if error != nil{
        print(error!);
    }else{
        print( result!);
    }
}
QCloudCOSXMLService.defaultCOSXML().getBucketWebsite(req);

Parameter description

QCloudGetBucketWebsiteRequest request parameter description

Parameter name Description Type Required/Optional
Bucket Query Bucket configured for static website. The format is BucketName-APPID,. For more information, please see Naming convention NSString * Yes

Return result description

QCloudWebsiteConfiguration parameter description

Parameter name Description Type
Rules Set redirection rules. Set a maximum of 100 RoutingRule QCloudWebsiteRoutingRules *
ErrorDocument Error document QCloudWebisteErrorDocument *
RedirectAllRequestsTo Redirect all requests QCloudWebsiteRedirectAllRequestsTo *

Returns the error code description

When the SDK request fails, the error returned is not empty and includes the error code, error description, and other necessary debugging information to help developers quickly solve the problem.

Error codes (encapsulated in the returned error) include those returned by the device due to network problems and those returned by COS.

  • For Device's own error codes due to network reasons, they are all negative and four digits, for example,-1001. This kind of error code is defined by Apple. For more information, please see the definition in the NSURLError.h header file in the Foundation framework, or Apple official documentation Description.
  • The error code returned by COS is based on the status code of HTTP, that is, 404503. For such error codes, see API Error Codes The document seeks a solution.
  • For the error codes customized by SDK, they are all 5 digits and all are positive numbers, such as 10000, 20000, and so on. For such error codes, see SDK Error Codes The document seeks a solution.

Delete static site configuration

Function description

DELETE Bucket website is used to delete the static website configuration in Bucket.

Method prototype

When you start using COS, you need to create a Bucket under the specified account to facilitate the use and management of objects, and specify the region to which Bucket belongs. The user who created Bucket becomes the holder of Bucket by default. If Access and Permission are not specified when Bucket is created, it defaults to Permission, a private read-write (private). The specific steps are as follows:

  1. Instantiate QCloudDeleteBucketWebsiteRequest
  2. Call the DeleteBucketWebsite method in the QCloudCOSXMLService object to make the request.
  3. Get the specific content from the outputObject in the finishBlock of the callback.

Request Sample

QCloudDeleteBucketWebsiteRequest *delReq = [QCloudDeleteBucketWebsiteRequest new];
delReq.bucket = "examplebucket-1250000000";
[delReq setFinishBlock:^(id outputObject, NSError *error) {

}];
[[QCloudCOSXMLService defaultCOSXML] DeleteBucketWebsite:delReq];

Swift code example:

let delReq = QCloudDeleteBucketWebsiteRequest.init();
delReq.bucket = "examplebucket-1250000000";
delReq.finishBlock = {(result,error) in

    if error != nil{
        print(error!);
    }else{
        print( result!);
    }
}

QCloudCOSXMLService.defaultCOSXML().deleteBucketWebsite(delReq);

Parameter description

QCloudDeleteBucketWebsiteRequest request parameter description

Parameter name Description Type Required/Optional
Bucket Bucket, whose static website configuration is deleted, is in BucketName-APPID format. For more information, please see Naming convention NSString * Yes

Returns the error code description

When the SDK request fails, the error returned is not empty and includes the error code, error description, and other necessary debugging information to help developers quickly solve the problem.

Error codes (encapsulated in the returned error) include those returned by the device due to network problems and those returned by COS.

  • For Device's own error codes due to network reasons, they are all negative and four digits, for example,-1001. This kind of error code is defined by Apple. For more information, please see the definition in the NSURLError.h header file in the Foundation framework, or Apple official documentation Description.
  • The error code returned by COS is based on the status code of HTTP, that is, 404503. For such error codes, see API Error Codes The document seeks a solution.
  • For the error codes customized by SDK, they are all 5 digits and all are positive numbers, such as 10000, 20000, and so on. For such error codes, see SDK Error Codes The document seeks a solution.