Manage table buckets (Python SDK V2)

Updated at:

Use Python SDK V2 to manage table buckets, namespaces, and tables, including creation, deletion, queries, encryption, policies, and maintenance configuration.

Notes

  • The sample code uses the region ID cn-hangzhou for the China (Hangzhou) region. Replace this value with the ID of your target region.

  • The sample code reads access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials. Make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set before you run the sample code.

  • Logging operations are not yet wrapped in strongly typed methods. The examples in this topic call them through the generic invoke_operation method. PUT requests must carry a Content-MD5 calculated from the request body.

Table bucket operations

CreateTableBucket

Creates a table bucket and optionally configures server-side encryption.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="create table bucket sample")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The endpoint to access OSS Tables.')
parser.add_argument('--name', help='The name of the table bucket.', required=True)
parser.add_argument('--sse-algorithm', help='The server-side encryption algorithm.')
parser.add_argument('--kms-key-arn', help='The KMS key ARN for encryption.')

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    encryption_configuration = None
    if args.sse_algorithm is not None or args.kms_key_arn is not None:
        encryption_configuration = oss_tables.models.EncryptionConfiguration(
            sse_algorithm=args.sse_algorithm,
            kms_key_arn=args.kms_key_arn,
        )

    result = client.create_table_bucket(oss_tables.models.CreateTableBucketRequest(
        name=args.name,
        encryption_configuration=encryption_configuration,
    ))

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


if __name__ == "__main__":
    main()

DeleteTableBucket

Deletes the specified table bucket. The table bucket must not contain any namespaces.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="delete table bucket sample")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The service endpoint for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.delete_table_bucket(oss_tables.models.DeleteTableBucketRequest(
        table_bucket_arn=args.table_bucket_arn,
    ))

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


if __name__ == "__main__":
    main()

GetTableBucket

Gets the details of a specified table bucket.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="get table bucket sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The access domain name for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.get_table_bucket(oss_tables.models.GetTableBucketRequest(
        table_bucket_arn=args.table_bucket_arn,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' arn: {result.arn},'
          f' name: {result.name},'
          f' owner account id: {result.owner_account_id},'
          f' created at: {result.created_at},'
          f' table bucket id: {result.table_bucket_id},'
          f' type: {result.type}')


if __name__ == "__main__":
    main()

ListTableBuckets

Lists all table buckets in the current account.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="list table buckets sample")
parser.add_argument('--region', help='The region where the table buckets are located.', required=True)
parser.add_argument('--endpoint', help='The endpoint for accessing OSS Tables.')
parser.add_argument('--prefix', help='A prefix for filtering table buckets.')
parser.add_argument('--max-buckets', help='The maximum number of buckets to return.', type=int)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.list_table_buckets(oss_tables.models.ListTableBucketsRequest(
        prefix=args.prefix,
        max_buckets=args.max_buckets,
    ))

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

    if result.table_buckets:
        for i, bucket in enumerate(result.table_buckets):
            print(f'bucket {i + 1}:'
                  f' arn: {bucket.arn},'
                  f' name: {bucket.name},'
                  f' id: {bucket.table_bucket_id},'
                  f' owner account id: {bucket.owner_account_id},'
                  f' created at: {bucket.created_at}')


if __name__ == "__main__":
    main()

PutTableBucketEncryption

Configures server-side encryption for a table bucket.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="put table bucket encryption sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--sse-algorithm', help='The server-side encryption algorithm.', required=True)
parser.add_argument('--kms-key-arn', help='The KMS key ARN for encryption.')

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    encryption_configuration = oss_tables.models.EncryptionConfiguration(
        sse_algorithm=args.sse_algorithm,
        kms_key_arn=args.kms_key_arn,
    )

    result = client.put_table_bucket_encryption(oss_tables.models.PutTableBucketEncryptionRequest(
        table_bucket_arn=args.table_bucket_arn,
        encryption_configuration=encryption_configuration,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}')
    print(f'successfully updated table bucket encryption for: {args.table_bucket_arn}')


if __name__ == "__main__":
    main()

GetTableBucketEncryption

Gets the encryption configuration for a table bucket.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="get table bucket encryption sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.get_table_bucket_encryption(oss_tables.models.GetTableBucketEncryptionRequest(
        table_bucket_arn=args.table_bucket_arn,
    ))

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

    if result.encryption_configuration:
        print(f'sse algorithm: {result.encryption_configuration.sse_algorithm},'
              f' kms key arn: {result.encryption_configuration.kms_key_arn}')


