mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
Add bytes support to FileStore write operations (#6019)
Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
parent
ec70af9412
commit
510c1644dd
@ -5,7 +5,7 @@ class E2BFileStore(FileStore):
|
||||
def __init__(self, filesystem):
|
||||
self.filesystem = filesystem
|
||||
|
||||
def write(self, path: str, contents: str) -> None:
|
||||
def write(self, path: str, contents: str | bytes) -> None:
|
||||
self.filesystem.write(path, contents)
|
||||
|
||||
def read(self, path: str) -> str:
|
||||
|
||||
@ -3,7 +3,7 @@ from abc import abstractmethod
|
||||
|
||||
class FileStore:
|
||||
@abstractmethod
|
||||
def write(self, path: str, contents: str) -> None:
|
||||
def write(self, path: str, contents: str | bytes) -> None:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
|
||||
@ -12,7 +12,9 @@ class InMemoryFileStore(FileStore):
|
||||
def __init__(self, files: dict[str, str] = IN_MEMORY_FILES):
|
||||
self.files = files
|
||||
|
||||
def write(self, path: str, contents: str) -> None:
|
||||
def write(self, path: str, contents: str | bytes) -> None:
|
||||
if isinstance(contents, bytes):
|
||||
contents = contents.decode('utf-8')
|
||||
self.files[path] = contents
|
||||
|
||||
def read(self, path: str) -> str:
|
||||
|
||||
@ -15,8 +15,8 @@ class S3FileStore(FileStore):
|
||||
self.bucket = os.getenv('AWS_S3_BUCKET')
|
||||
self.client = Minio(endpoint, access_key, secret_key, secure=secure)
|
||||
|
||||
def write(self, path: str, contents: str) -> None:
|
||||
as_bytes = contents.encode('utf-8')
|
||||
def write(self, path: str, contents: str | bytes) -> None:
|
||||
as_bytes = contents.encode('utf-8') if isinstance(contents, str) else contents
|
||||
stream = io.BytesIO(as_bytes)
|
||||
try:
|
||||
self.client.put_object(self.bucket, path, stream, len(as_bytes))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user