Skip to content

Commit

Permalink
Use get_file_status API to reduce rpc requests
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghua committed Nov 12, 2024
1 parent bf1c9d4 commit 5bd6337
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions tosfs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
from tosfs.mpu import MultipartUploader
from tosfs.retry import INVALID_RANGE_CODE, retryable_func_executor
from tosfs.tag import BucketTagMgr
from tosfs.utils import find_bucket_key, get_brange
from tosfs.utils import find_bucket_key, get_brange, is_dir
from tosfs.version import Version

logger = logging.getLogger("tosfs")
Expand Down Expand Up @@ -819,28 +819,15 @@ def isdir(self, path: str) -> bool:
key = key.rstrip("/") + "/"

try:
return retryable_func_executor(
lambda: self.tos_client.head_object(bucket, key) and True,
resp = retryable_func_executor(
lambda: self.tos_client.get_file_status(bucket, key),
max_retry_num=self.max_retry_num,
)
except TosClientError as e:
raise e
return resp.key is not None and is_dir(resp.key)
except TosServerError as e:
if e.status_code == TOS_SERVER_STATUS_CODE_NOT_FOUND:
out = retryable_func_executor(
lambda: self.tos_client.list_objects_type2(
bucket,
prefix=key,
delimiter="/",
max_keys=1,
),
max_retry_num=self.max_retry_num,
)
return out.key_count > 0
else:
raise e
except Exception as e:
raise TosfsError(f"Tosfs failed with unknown error: {e}") from e
return False
raise e

def isfile(self, path: str) -> bool:
"""Check if the path is a file.
Expand Down

0 comments on commit 5bd6337

Please sign in to comment.