Log storage (Python SDK V2)

更新时间:
复制 MD 格式

Accessing OSS generates numerous access logs. You can use the log storage feature to save these logs to a specified bucket. The logs are stored as log files that are generated hourly and follow a fixed naming convention.

Precautions

To enable log storage for a bucket, you must have the oss:PutBucketLogging permission. To view the log storage configuration, you must have the oss:GetBucketLogging permission. To disable log storage, you must have the oss:DeleteBucketLogging permission. For more information, see Grant custom access policies to a Resource Access Management (RAM) user.

Method definitions

Enable log storage

put_bucket_logging(request: PutBucketLoggingRequest, **kwargs) → PutBucketLoggingResult

View the log storage configuration

get_bucket_logging(request: GetBucketLoggingRequest, **kwargs) → GetBucketLoggingResult

Disable log storage

delete_bucket_logging(request: DeleteBucketLoggingRequest, **kwargs) → DeleteBucketLoggingResult

Request parameters

Parameter name

Type

Description

request

PutBucketLoggingRequest

The request parameters. For more information, see PutBucketLoggingRequest

GetBucketLoggingRequest

The request parameters. For more information, see GetBucketLoggingRequest

DeleteBucketLoggingRequest

The request parameters. For more information, see DeleteBucketLoggingRequest

Return values

Type

Description

PutBucketLoggingResult

The return value. For more information, see PutBucketLoggingResult

GetBucketLoggingResult

The return value. For more information, see GetBucketLoggingResult

DeleteBucketLoggingResult

The return value. For more information, see DeleteBucketLoggingResult

For the complete definition of the operation to enable log storage, see put_bucket_logging.

For the complete definition of the operation to view the log storage configuration, see get_bucket_logging.

For the complete definition of the operation to disable log storage, see delete_bucket_logging

Code examples

Enable log storage

The following code shows how to enable the log storage feature.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors

parser = argparse.ArgumentParser(description="vector put bucket logging sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The endpoint to access OSS')
parser.add_argument('--account_id', help='The account ID.', required=True)
parser.add_argument('--target_bucket', help='The name of the target bucket.', required=True)

def main():
    args = parser.parse_args()

    # Load credentials from environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the SDK's default configuration
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    cfg.account_id = args.account_id
    cfg.use_internal_endpoint = True  # Set to False to use the public network endpoint.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    vector_client = oss_vectors.Client(cfg)

    result = vector_client.put_bucket_logging(oss_vectors.models.PutBucketLoggingRequest(
        bucket=args.bucket,
        bucket_logging_status=oss_vectors.models.BucketLoggingStatus(
            logging_enabled=oss_vectors.models.LoggingEnabled(
                target_bucket=args.target_bucket,
                target_prefix='log-prefix',
                logging_role='AliyunOSSLoggingDefaultRole'
            )
        )
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
    )

if __name__ == "__main__":
    main()

View the log storage configuration

The following code shows how to view the log storage configuration.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors

parser = argparse.ArgumentParser(description="vector get bucket logging sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The endpoint to access OSS')
parser.add_argument('--account_id', help='The account ID.', required=True)

def main():
    args = parser.parse_args()

    # Load credentials from environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the SDK's default configuration
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    cfg.account_id = args.account_id
    cfg.use_internal_endpoint = True  # Set to False to use the public network endpoint.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    vector_client = oss_vectors.Client(cfg)

    result = vector_client.get_bucket_logging(oss_vectors.models.GetBucketLoggingRequest(
        bucket=args.bucket,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          )

    if result.bucket_logging_status:
        print(f'logging enabled: target_bucket={result.bucket_logging_status.logging_enabled.target_bucket}, '
              f'target_prefix={result.bucket_logging_status.logging_enabled.target_prefix}')


if __name__ == "__main__":
    main()

Disable log storage

The following code shows how to disable the log storage feature.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors

parser = argparse.ArgumentParser(description="vector delete bucket logging sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The endpoint to access OSS')
parser.add_argument('--account_id', help='The account ID.', required=True)

def main():
    args = parser.parse_args()

    # Load credentials from environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the SDK's default configuration
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    cfg.account_id = args.account_id
    cfg.use_internal_endpoint = True  # Set to False to use the public network endpoint.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    vector_client = oss_vectors.Client(cfg)

    result = vector_client.delete_bucket_logging(oss_vectors.models.DeleteBucketLoggingRequest(
        bucket=args.bucket,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
    )

if __name__ == "__main__":
    main()

References

  • For complete sample code that shows how to enable log storage for a vector bucket, see put_bucket_logging.py.

  • For complete sample code that shows how to retrieve the log storage configuration of a vector bucket, see get_bucket_logging.py.

  • For complete sample code that shows how to delete the log storage configuration for a vector bucket, see delete_bucket_logging.py.