云审计服务 CTS-创建追踪器:Python

时间:2023-12-22 17:31:29

Python

  • 管理类追踪器创建样例。
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    # coding: utf-8
    
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkcts.v3.region.cts_region import CtsRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkcts.v3 import *
    
    if __name__ == "__main__":
        # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak = os.getenv("CLOUD_SDK_AK")
        sk = os.getenv("CLOUD_SDK_SK")
    
        credentials = BasicCredentials(ak, sk) \
    
        client = CtsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(CtsRegion.value_of("cn-north-4")) \
            .build()
    
        try:
            request = CreateTrackerRequest()
            obsInfobody = TrackerObsInfo(
                bucket_name="test-data-tracker",
                file_prefix_name="11",
                is_obs_created=False
            )
            request.body = CreateTrackerRequestBody(
                is_support_validate=True,
                kms_id="13a4207c-7abe-4b68-8510-16b84c3b5504",
                is_support_trace_files_encryption=True,
                obs_info=obsInfobody,
                is_lts_enabled=True,
                tracker_name="system",
                tracker_type="system"
            )
            response = client.create_tracker(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 数据类追踪器创建样例。
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    # coding: utf-8
    
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkcts.v3.region.cts_region import CtsRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkcts.v3 import *
    
    if __name__ == "__main__":
        # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak = os.getenv("CLOUD_SDK_AK")
        sk = os.getenv("CLOUD_SDK_SK")
    
        credentials = BasicCredentials(ak, sk) \
    
        client = CtsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(CtsRegion.value_of("cn-north-4")) \
            .build()
    
        try:
            request = CreateTrackerRequest()
            listDataEventDataBucket = [
                "READ",
                "WRITE"
            ]
            dataBucketbody = DataBucket(
                data_bucket_name="cstest0423",
                data_event=listDataEventDataBucket
            )
            obsInfobody = TrackerObsInfo(
                bucket_name="saveTraceBucket",
                file_prefix_name="11",
                is_obs_created=False,
                bucket_lifecycle=30
            )
            request.body = CreateTrackerRequestBody(
                data_bucket=dataBucketbody,
                obs_info=obsInfobody,
                is_lts_enabled=True,
                tracker_name="data-tracker-name",
                tracker_type="data"
            )
            response = client.create_tracker(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
support.huaweicloud.com/api-cts/cts_api_0321.html