Skip to content

Commit

Permalink
Core: Support append mode for write API (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghua authored Sep 9, 2024
1 parent 330abd7 commit 51496f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tosfs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,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:
Expand Down
16 changes: 16 additions & 0 deletions tosfs/tests/test_tosfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,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"
Expand Down

0 comments on commit 51496f7

Please sign in to comment.