if __name__ == "__main__":
    main()

DeleteTableBucketEncryption

Deletes the encryption configuration for a table bucket.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="delete table bucket encryption sample")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The service endpoint for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.delete_table_bucket_encryption(oss_tables.models.DeleteTableBucketEncryptionRequest(
        table_bucket_arn=args.table_bucket_arn,
    ))

    print(f'status code: {result.status_code},'
          f' request ID: {result.request_id}')
    print(f'successfully deleted table bucket encryption for: {args.table_bucket_arn}')


if __name__ == "__main__":
    main()

PutTableBucketPolicy

Sets the resource policy for a table bucket.

import argparse
import json
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="put table bucket policy sample")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--policy', help='The resource policy JSON string.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.put_table_bucket_policy(oss_tables.models.PutTableBucketPolicyRequest(
        table_bucket_arn=args.table_bucket_arn,
        resource_policy=args.policy,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}')
    print(f'successfully set policy for: {args.table_bucket_arn}')


if __name__ == "__main__":
    main()

Get table bucket policy

Gets the resource policy for a table bucket.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="get table bucket policy sample")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The endpoint for accessing OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.get_table_bucket_policy(oss_tables.models.GetTableBucketPolicyRequest(
        table_bucket_arn=args.table_bucket_arn,
    ))

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


if __name__ == "__main__":
    main()

DeleteTableBucketPolicy

Deletes the resource policy for a table bucket.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="delete table bucket policy sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The service endpoint for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.delete_table_bucket_policy(oss_tables.models.DeleteTableBucketPolicyRequest(
        table_bucket_arn=args.table_bucket_arn,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}')
    print(f'successfully deleted policy for: {args.table_bucket_arn}')


if __name__ == "__main__":
    main()

PutTableBucketMaintenanceConfiguration

Sets the maintenance configuration for a table bucket.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="put table bucket maintenance configuration sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The endpoint for accessing OSS Tables.', required=False)
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--type', help='The maintenance type, e.g., icebergUnreferencedFileRemoval.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    value = {
        'status': 'enabled',
        'settings': {
            'icebergUnreferencedFileRemoval': {
                'unreferencedDays': 7,
                'nonCurrentDays': 30
            }
        }
    }

    result = client.put_table_bucket_maintenance_configuration(
        oss_tables.models.PutTableBucketMaintenanceConfigurationRequest(
            table_bucket_arn=args.table_bucket_arn,
            type=args.type,
            value=value,
        )
    )

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}')
    print(f'successfully updated maintenance configuration for: {args.table_bucket_arn}')


if __name__ == "__main__":
    main()

GetTableBucketMaintenanceConfiguration

Retrieves the maintenance configuration for a table bucket.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="get table bucket maintenance configuration sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The service endpoint for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.get_table_bucket_maintenance_configuration(
        oss_tables.models.GetTableBucketMaintenanceConfigurationRequest(
            table_bucket_arn=args.table_bucket_arn,
        )
    )

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' table bucket ARN: {result.table_bucket_arn},'
          f' configuration: {result.configuration}')


if __name__ == "__main__":
    main()

PutTableBucketLogging

Enables logging for the table bucket and delivers access logs hourly to the specified prefix of the target bucket.

import argparse
import base64
import hashlib
import json
from urllib.parse import quote

