This topic describes how to determine whether a specified bucket exists using Python SDK V2.
Usage notes
The sample code in this topic uses the region ID
cn-hangzhoufor the China (Hangzhou) region as an example. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see Regions and endpoints.This topic provides an example of obtaining access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.
To determine whether a bucket exists, you must have the
oss:GetBucketAclpermission. For more information, see Attach a custom policy to a RAM user.
Method definition
is_bucket_exist(bucket: str, request_payer: str | None = None, **kwargs) → boolRequest parameters
Parameter | Type | Description |
bucket | str | The name of the bucket. |
Return values
Type | Description |
bool | The return value. |
For the complete method definition, see is_bucket_exist.
Sample code
You can use the following code to determine whether a bucket exists.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser that describes that this script is used to check whether a specified bucket exists.
parser = argparse.ArgumentParser(description="Check if a specified OSS bucket exists.")
# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the name of the bucket to check for existence. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket to check for existence.', required=True)
# Add the --endpoint command-line argument, which specifies the domain names that other services can use to access OSS. This argument 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 arguments and check whether the specified bucket exists.
"""
args = parser.parse_args() # Parse command-line arguments.
# Load credentials from environment variables for identity verification.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configurations of the SDK, and set the credential provider and region information.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
# If an endpoint argument is provided, set the endpoint in the configuration.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client using the configured information.
client = oss.Client(cfg)
# Check whether the specified bucket exists.
result = client.is_bucket_exist(bucket=args.bucket)
# Print the check result.
print(f'Bucket {args.bucket} exists: {result}')
if __name__ == "__main__":
main() # The entry point of the script. The main function is called when the file is run directly.References
For the complete sample code, see is_bucket_exist.py.
This topic provides an example of obtaining access credentials from environment variables. For more information about how to configure access credentials, see https://icms.alibaba-inc.com/content/oss/oss?l=1&m=15095&n=5528612.