• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Java SDK)
  4. Access Logging
  5. Enabling Bucket Logging

Enabling Bucket Logging

You can call ObsClient.setBucketLoggingConfiguration 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.

NOTE:

If the bucket is in the OBS Warm or Cold storage class, it cannot be used as the target bucket.

Directly Enabling Bucket Logging

Sample code:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

BucketLoggingConfiguration config = new BucketLoggingConfiguration();

config.setTargetBucketName("targetbucketname");
config.setLogfilePrefix("targetprefix");

// Configure the bucket logging. When the third parameter is set to true, the access permissions of the log delivery group on the target bucket is automatically configured.
obsClient.setBucketLoggingConfiguration("bucketname", config, true);

// Close obsClient.
obsClient.close();
NOTE:

The third parameter in ObsClient.setBucketLoggingConfiguration specifies whether to automatically grant the WRITE and READ_ACP permissions to the log delivery group by OBS Java SDK.

Enabling Bucket Logging by Steps

Sample code:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

String targetBucket = "targetbucketname"; 

// Grant the WRITE and READ_ACP permissions on the target bucket to the log delivery group.
obsClient.setBucketAcl(targetBucket, AccessControlList.REST_CANNED_LOG_DELIVERY_WRITE);

// Configure logging. 
BucketLoggingConfiguration config = new BucketLoggingConfiguration();
config.setTargetBucketName(targetBucket);
config.setLogfilePrefix("prefix");

obsClient.setBucketLoggingConfiguration("bucketname", config);

// Close obsClient.
obsClient.close();

Setting ACLs for Objects to Be Logged

Sample code:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

String targetBucket = "targetbucketname"; 

// Configure logging. 
BucketLoggingConfiguration config = new BucketLoggingConfiguration();
config.setTargetBucketName(targetBucket);
config.setLogfilePrefix("prefix");

// Grant the READ permission on the objects to be logged to all users.
GrantAndPermission grant1 = new GrantAndPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_READ);
// Grant the FULL_CONTROL permission on logs to the authorized users.
GrantAndPermission grant2 = new GrantAndPermission(GroupGrantee.AUTHENTICATED_USERS, Permission.PERMISSION_FULL_CONTROL);
config.setTargetGrants(new GrantAndPermission[]{grant1, grant2});

obsClient.setBucketLoggingConfiguration("bucketname", config, true);

// Close obsClient.
obsClient.close();