更新时间:2023-11-08 gmt 08:00

设置托管配置-九游平台

您可以通过setbucketwebsite设置桶的托管配置。

配置默认九游平台主页和错误页面

以下代码展示了如何配置默认九游平台主页和错误页面:

static obsclient *client;
nsstring *endpoint = @"your-endpoint";
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量accesskeyid和secretaccesskey。
// 您可以登录访问管理控制台获取访问密钥ak/sk,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
char* ak_env = getenv("accesskeyid");
char* sk_env = getenv("secretaccesskey");
nsstring *ak = [nsstring stringwithutf8string:ak_env];
nsstring *sk = [nsstring stringwithutf8string:sk_env];
    
// 初始化身份验证
obsstaticcredentialprovider *credentialprovider = [[obsstaticcredentialprovider alloc] initwithaccesskey:ak secretkey:sk];
    
//初始化服务配置
obsserviceconfiguration *conf = [[obsserviceconfiguration alloc] initwithurlstring:endpoint credentialprovider:credentialprovider];
    
// 初始化client
client = [[obsclient alloc] initwithconfiguration:conf];
    
    
obswebsiteconfcustom* redirectcustom = [obswebsiteconfcustom new];
    
// 配置默认九游平台主页
obswebsiteconfcustomindexdocument* indexdocument = [[obswebsiteconfcustomindexdocument alloc]initwithsuffix:@"index.html"];
// 配置默认错误界面
obswebsiteconfcustomerrordocument* errordocument = [[obswebsiteconfcustomerrordocument alloc]initwithkey:@"error.html"];
    
obswebsiteconfcustomroutingrule* redirectrule = [obswebsiteconfcustomroutingrule new];
    
obswebsiteconfcustomcondition* condition = [obswebsiteconfcustomcondition new];
condition.keyprefixequals = @"docs/";
//    condition.httperrorcodereturnedequals = @"404";
    
//  配置重定向规则
obswebsiteconfcustomredirect* redirect = [obswebsiteconfcustomredirect new];
redirect.replacekeyprefixwith = @"documents/";
redirect.protocol = @"http";
redirect.hostname = @"url";
//    redirect.replacekeywith = @"error.html";
redirect.httpredirectcode = @"301";
    
    
redirectrule.condition = condition;
redirectrule.redirect = redirect;
    
[redirectcustom.indexdocumentlist addobject:indexdocument];
[redirectcustom.errordocumentlist addobject:errordocument];
[redirectcustom.routingruleslist addobject:redirectrule];
    
obssetbucketwebsiterequest *request = [[obssetbucketwebsiterequest alloc]initwithbucketname:@"bucketname" configuration:redirectcustom];
    
[client setbucketwebsite:request completionhandler:^(obssetbucketwebsiteresponse *response, nserror *error){
    nslog(@"%@",response);
 }];

配置所有请求重定向

以下代码展示了如何配置所有请求重定向:

static obsclient *client;
nsstring *endpoint = @"your-endpoint";
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量accesskeyid和secretaccesskey。
// 您可以登录访问管理控制台获取访问密钥ak/sk,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
char* ak_env = getenv("accesskeyid");
char* sk_env = getenv("secretaccesskey");
nsstring *ak = [nsstring stringwithutf8string:ak_env];
nsstring *sk = [nsstring stringwithutf8string:sk_env];
    
// 初始化身份验证
obsstaticcredentialprovider *credentialprovider = [[obsstaticcredentialprovider alloc] initwithaccesskey:ak secretkey:sk];
    
//初始化服务配置
obsserviceconfiguration *conf = [[obsserviceconfiguration alloc] initwithurlstring:endpoint credentialprovider:credentialprovider];
    
// 初始化client
client = [[obsclient alloc] initwithconfiguration:conf];
    
// 配置所有请求重定向
obswebsiteconfredirectall* redirectall = [obswebsiteconfredirectall new];
redirectall.hostname = @"url";
obssetbucketwebsiterequest *request = [[obssetbucketwebsiterequest alloc]initwithbucketname:@"bucketname" configuration:redirectall];
    
 [client setbucketwebsite:request completionhandler:^(obssetbucketwebsiteresponse *response, nserror *error){
     nslog(@"%@",response);
 }];

相关文档

网站地图