All Products
Search
Document Center

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

Last Updated:Mar 20, 2026

Use OSS SDK for Python V2 to enable or query archive direct read for a bucket. With archive direct read enabled, Archive objects can be read without a restore operation.

This topic covers two related operations: put_bucket_archive_direct_read to configure archive direct read, and get_bucket_archive_direct_read to verify its current status.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou (China (Hangzhou)). By default, the public endpoint is used. To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint instead. For a full list of regions and endpoints, see Regions and endpoints.

Prerequisites

Before running the sample code, ensure that you have:

  • An OSS bucket of the Archive storage class

  • Environment variables set with valid credentials for EnvironmentVariableCredentialsProvider

Method definitions

The following two methods manage archive direct read at the bucket level.

Enable archive direct read

put_bucket_archive_direct_read(request: PutBucketArchiveDirectReadRequest, **kwargs) -> PutBucketArchiveDirectReadResult

Request syntax:

result = client.put_bucket_archive_direct_read(
    oss.PutBucketArchiveDirectReadRequest(
        bucket='<bucket-name>',
        archive_direct_read_configuration=oss.ArchiveDirectReadConfiguration(
            enabled=True,   # True to enable, False to disable
        ),
    )
)
ParameterTypeDescription
requestPutBucketArchiveDirectReadRequestRequest parameters. See PutBucketArchiveDirectReadRequest
Response typeDescription
PutBucketArchiveDirectReadResultResponse parameters. See PutBucketArchiveDirectReadResult

For the complete method definition, see put_bucket_archive_direct_read.

Query archive direct read status

get_bucket_archive_direct_read(request: GetBucketArchiveDirectReadRequest, **kwargs) -> GetBucketArchiveDirectReadResult

Request syntax:

result = client.get_bucket_archive_direct_read(
    oss.GetBucketArchiveDirectReadRequest(
        bucket='<bucket-name>',
    )
)
ParameterTypeDescription
requestGetBucketArchiveDirectReadRequestRequest parameters. See GetBucketArchiveDirectReadRequest
Response typeDescription
GetBucketArchiveDirectReadResultResponse parameters. See GetBucketArchiveDirectReadResult

For the complete method definition, see get_bucket_archive_direct_read.

Sample code

Both examples use EnvironmentVariableCredentialsProvider to load credentials from environment variables, avoiding hardcoded secrets.

Enable archive direct read for a bucket

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="put bucket archive direct read 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('--enabled', help='Specifies whether to enable real-time access of Archive objects for a bucket. Valid values: "true" or "false".', 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.Client(cfg)

    result = client.put_bucket_archive_direct_read(oss.PutBucketArchiveDirectReadRequest(
            bucket=args.bucket,
            archive_direct_read_configuration=oss.ArchiveDirectReadConfiguration(
                enabled=args.enabled.lower() == 'true',  # Converts the string argument to a Python Boolean.
            ),
    ))

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

if __name__ == "__main__":
    main()

Query archive direct read status for a bucket

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="get bucket archive direct read 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()

    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.Client(cfg)

    result = client.get_bucket_archive_direct_read(oss.GetBucketArchiveDirectReadRequest(
            bucket=args.bucket,
    ))

    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"},'
    )

if __name__ == "__main__":
    main()