From 81a408b3d1674443fe76649dcf52cc1b6c645dcd Mon Sep 17 00:00:00 2001 From: yanghua Date: Mon, 9 Sep 2024 11:46:04 +0800 Subject: [PATCH] Core: Support append mode for write API --- tosfs/core.py | 11 +++++++++++ tosfs/tests/test_tosfs.py | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/tosfs/core.py b/tosfs/core.py index 1534909..6f770d4 100644 --- a/tosfs/core.py +++ b/tosfs/core.py @@ -1700,6 +1700,17 @@ def __init__( self.append_block = False self.buffer: Optional[io.BytesIO] = io.BytesIO() + if "a" in mode and fs.exists(path): + head = self.fs.tos_client.head_object(bucket, key) + loc = head.content_length + + if loc < 5 * 2**20: + # existing file too small for multi-upload: download + self.write(self.fs.cat(self.path)) + else: + self.append_block = True + self.loc = loc + def _initiate_upload(self) -> None: """Create remote file/upload.""" if self.autocommit and not self.append_block and self.tell() < self.blocksize: diff --git a/tosfs/tests/test_tosfs.py b/tosfs/tests/test_tosfs.py index 9c2e89a..cafcbbd 100644 --- a/tosfs/tests/test_tosfs.py +++ b/tosfs/tests/test_tosfs.py @@ -619,6 +619,22 @@ def test_file_write_mpu( ) +def test_file_write_append( + tosfs: TosFileSystem, bucket: str, temporary_workspace: str +) -> None: + file_name = random_str() + content = "hello world" + with tosfs.open(f"{bucket}/{temporary_workspace}/{file_name}", "w") as f: + f.write(content) + with tosfs.open(f"{bucket}/{temporary_workspace}/{file_name}", "a") as f: + f.write(content) + assert tosfs.info(f"{bucket}/{temporary_workspace}/{file_name}")["size"] == 2 * len( + content + ) + with tosfs.open(f"{bucket}/{temporary_workspace}/{file_name}", "r") as f: + assert f.read() == content + content + + def test_file_read(tosfs: TosFileSystem, bucket: str, temporary_workspace: str) -> None: file_name = random_str() content = "hello world"