• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Node.js 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.
NOTE:
  • The source bucket and target bucket of logging must be in the same region.
  • If the bucket is in the OBS Warm or Cold storage class, it cannot be used as the target bucket.

Enabling Bucket Logging Step by Step

Sample code:

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

var targetBucketName = 'targetbucketname';
// Grant the WRITE and READ_ACP permissions on the target bucket to the log delivery group.
obsClient.setBucketAcl({
       Bucket : targetBucketName,
       ACL : obsClient.enums.AclLogDeliveryWrite
}, (err, result) => {
       if(err){
              console.log('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
              // Configure logging for the bucket.
              obsClient.setBucketLoggingConfiguration({
                     Bucket:'bucketname',
                     LoggingEnabled:{
                           TargetBucket: targetBucketName,
                           TargetPrefix: 'prefix'
                     }
              }, (err, result) => {
                     if(err){
                           console.log('Error-->' + err);
                     }else{
                           console.log('Status-->' + result.CommonMsg.Status);
                     }
              });
       }
});
NOTE:

Use the LoggingEnabled parameter to configure logging for a bucket.

Setting ACLs for Objects to Be Logged

Sample code:

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

var targetBucketName = 'targetbucketname';
// Grant the WRITE and READ_ACP permissions on the target bucket to the log delivery group.
obsClient.setBucketAcl({
       Bucket : targetBucketName,
       ACL : obsClient.enums.AclLogDeliveryWrite
}, (err, result) => {
       if(err){
              console.log('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
              // Configure logging for the bucket.
              obsClient.setBucketLoggingConfiguration({
                     Bucket:'bucketname',
                     LoggingEnabled:{
                           TargetBucket: targetBucketName,
                           TargetPrefix: 'prefix',
                           TargetGrants:{
                                  Grant:[
// Grant the READ permission to all users.
                                      {Grantee:{Type:'Group',URI:obsClient.enums.GroupAllUsers},Permission:obsClient.enums.PermissionRead},
// Grant the FULL_CONTROL permission on logs to the authorized users.
                                      {Grantee:{Type:'Group',URI:obsClient.enums.GroupAuthenticatedUsers},Permission:obsClient.enums.PermissionFullControl}
                                  ]
                           }
                     }
              }, (err, result) => {
                     if(err){
                           console.log('Error-->' + err);
                     }else{
                           console.log('Status-->' + result.CommonMsg.Status);
                     }
              });
       }
});