API | 操作名 | 操作描述 |
设置静态网站 | 设置存储桶的静态网站配置 | |
查询静态网站配置 | 查询存储桶的静态网站配置 | |
删除静态网站配置 | 删除存储桶的静态网站配置 |
// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.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];// 指定索引文档的对象键后缀。例如指定为index.html,那么当访问到存储桶的根目录时,会自动返回// index.html 的内容,或者当访问到article/目录时,会自动返回 article/index.html的内容indexDocument.suffix = indexDocumentSuffix;// 索引文档配置config.indexDocument = indexDocument;// 错误文档配置QCloudWebisteErrorDocument *errDocument = [QCloudWebisteErrorDocument new];errDocument.key = errorDocKey;// 指定通用错误文档的对象键,当发生错误且未命中重定向规则中的错误码重定向时,将返回该对象键的内容config.errorDocument = errDocument;// 重定向所有请求配置QCloudWebsiteRedirectAllRequestsTo *redir = [QCloudWebsiteRedirectAllRequestsTo new];redir.protocol = derPro;// 指定重定向所有请求的目标协议,只能设置为 httpsconfig.redirectAllRequestsTo = redir;// 单条重定向规则配置QCloudWebsiteRoutingRule *rule = [QCloudWebsiteRoutingRule new];// 重定向规则的条件配置QCloudWebsiteCondition *contition = [QCloudWebsiteCondition new];contition.httpErrorCodeReturnedEquals = errorCode;rule.condition = contition;// 重定向规则的具体重定向目标配置QCloudWebsiteRedirect *webRe = [QCloudWebsiteRedirect new];webRe.protocol = derPro;// 指定重定向规则的具体重定向目标的对象键,替换方式为替换原始请求中所匹配到的前缀部分,// 仅可在 Condition 为 KeyPrefixEquals 时设置webRe.replaceKeyPrefixWith = replaceKeyPrefixWith;rule.redirect = webRe;QCloudWebsiteRoutingRules *routingRules = [QCloudWebsiteRoutingRules new];routingRules.routingRule = @[rule];// 重定向规则配置,最多设置100条 RoutingRuleconfig.rules = routingRules;putReq.websiteConfiguration = config;[putReq setFinishBlock:^(id outputObject, NSError *error) {// outputObject 包含所有的响应 http 头部NSDictionary* info = (NSDictionary *) outputObject;}];[[QCloudCOSXMLService defaultCOSXML] PutBucketWebsite:putReq];
let req = QCloudPutBucketWebsiteRequest.init();// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.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();// 指定索引文档的对象键后缀。例如指定为index.html,那么当访问到存储桶的根目录时,会自动返回// index.html 的内容,或者当访问到article/目录时,会自动返回 article/index.html的内容indexDocument.suffix = indexDocumentSuffix;// 索引文档配置config.indexDocument = indexDocument;// 错误文档配置let errDocument = QCloudWebisteErrorDocument.init();errDocument.key = errorDocKey;// 指定通用错误文档的对象键,当发生错误且未命中重定向规则中的错误码重定向时,将返回该对象键的内容config.errorDocument = errDocument;// 重定向所有请求配置let redir = QCloudWebsiteRedirectAllRequestsTo.init();// 指定重定向所有请求的目标协议,只能设置为 httpsredir.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";// 指定重定向规则的具体重定向目标的对象键,替换方式为替换原始请求中所匹配到的前缀部分,// 仅可在 Condition 为 KeyPrefixEquals 时设置webRe.replaceKeyPrefixWith = replaceKeyPrefixWith;rule.redirect = webRe;let routingRules = QCloudWebsiteRoutingRules.init();routingRules.routingRule = [rule];// 重定向规则配置,最多设置100条 RoutingRuleconfig.rules = routingRules;req.websiteConfiguration = config;req.finishBlock = {(result,error) inif let result = result {// result 包含响应的 header 信息} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().putBucketWebsite(req);
QCloudGetBucketWebsiteRequest *getReq = [QCloudGetBucketWebsiteRequest new];// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucketgetReq.bucket = @"examplebucket-1250000000";[getReq setFinishBlock:^(QCloudWebsiteConfiguration * result,NSError * error) {// 设置重定向规则,最多设置100条RoutingRuleQCloudWebsiteRoutingRules *rules =result.rules;// 索引文档QCloudWebsiteIndexDocument *indexDocument = result.indexDocument;// 错误文档QCloudWebisteErrorDocument *errorDocument = result.errorDocument;// 重定向所有请求QCloudWebsiteRedirectAllRequestsTo *redirectAllRequestsTo = result.redirectAllRequestsTo;}];[[QCloudCOSXMLService defaultCOSXML] GetBucketWebsite:getReq];
let req = QCloudGetBucketWebsiteRequest.init();// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.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);
QCloudDeleteBucketWebsiteRequest *delReq = [QCloudDeleteBucketWebsiteRequest new];// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucketdelReq.bucket = @"examplebucket-1250000000";[delReq setFinishBlock:^(id outputObject, NSError *error) {// outputObject 包含所有的响应 http 头部NSDictionary* info = (NSDictionary *) outputObject;}];[[QCloudCOSXMLService defaultCOSXML] DeleteBucketWebsite:delReq];
let delReq = QCloudDeleteBucketWebsiteRequest.init();// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucketdelReq.bucket = "examplebucket-1250000000";delReq.finishBlock = {(result,error) inif let result = result {// result 包含响应的 header 信息} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().deleteBucketWebsite(delReq);
本页内容是否解决了您的问题?