Enabling Bucket Logging

You can call setBucketLogging to enable bucket logging. by performing the following two steps.

  1. Set the log delivery group's access permissions on the target bucket. (Ensure that the log delivery group has the WRITE and READ_ACP permissions on the bucket.)
  2. Configure logging.

The source bucket and target bucket of logging must be in the same region.

If the bucket is in the OBS Infrequent Access or Archive storage class, it cannot be used as the target bucket.

Enabling Bucket Logging

Sample code:

static OBSClient *client;
NSString *endPoint = @"your-endpoint";
NSString *SK = @"*** Provide your Secret Key ***";
NSString *AK = @"*** Provide your Access Key ***";
    
// Initialize identity authentication.
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];
    
//Initialize service configuration.
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];
    
// Initialize an instance of OBSClient.
client = [[OBSClient alloc] initWithConfiguration:conf];
    
//Set the bucket ACL.
// Step 1: Set the log delivery group's access permissions.
OBSUser *owner = [[OBSUser alloc] initWithID:@"ownerID"];
    
OBSACLGranteeLogDelivery *grantee = [OBSACLGranteeLogDelivery new];
OBSACLGrant *grant = [[OBSACLGrant alloc]initWithGrantee:grantee permission:OBSACLFull_Control];
    
// Set the log delivery group's access permission to FULL_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);
}];
    
    
// Step 2: Set bucket logging.
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);
}];