import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="put table bucket logging sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--target-bucket', help='The bucket that stores the access log.', required=True)
parser.add_argument('--target-prefix', help='The prefix of the access log object.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    # Logging role. Use the default role, or replace it with a custom role name in your account.
    logging_role = 'AliyunOSSLoggingDefaultRole'

    # The request body is in JSON format. A PUT request must carry a Content-MD5 calculated from the request body.
    body = json.dumps({
        'BucketLoggingStatus': {
            'LoggingEnabled': {
                'TargetBucket': args.target_bucket,
                'TargetPrefix': args.target_prefix,
                'LoggingRole': logging_role,
                'TargetSuffix': {'UseRandomPart': False},
            }
        }
    }).encode('utf-8')

    op_input = oss.types.OperationInput(
        op_name='PutTableBucketLogging',
        method='PUT',
        headers={
            'Content-Type': 'application/json',
            'Content-MD5': base64.b64encode(hashlib.md5(body).digest()).decode(),
        },
        body=body,
        # The bucket is specified as an ARN, the key is the full resource path, and is_bucket_arn flags it to be included in signing.
        bucket=args.table_bucket_arn,
        key=f"buckets/{quote(args.table_bucket_arn, safe='')}/logging",
        op_metadata={'is_bucket_arn': True},
    )

    result = client.invoke_operation(op_input)

    print(f'status code: {result.status_code},'
          f' request id: {result.headers.get("x-oss-request-id")}')
    print(f'successfully enabled logging for: {args.table_bucket_arn}')


if __name__ == "__main__":
    main()

GetTableBucketLogging

Queries the current logging configuration of the table bucket.

import argparse
from urllib.parse import quote

import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="get table bucket logging sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    op_input = oss.types.OperationInput(
        op_name='GetTableBucketLogging',
        method='GET',
        headers={'Content-Type': 'application/json'},
        # The bucket is specified as an ARN, the key is the full resource path, and is_bucket_arn flags it to be included in signing.
        bucket=args.table_bucket_arn,
        key=f"buckets/{quote(args.table_bucket_arn, safe='')}/logging",
        op_metadata={'is_bucket_arn': True},
    )

    result = client.invoke_operation(op_input)
    # The response body must be read from the raw HTTP response.
    content = result.http_response.content.decode('utf-8') if result.http_response else ''

    print(f'status code: {result.status_code},'
          f' request id: {result.headers.get("x-oss-request-id")}')
    print(f'logging configuration: {content}')


if __name__ == "__main__":
    main()

DeleteTableBucketLogging

Disables logging for the table bucket. After logging is disabled, tables under this table bucket that do not have logging individually disabled no longer record access logs.

import argparse
from urllib.parse import quote

import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="delete table bucket logging sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    op_input = oss.types.OperationInput(
        op_name='DeleteTableBucketLogging',
        method='DELETE',
        headers={'Content-Type': 'application/json'},
        # The bucket is specified as an ARN, the key is the full resource path, and is_bucket_arn flags it to be included in signing.
        bucket=args.table_bucket_arn,
        key=f"buckets/{quote(args.table_bucket_arn, safe='')}/logging",
        op_metadata={'is_bucket_arn': True},
    )

    result = client.invoke_operation(op_input)

    print(f'status code: {result.status_code},'
          f' request id: {result.headers.get("x-oss-request-id")}')
    print(f'successfully disabled logging for: {args.table_bucket_arn}')


if __name__ == "__main__":
    main()

Namespace operations

Create namespace

Creates a namespace in a table bucket.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="Creates one or more namespaces in a Table Bucket.")
parser.add_argument('--region', help='The region of the Table Bucket.', required=True)
parser.add_argument('--endpoint', help='The access endpoint (domain name) for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the Table Bucket.', required=True)
parser.add_argument('--namespace', help='The namespace(s) to create, separated by commas.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    namespaces = args.namespace.split(",")

    result = client.create_namespace(oss_tables.models.CreateNamespaceRequest(
        table_bucket_arn=args.table_bucket_arn,
        namespace=namespaces,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}')
    print(f'successfully created namespace(s) in Table Bucket: {args.table_bucket_arn}')


if __name__ == "__main__":
    main()

DeleteNamespace

Deletes the specified namespace.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="delete namespace sample")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The access endpoint for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace to delete.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.delete_namespace(oss_tables.models.DeleteNamespaceRequest(
        table_bucket_arn=args.table_bucket_arn,
        namespace=args.namespace,
    ))

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


if __name__ == "__main__":
    main()

GetNamespace

Gets the details of a specified namespace.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="get namespace sample")
parser.add_argument('--region', help='The region where the Table Bucket is located.', required=True)
parser.add_argument('--endpoint', help='The service endpoint for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the Table Bucket.', required=True)
parser.add_argument('--namespace', help='The namespace to retrieve.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.get_namespace(oss_tables.models.GetNamespaceRequest(
        table_bucket_arn=args.table_bucket_arn,
        namespace=args.namespace,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' namespace: {result.namespace},'
          f' namespace id: {result.namespace_id},'
          f' table bucket id: {result.table_bucket_id},'
          f' owner account id: {result.owner_account_id},'
          f' created at: {result.created_at},'
          f' created by: {result.created_by}')


