• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (PHP 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 by Steps

Sample code:

// Import the third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
       'key' => '*** Provide your Access Key ***',
       'secret' => '*** Provide your Secret Key ***',
       'endpoint' => 'https://yourdomainname'
] );

$targetBucketName = 'targetbucketname';
// Grant the WRITE and READ_ACP permissions on the target bucket to the log delivery group.
$resp = $obsClient->setBucketAcl( [ 
       'Bucket' => $targetBucketName,
       'ACL' => ObsClient::AclLogDeliveryWrite
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );

// Configure logging for the bucket.
$resp = $obsClient->setBucketLoggingConfiguration( [
              'Bucket' => 'bucketname',
              'LoggingEnabled' => [                           
                     'TargetBucket' => $targetBucketName,
                     'TargetPrefix' => 'prefix'                     
              ]
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );
NOTE:

Use the LoggingEnabled parameter to configure logging for a bucket.

Setting ACLs for Objects to Be Logged

Sample code:

// Import the third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
       'key' => '*** Provide your Access Key ***',
       'secret' => '*** Provide your Secret Key ***',
       'endpoint' => 'https://yourdomainname'
] );

$targetBucketName = 'targetbucketname';
// Grant the WRITE and READ_ACP permissions on the target bucket to the log delivery group.
$resp = $obsClient->setBucketAcl ( [ 
       'Bucket' => $targetBucketName,
       'ACL' => ObsClient::AclLogDeliveryWrite 
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );

// Configure logging for the bucket.
$resp = $obsClient->setBucketLoggingConfiguration ( [ 
       'Bucket' => 'bucketname',
       'LoggingEnabled' => [ 
              'TargetBucket' => $targetBucketName,
              'TargetPrefix' => 'prefix',
              'TargetGrants' => [
                           // Grant the READ permission on the objects to be logged to all users.
                           [ 
                                  'Grantee' => [ 
                                                'URI' => ObsClient::GroupAllUsers,
                                                'Type' => 'Group' 
                                  ],
                                  'Permission' => ObsClient::PermissionRead
                           ],
                           // Grant the FULL_CONTROL permission on logs to the authorized users.
                           [ 
                                  'Grantee' => [ 
                                                'URI' => ObsClient::GroupAuthenticatedUsers,
                                                'Type' => 'Group' 
                                  ],
                                  'Permission' => ObsClient::PermissionFullControl
                           ] 
              ] 
       ] 
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );