Determine whether a bucket exists using OSS SDK for Python 2.0

Updated at:

This topic describes how to determine whether a bucket exists using OSS SDK for Python 2.0.

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 these resources from other Alibaba Cloud services in the same region, use an internal endpoint. For information about supported regions and endpoints, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure the access credentials, see Configure access credentials.

  • To determine whether a bucket exists, you must have the oss:GetBucketAcl permission. For more information, see Grant a custom policy.

Method

is_bucket_exist(bucket: str, request_payer: str | None = None, **kwargs) → bool

Request parameters

Parameter

Type

Description

bucket

str

The name of the bucket.

Response parameters

Type

Description

bool

The return value.

For details about the method for determining whether a bucket exists, see is_bucket_exist.

Sample code

Below is the sample code for determining whether a bucket exists:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line parameter parser and describe the purpose of the script.
parser = argparse.ArgumentParser(description="Check if a specified OSS bucket exists.")

# Specify the --region parameter to indicate the region in which the bucket is located. This parameter is required. 
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)

# Specify the --bucket parameter to indicate the name of the bucket. This command line parameter is required.
parser.add_argument('--bucket', help='The name of the bucket to check for existence.', required=True)

# Specify the --endpoint parameter to indicate the endpoint of the region in which the bucket is located. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')

def main():
    """
    The main function which is used to parse command line parameters and determine if the specified bucket exists.
    """

    args = parser.parse_args()  # Parse the command-line parameters.

    # Obtain access credentials from environment variables for authentication.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configuration of the SDK and specify the credential provider and region.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # If an endpoint is provided, specify the endpoint in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configurations to create an OSSClient instance.
    client = oss.Client(cfg)

    # Determine if the specified bucket exists.
    result = client.is_bucket_exist(bucket=args.bucket)

    # Display the result.
    print(f'Bucket {args.bucket} exists: {result}')

if __name__ == "__main__":
    main()  # The entry point of the script. When the script is directly run, the main function is called. 

Reference

  • For the complete sample code that is used to determine whether a bucket exists, visit is_bucket_exist.py.