if __name__ == "__main__":
    main()

List namespaces

Lists all namespaces in a table bucket.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="list namespaces sample")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The service endpoint for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--prefix', help='The prefix to filter namespaces.')
parser.add_argument('--max-namespaces', type=int, help='The maximum number of namespaces to return.')

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.list_namespaces(oss_tables.models.ListNamespacesRequest(
        table_bucket_arn=args.table_bucket_arn,
        prefix=args.prefix,
        max_namespaces=args.max_namespaces,
    ))

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

    if result.namespaces:
        for i, ns in enumerate(result.namespaces):
            print(f'namespace {i + 1}:'
                  f' namespace: {ns.namespace},'
                  f' namespace id: {ns.namespace_id},'
                  f' owner account id: {ns.owner_account_id},'
                  f' created at: {ns.created_at},'
                  f' created by: {ns.created_by}')


if __name__ == "__main__":
    main()

Table operations

CreateTable

Creates an Iceberg table with a schema, partition, and encryption configuration.

Note

This API supports only Iceberg tables, not Lance tables. To create Lance tables, use the Lance REST Namespace API or the Lance ecosystem (Spark, Trino, and other compute engines).

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="create table sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The endpoint for accessing OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace of the table.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)
parser.add_argument('--format', help='The format of the table.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    # Create schema fields
    schema = oss_tables.models.IcebergSchema(fields=[
        oss_tables.models.SchemaField(name="id", type="long", required=True),
        oss_tables.models.SchemaField(name="name", type="string", required=False),
        oss_tables.models.SchemaField(name="ts", type="timestamptz", required=False),
    ])

    # Create partition spec
    partition_spec = oss_tables.models.IcebergPartitionSpec(
        spec_id=0,
        fields=[
            oss_tables.models.IcebergPartitionField(
                source_id=2,
                field_id=1001,
                name="region",
                transform="identity",
            ),
        ],
    )

    # Create Iceberg metadata
    iceberg_metadata = oss_tables.models.IcebergMetadata(
        schema=schema,
        partition_spec=partition_spec,
    )

    # Set the table metadata
    metadata = oss_tables.models.TableMetadata(iceberg=iceberg_metadata)

    # Add encryption configuration
    encryption_configuration = oss_tables.models.EncryptionConfiguration(
        sse_algorithm="AES256",
    )

    result = client.create_table(oss_tables.models.CreateTableRequest(
        table_bucket_arn=args.table_bucket_arn,
        namespace=args.namespace,
        name=args.name,
        format=args.format,
        metadata=metadata,
        encryption_configuration=encryption_configuration,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' table arn: {result.table_arn},'
          f' version token: {result.version_token}')


if __name__ == "__main__":
    main()

DeleteTable

Deletes the specified table.

Note

This API supports only Iceberg tables, not Lance tables. To delete Lance tables, use the Lance REST Namespace API or the Lance ecosystem (Spark, Trino, and other compute engines).

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="Sample script for deleting a table.")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The service endpoint for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace of the table.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)
parser.add_argument('--version-token', help='The version token for optimistic locking.')

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.delete_table(oss_tables.models.DeleteTableRequest(
        table_bucket_arn=args.table_bucket_arn,
        namespace=args.namespace,
        name=args.name,
        version_token=args.version_token,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}')
    print(f'successfully deleted table: {args.namespace}/{args.name}')


if __name__ == "__main__":
    main()

Get table

Gets the details of a specified table.

Note

This API supports only Iceberg tables, not Lance tables. To query Lance tables, use the Lance REST Namespace API or the Lance ecosystem (Spark, Trino, and other compute engines).

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="get table sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The endpoint for accessing OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.')
parser.add_argument('--namespace', help='The namespace of the table.')
parser.add_argument('--name', help='The name of the table.')
parser.add_argument('--table-arn', help='The ARN of the table.')

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    # Method 1: Query using the table bucket ARN, namespace, and name
    result = client.get_table(oss_tables.models.GetTableRequest(
        table_bucket_arn=args.table_bucket_arn,
        namespace=args.namespace,
        name=args.name,
    ))

    # Method 2: Query using the table ARN
    # result = client.get_table(oss_tables.models.GetTableRequest(
    #     table_arn=args.table_arn,
    # ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' name: {result.name},'
          f' table arn: {result.table_arn},'
          f' format: {result.format},'
          f' created by: {result.created_by},'
          f' created at: {result.created_at}')


