is_object_exist checks whether a named object is present in a bucket and returns a boolean. The call requires the oss:GetObject permission.
Prerequisites
Before you begin, ensure that you have:
The
oss:GetObjectpermission granted to your RAM (Resource Access Management) user. For details, see Attach a custom policy to a RAM userOSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETset as environment variables
Method definition
is_object_exist(bucket: str, key: str, version_id: str | None = None, request_payer: str | None = None, **kwargs) -> boolParameters
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | str | Yes | The name of the bucket. |
key | str | Yes | The name of the object. |
version_id | str | None | No | The version ID of the object. Use this when versioning is enabled on the bucket. |
request_payer | str | None | No | The entity that pays for the request. Set to requester for requester-pays buckets. |
Return value
| Type | Description |
|---|---|
bool | True if the object exists; False if it does not. |
For the full method reference, see is_object_exist.
Check if an object exists
The following examples use cn-hangzhou as the region. By default, the SDK connects to the public endpoint for that region. To access OSS from another Alibaba Cloud service in the same region, pass the internal endpoint explicitly. For the full list of regions and endpoints, see OSS regions and endpoints.
Basic check
import alibabacloud_oss_v2 as oss
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = "cn-hangzhou"
client = oss.Client(cfg)
result = client.is_object_exist(
bucket="examplebucket",
key="exampledir/exampleobject.jpg",
)
print(f"Object exists: {result}")Check a specific version
If versioning is enabled on the bucket, pass version_id to check a particular version of the object.
result = client.is_object_exist(
bucket="examplebucket",
key="exampledir/exampleobject.jpg",
version_id="<your-version-id>",
)
print(f"Object version exists: {result}")Full command-line example
The following script accepts the bucket name, object key, and region as command-line arguments.
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="Check if an object exists in a specified OSS bucket")
parser.add_argument('--region', help='The region where the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The endpoint URL for the OSS service. If not provided, the default endpoint is used.')
parser.add_argument('--key', help='The name of the object.', 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.is_object_exist(
bucket=args.bucket,
key=args.key,
)
print(f"Object exists: {result}")
if __name__ == "__main__":
main()Run the script:
python is_object_exist.py \
--region cn-hangzhou \
--bucket examplebucket \
--key exampledir/exampleobject.jpgReferences
Complete sample: is_object_exist.py