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

开启桶日志-九游平台

您可以通过setbucketlogging开启桶日志功能。

日志目标桶与源桶必须在同一个区域(region)。

已支持日志目标桶的存储类型:低频访问存储或归档存储或标准存储。

开启桶日志

以下代码展示了如何开启桶日志:

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];
    
//设置桶访问日志
// 第一步 设置目标桶的日志投递组访问权限
obsuser *owner = [[obsuser alloc] initwithid:@"ownerid"];
    
obsaclgranteelogdelivery *grantee = [obsaclgranteelogdelivery new];
obsaclgrant *grant = [[obsaclgrant alloc]initwithgrantee:grantee permission:obsaclfull_control];
    
// 设置目标桶的日志投递组为完全控制权限
obsaclgranteeuser *usergrantee = [[obsaclgranteeuser alloc]initwithid:@"granteeid"];
obsaclgrant *usergrant = [[obsaclgrant alloc]initwithgrantee:usergrantee permission:obsaclfull_control];
    
obsaclgranteeallusers *allusergrantee = [obsaclgranteeallusers new];
obsaclgrant *allusergrant = [[obsaclgrant alloc]initwithgrantee:allusergrantee permission:obsaclfull_control];
    
obsaccesscontrolpolicy *policy = [obsaccesscontrolpolicy new];
policy.owner = owner;
[policy.accesscontrollist addobject:grant];
[policy.accesscontrollist addobject:usergrant];
    
obssetbucketaclwithpolicyrequest *setaclrequest = [[obssetbucketaclwithpolicyrequest alloc]initwithbucketname:@"bucketname" accesscontrolpolicy:policy];
    
[client setbucketacl:setaclrequest completionhandler:^(obssetbucketaclresponse *response, nserror *error){
    nslog(@"%@",response);
}];
    
    
// 第二步 设置桶的日志管理配置
grant = [[obsaclgrant alloc]initwithgrantee:grantee permission:obsaclfull_control];
    
obssetbucketloggingrequest *request = [[obssetbucketloggingrequest alloc]initwithbucketname:@"bucketname"];
    
obsloggingenabled* enableditem = [[obsloggingenabled alloc]initwithtargetbucket:@"bucketname" targetprefix:@"access-log"];
    
[enableditem.targetgrantslist addobject:usergrant];
[enableditem.targetgrantslist addobject:allusergrant];
    
[request.loggingenabledlist addobject:enableditem];
[client setbucketlogging:request completionhandler:^(obssetbucketloggingresponse *response, nserror *error){
    nslog(@"%@",response);
}];

相关文档

网站地图