minio / minio-py

MinIO Client SDK for Python
https://docs.min.io/docs/python-client-quickstart-guide.html
Apache License 2.0
851 stars 325 forks source link

How does minio's python sdk determine if an object exists? #1316

Closed ponponon closed 1 year ago

ponponon commented 1 year ago

I googled 「"python" "minio" determines whether an object exists」 and couldn't find an answer. minio is a full-fledged object store, it can't even have a basic API, right?

balamurugana commented 1 year ago

Use stat_object() API

ponponon commented 1 year ago

Use stat_object() API

One problem with stat_object is that it reports S3Error when the object doesn't exist.

from minio.error import S3Error

def object_exist(file_path: str) -> bool:
    try:
        client.stat_object(bucket,file_path)
        return True
    except Exception as error:
        if 'code: NoSuchKey' in str(error):
            return False
        else:
            raise error

So every time I call stat_object, I need to use try ... except ...

I would like to have a function that returns a bool, but the current minio python sdk doesn't have such a convenient function.

Is it possible to provide such a function? I can provide PR

balamurugana commented 1 year ago

This is the only way as per S3 specifications and we have no plans to add a wrapper to stat_object() for this purpose.