if __name__ == "__main__":
    main()

ListTables

Lists all tables in a namespace.

Note

This API supports only Iceberg tables, not Lance tables. To list Lance tables, use the Lance REST Namespace API or the Lance ecosystem (Spark, Trino, and other compute engines).

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="list tables sample")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The endpoint for accessing OSS Tables.', required=False)
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace of the tables.', required=True)
parser.add_argument('--prefix', help='The prefix to filter tables.')
parser.add_argument('--max-tables', type=int, help='The maximum number of tables to return.')

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.list_tables(oss_tables.models.ListTablesRequest(
        table_bucket_arn=args.table_bucket_arn,
        namespace=args.namespace,
        prefix=args.prefix,
        max_tables=args.max_tables,
    ))

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

    if result.table_summaries:
        for i, table in enumerate(result.table_summaries):
            print(f'table {i + 1}:'
                  f' name: {table.name},'
                  f' namespace: {table.namespace},'
                  f' type: {table.type},'
                  f' table arn: {table.table_arn},'
                  f' created at: {table.created_at},'
                  f' modified at: {table.modified_at}')


if __name__ == "__main__":
    main()

RenameTable

Renames a table or moves it to another namespace.

Note

This API supports only Iceberg tables, not Lance tables. To rename Lance tables, use the Lance REST Namespace API or the Lance ecosystem (Spark, Trino, and other compute engines).

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="rename table sample")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The endpoint for accessing OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace of the table.', required=True)
parser.add_argument('--name', help='The current name of the table.', required=True)
parser.add_argument('--new-namespace-name', help='The new namespace for the table.')
parser.add_argument('--new-name', help='The new name for the table.')
parser.add_argument('--version-token', help='The version token for the table.')

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.rename_table(oss_tables.models.RenameTableRequest(
        table_bucket_arn=args.table_bucket_arn,
        namespace=args.namespace,
        name=args.name,
        new_namespace_name=args.new_namespace_name,
        new_name=args.new_name,
        version_token=args.version_token,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}')
    print(f'Successfully renamed table {args.namespace}/{args.name}.')


if __name__ == "__main__":
    main()

GetTableMetadataLocation

Retrieves the location of a table's metadata file.

Note

This API supports only Iceberg tables, not Lance tables.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="get table metadata location sample")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The service endpoint for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace of the table.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.get_table_metadata_location(
        oss_tables.models.GetTableMetadataLocationRequest(
            table_bucket_arn=args.table_bucket_arn,
            namespace=args.namespace,
            name=args.name,
        )
    )

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' version token: {result.version_token},'
          f' metadata location: {result.metadata_location},'
          f' warehouse location: {result.warehouse_location}')


if __name__ == "__main__":
    main()

UpdateTableMetadataLocation

Updates the metadata location for a table.

Note

This API supports only Iceberg tables, not Lance tables.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="Updates the metadata location of a table.")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The domain name for accessing OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace of the table.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)
parser.add_argument('--version-token', help='The version token.', required=True)
parser.add_argument('--metadata-location', help='The new metadata location.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.update_table_metadata_location(
        oss_tables.models.UpdateTableMetadataLocationRequest(
            table_bucket_arn=args.table_bucket_arn,
            namespace=args.namespace,
            name=args.name,
            version_token=args.version_token,
            metadata_location=args.metadata_location,
        )
    )

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' name: {result.name},'
          f' version token: {result.version_token},'
          f' metadata location: {result.metadata_location},'
          f' table arn: {result.table_arn}')


