All Products
Search
Document Center

Object Storage Service:Real-time access of Archive objects using OSS SDK for Python 2.0

Last Updated:Oct 21, 2025

This topic describes how to use Python SDK V2 to manage real-time access of Archive objects for a bucket.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

Method definition

Enable real-time access of Archive objects for a bucket

put_bucket_archive_direct_read(request: PutBucketArchiveDirectReadRequest, **kwargs) → PutBucketArchiveDirectReadResult

Query whether real-time access of Archive objects is enabled for a bucket

get_bucket_archive_direct_read(request: GetBucketArchiveDirectReadRequest, **kwargs) → GetBucketArchiveDirectReadResult

Request parameters

Parameter

Type

Description

request

PutBucketArchiveDirectReadRequest

The request parameters. For more information, see PutBucketArchiveDirectReadRequest

GetBucketArchiveDirectReadRequest

The request parameters. For more information, see GetBucketArchiveDirectReadRequest

Response parameters

Type

Description

PutBucketArchiveDirectReadResult

The response parameters. For more information, see PutBucketArchiveDirectReadResult

GetBucketArchiveDirectReadResult

The response parameters. For more information, see GetBucketArchiveDirectReadResult

For the complete definition of the method to enable real-time access of Archive objects for a bucket, see put_bucket_archive_direct_read.

For the complete definition of the method to query whether real-time access of Archive objects is enabled for a bucket, see get_bucket_archive_direct_read.

Sample code

Enable real-time access of Archive objects for a bucket

You can use the following code to enable real-time access of Archive objects for a bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the purpose of the script: set the configuration for real-time access of Archive objects for a bucket.
parser = argparse.ArgumentParser(description="put bucket archive direct read sample")

# Define command-line arguments, including the required region, bucket name, endpoint, and whether to enable real-time access of Archive objects.
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('--enabled', help='Specifies whether to enable real-time access of Archive objects for a bucket. Valid values: "true" or "false".', required=True)

def main():
    # Parse the command-line arguments to obtain the values entered by the user.
    args = parser.parse_args()

    # Load access credential information from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default SDK configurations and set the authentication provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region property of the configuration object based on the command-line arguments provided by the user.
    cfg.region = args.region

    # If a custom endpoint is provided, update the endpoint property in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configurations to initialize the OSS client for interacting with OSS.
    client = oss.Client(cfg)

    # Execute a request to set the configuration for real-time access of Archive objects for the specified bucket.
    result = client.put_bucket_archive_direct_read(oss.PutBucketArchiveDirectReadRequest(
            bucket=args.bucket,  # The bucket name.
            archive_direct_read_configuration=oss.ArchiveDirectReadConfiguration(
                enabled=args.enabled.lower() == 'true',  # Convert the string-formatted Boolean value to a Python Boolean type.
            ),
    ))

    # Print the status code and request ID of the operation result to confirm the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
    )

# When this script is directly executed, call the main function to start processing the logic.
if __name__ == "__main__":
    main()  # The entry point of the script. The program flow starts from here.

Query whether real-time access of Archive objects is enabled for a bucket

You can use the following code to query whether real-time access of Archive objects is enabled for a bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the purpose of the script: obtain the configuration for real-time access of Archive objects for a bucket.
parser = argparse.ArgumentParser(description="get bucket archive direct read sample")

# Define command-line arguments, including the required region, bucket name, and endpoint.
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():
    # Parse the command-line arguments to obtain the values entered by the user.
    args = parser.parse_args()

    # Load access credential information from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default SDK configurations and set the authentication provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region property of the configuration object based on the command-line arguments provided by the user.
    cfg.region = args.region

    # If a custom endpoint is provided, update the endpoint property in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configurations to initialize the OSS client for interacting with OSS.
    client = oss.Client(cfg)

    # Execute a request to obtain the details of the configuration for real-time access of Archive objects for the specified bucket.
    result = client.get_bucket_archive_direct_read(oss.GetBucketArchiveDirectReadRequest(
            bucket=args.bucket,  # The bucket name.
    ))

    # Print the status code and request ID of the operation result to confirm the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' enabled: {result.archive_direct_read_configuration.enabled if hasattr(result.archive_direct_read_configuration, "enabled") else "Not set"},'  # Whether real-time access of Archive objects is enabled. If it is not set, "Not set" is displayed.
    )

# When this script is directly executed, call the main function to start processing the logic.
if __name__ == "__main__":
    main()  # The entry point of the script. The program flow starts from here.