Metadata management (Python SDK V2)

Updated at:

This topic describes how to create, view, update, and delete the metadata table configurations of a bucket by using Python SDK V2, including the journal table configuration and the inventory table configuration.

Overview

Metadata management provides metadata table configuration capabilities for a bucket, including the following two types of configurations:

  • Journal table configuration (JournalTableConfiguration): Use RecordExpiration to control whether journal records are enabled (Expiration is set to ENABLED or DISABLED) and the record retention period (Days).

  • Inventory table configuration (InventoryTableConfiguration): Use ConfigurationState to control whether the inventory table is enabled (set to ENABLED or DISABLED), and optionally use EncryptionConfiguration to specify the encryption algorithm (SseAlgorithm).

Notes

  • In this topic, access credentials are obtained from environment variables. Make sure that you have configured access credentials (AccessKey) before use.

  • Make sure that you have the permissions to perform metadata table configuration operations on the target bucket. By default, the bucket owner has these permissions. If you do not have the required permissions, contact the bucket owner to grant you the permissions.

  • You can also enable, modify, or disable metadata management in the OSS console.

Sample code

Create the metadata table configuration

The following code provides an example of how to create the metadata table configuration for a bucket (setting both the journal table and the inventory table).

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="create bucket metadata configuration 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 domain names that other services can use to access OSS')


def main():

    args = parser.parse_args()

    # Loading credentials values from the environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Using the SDK's default configuration
    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.Client(cfg)

    result = client.create_bucket_metadata_configuration(oss.CreateBucketMetadataConfigurationRequest(
            bucket=args.bucket,
            metadata_configuration=oss.MetadataConfiguration(
                journal_table_configuration=oss.JournalTableConfiguration(
                    record_expiration=oss.MetadataTableRecordExpiration(
                        expiration='ENABLED',
                        days=30,
                    ),
                ),
                inventory_table_configuration=oss.InventoryTableConfiguration(
                    configuration_state='DISABLED',
                ),
            ),
    ))

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


if __name__ == "__main__":
    main()

View the metadata table configuration

The following code provides an example of how to view the metadata table configuration of a bucket.

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="get bucket metadata configuration 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 domain names that other services can use to access OSS')


def main():

    args = parser.parse_args()

    # Loading credentials values from the environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Using the SDK's default configuration
    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.Client(cfg)

    result = client.get_bucket_metadata_configuration(oss.GetBucketMetadataConfigurationRequest(
            bucket=args.bucket,
    ))

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


if __name__ == "__main__":
    main()

Update the inventory table configuration

The following code provides an example of how to update the inventory table configuration of a bucket.

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="update bucket metadata inventory table configuration 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 domain names that other services can use to access OSS')
parser.add_argument('--state', help='The state of the inventory metadata table. Valid values: ENABLED, DISABLED.', choices=['ENABLED', 'DISABLED'], default='ENABLED')


def main():

    args = parser.parse_args()

    # Loading credentials values from the environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Using the SDK's default configuration
    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.Client(cfg)

    result = client.update_bucket_metadata_inventory_table_configuration(oss.UpdateBucketMetadataInventoryTableConfigurationRequest(
            bucket=args.bucket,
            inventory_table_configuration=oss.InventoryTableConfiguration(
                configuration_state=args.state,
            ),
    ))

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


if __name__ == "__main__":
    main()

Update the journal table configuration

The following code provides an example of how to update the journal table configuration of a bucket.

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="update bucket metadata journal table configuration 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 domain names that other services can use to access OSS')
parser.add_argument('--expiration', help='The expiration state of journal table records. Valid values: ENABLED, DISABLED.', choices=['ENABLED', 'DISABLED'], default='ENABLED')
parser.add_argument('--days', help='The number of days to retain journal records. The minimum value is 7.', type=int, default=30)


def main():

    args = parser.parse_args()

    # Loading credentials values from the environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Using the SDK's default configuration
    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.Client(cfg)

    result = client.update_bucket_metadata_journal_table_configuration(oss.UpdateBucketMetadataJournalTableConfigurationRequest(
            bucket=args.bucket,
            journal_table_configuration=oss.JournalTableConfiguration(
                record_expiration=oss.MetadataTableRecordExpiration(
                    expiration=args.expiration,
                    days=args.days if args.expiration == 'ENABLED' else None,
                ),
            ),
    ))

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


if __name__ == "__main__":
    main()

Delete the metadata table configuration

The following code provides an example of how to delete the metadata table configuration of a bucket.

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="delete bucket metadata configuration 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 domain names that other services can use to access OSS')


def main():

    args = parser.parse_args()

    # Loading credentials values from the environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Using the SDK's default configuration
    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.Client(cfg)

    result = client.delete_bucket_metadata_configuration(oss.DeleteBucketMetadataConfigurationRequest(
            bucket=args.bucket,
    ))

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


if __name__ == "__main__":
    main()

References