if __name__ == "__main__":
    main()

GetTableEncryption

Retrieves the encryption configuration for a table.

Note

This API is independent of table format and supports both Iceberg and Lance tables.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="get table encryption sample")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The domain names used to access OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace of the table.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.get_table_encryption(oss_tables.models.GetTableEncryptionRequest(
        table_bucket_arn=args.table_bucket_arn,
        namespace=args.namespace,
        name=args.name,
    ))

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

    if result.encryption_configuration:
        print(f'sse algorithm: {result.encryption_configuration.sse_algorithm},'
              f' kms key arn: {result.encryption_configuration.kms_key_arn}')


if __name__ == "__main__":
    main()

Put table policy

Sets the resource policy for a table.

Note

This API is independent of table format and supports both Iceberg and Lance tables.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="put table policy sample")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The endpoint for accessing OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace of the table.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)
parser.add_argument('--policy', help='The resource policy JSON string.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.put_table_policy(oss_tables.models.PutTablePolicyRequest(
        table_bucket_arn=args.table_bucket_arn,
        namespace=args.namespace,
        name=args.name,
        resource_policy=args.policy,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}')
    print(f'successfully set policy for: {args.namespace}/{args.name}')


if __name__ == "__main__":
    main()

GetTablePolicy

Gets the resource policy for a table.

Note

This API is independent of table format and supports both Iceberg and Lance tables.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="get table policy sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The service endpoint for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace of the table.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.get_table_policy(oss_tables.models.GetTablePolicyRequest(
        table_bucket_arn=args.table_bucket_arn,
        namespace=args.namespace,
        name=args.name,
    ))

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


if __name__ == "__main__":
    main()

DeleteTablePolicy

Deletes the resource policy for a table.

Note

This API is independent of table format and supports both Iceberg and Lance tables.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="delete table policy sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The access endpoint for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace of the table.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.delete_table_policy(oss_tables.models.DeleteTablePolicyRequest(
        table_bucket_arn=args.table_bucket_arn,
        namespace=args.namespace,
        name=args.name,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}')
    print(f'successfully deleted policy for: {args.namespace}/{args.name}')


if __name__ == "__main__":
    main()

PutTableMaintenanceConfiguration

Sets the maintenance configuration for a table.

Note

This API supports only Iceberg tables, not Lance tables.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="put table maintenance configuration sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The service endpoint for accessing OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace of the table.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)
parser.add_argument('--type', help='The maintenance type, e.g., icebergUnreferencedFileRemoval.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    value = {
        'status': 'enabled',
        'settings': {
            'icebergUnreferencedFileRemoval': {
                'unreferencedDays': 7,
                'nonCurrentDays': 30
            }
        }
    }

    result = client.put_table_maintenance_configuration(
        oss_tables.models.PutTableMaintenanceConfigurationRequest(
            table_bucket_arn=args.table_bucket_arn,
            namespace=args.namespace,
            name=args.name,
            type=args.type,
            value=value,
        )
    )

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}')
    print(f'successfully updated maintenance configuration for: {args.namespace}/{args.name}')


if __name__ == "__main__":
    main()

GetTableMaintenanceConfiguration

Retrieves the maintenance configuration for a table.

Note

This API supports only Iceberg tables, not Lance tables.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="get table maintenance configuration sample")
parser.add_argument('--region', help='The region where the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The access endpoint for OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace of the table.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.get_table_maintenance_configuration(
        oss_tables.models.GetTableMaintenanceConfigurationRequest(
            table_bucket_arn=args.table_bucket_arn,
            namespace=args.namespace,
            name=args.name,
        )
    )

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


if __name__ == "__main__":
    main()

GetTableMaintenanceJobStatus

Retrieves the execution status of a table maintenance job.

Note

