This topic describes how to determine whether an object exists.
# -*- coding: utf-8 -*- import oss2 # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret') # Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. # Set yourBucketName to the name of the bucket. bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName') # Specify the full path of the object. The full path of the object cannot contain the bucket name. exist = bucket.object_exists('exampleobject.txt') # If the returned value is true, the specified object exists. If the returned value is false, the specified object does not exist. if exist: print('object exist') else: print('object not exist')