All Products
Search
Document Center

Object Storage Service:Determine whether an object exists using OSS SDK for Java

Last Updated:Sep 24, 2026

This topic describes how to determine whether an object exists using OSS SDK for Python.

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 OSS regions and endpoints.

  • The oss:GetObject permission is required to determine whether objects exist. For more information, see Grant a custom policy.

Method

is_object_exist(bucket: str, key: str, version_id: str | None = None, request_payer: str | None = None, **kwargs) → bool

Request parameters

Parameter

Type

Description

bucket

str

Name of the bucket.

key

str

Name of the object.

version_id

str

Version ID of the object (optional).

Response parameters

Type

Description

bool

Determines whether an object exists.

For details about this method, see is_object_exist.

Sample code

Below is a code example for determining whether an object exists.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line parameter parser.
parser = argparse.ArgumentParser(description="Check if an object exists in a specified OSS bucket")
# Specify the command line parameters.
parser.add_argument('--region', help='The region where the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket to check.', required=True)
parser.add_argument('--endpoint', help='Optional: The endpoint URL for the OSS service. If not provided, the default endpoint will be used.')
parser.add_argument('--key', help='The key (or name) of the object to check for existence.', required=True)

def main():
    # Parse the command line parameters.
    args = parser.parse_args()

    # Load access credentials from environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

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

    # Update the region parameter based on the input.
    cfg.region = args.region

    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client instance.
    client = oss.Client(cfg)

    # Call the is_object_exist method to check whether the specified object exists in the bucket.
    result = client.is_object_exist(
        bucket=args.bucket,
        key=args.key,
    )

    # Output the result.
    print(f'Object exists: {result}')

# Entry point of the script. The main function is invoked when the file is run directly.
if __name__ == "__main__":
    main()

Reference

  • For the complete sample code used to determine whether an object exists, visit is_object_exist.py.