This API supports only Iceberg tables, not Lance tables.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="Gets the status of a table maintenance job.")
parser.add_argument('--region', help='The region of the table bucket.', required=True)
parser.add_argument('--endpoint', help='The endpoint for accessing OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The namespace of the table.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    result = client.get_table_maintenance_job_status(
        oss_tables.models.GetTableMaintenanceJobStatusRequest(
            table_bucket_arn=args.table_bucket_arn,
            namespace=args.namespace,
            name=args.name,
        )
    )

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


if __name__ == "__main__":
    main()

PutTableLogging

Sets the logging status for a single table. When logging is enabled for the table bucket, you can disable logging for a specified table individually.

import argparse
import base64
import hashlib
from urllib.parse import quote

import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="put table logging sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The name of the namespace.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    # The request body is in JSON format. A PUT request must carry a Content-MD5 calculated from the request body.
    body = b'{"status":"disabled"}'

    op_input = oss.types.OperationInput(
        op_name='PutTableLogging',
        method='PUT',
        headers={
            'Content-Type': 'application/json',
            'Content-MD5': base64.b64encode(hashlib.md5(body).digest()).decode(),
        },
        body=body,
        # The bucket is specified as an ARN, the key is the full resource path, and is_bucket_arn flags it to be included in signing.
        bucket=args.table_bucket_arn,
        key=f"tables/{quote(args.table_bucket_arn, safe='')}"
            f"/{quote(args.namespace, safe='')}"
            f"/{quote(args.name, safe='')}/logging",
        op_metadata={'is_bucket_arn': True},
    )

    result = client.invoke_operation(op_input)

    print(f'status code: {result.status_code},'
          f' request id: {result.headers.get("x-oss-request-id")}')
    print(f'successfully updated logging config for table: {args.name}')


if __name__ == "__main__":
    main()

GetTableLogging

Queries the logging status of a single table.

import argparse
from urllib.parse import quote

import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="get table logging sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The name of the namespace.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    op_input = oss.types.OperationInput(
        op_name='GetTableLogging',
        method='GET',
        headers={'Content-Type': 'application/json'},
        # The bucket is specified as an ARN, the key is the full resource path, and is_bucket_arn flags it to be included in signing.
        bucket=args.table_bucket_arn,
        key=f"tables/{quote(args.table_bucket_arn, safe='')}"
            f"/{quote(args.namespace, safe='')}"
            f"/{quote(args.name, safe='')}/logging",
        op_metadata={'is_bucket_arn': True},
    )

    result = client.invoke_operation(op_input)
    # The response body must be read from the raw HTTP response.
    content = result.http_response.content.decode('utf-8') if result.http_response else ''

    print(f'status code: {result.status_code},'
          f' request id: {result.headers.get("x-oss-request-id")}')
    print(f'logging status: {content}')


if __name__ == "__main__":
    main()

DeleteTableLogging

Deletes the logging setting on a table. After the setting is deleted, the table no longer has its own logging status and inherits the logging configuration of the table bucket.

import argparse
from urllib.parse import quote

import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.tables as oss_tables

parser = argparse.ArgumentParser(description="delete table logging sample")
parser.add_argument('--region', help='The region in which the table bucket is located.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS Tables.')
parser.add_argument('--table-bucket-arn', help='The ARN of the table bucket.', required=True)
parser.add_argument('--namespace', help='The name of the namespace.', required=True)
parser.add_argument('--name', help='The name of the table.', required=True)

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

    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_tables.Client(cfg)

    op_input = oss.types.OperationInput(
        op_name='DeleteTableLogging',
        method='DELETE',
        headers={'Content-Type': 'application/json'},
        # The bucket is specified as an ARN, the key is the full resource path, and is_bucket_arn flags it to be included in signing.
        bucket=args.table_bucket_arn,
        key=f"tables/{quote(args.table_bucket_arn, safe='')}"
            f"/{quote(args.namespace, safe='')}"
            f"/{quote(args.name, safe='')}/logging",
        op_metadata={'is_bucket_arn': True},
    )

    result = client.invoke_operation(op_input)

    print(f'status code: {result.status_code},'
          f' request id: {result.headers.get("x-oss-request-id")}')
    print(f'successfully deleted logging config for table: {args.name}')


if __name__ == "__main__":
    main()