Skip to content

Commit

Permalink
Better AWS S3 storage support (#4195)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbren authored Oct 3, 2024
1 parent 4264974 commit 641a153
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions openhands/storage/s3.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
import io
import os

from minio import Minio

from openhands.storage.files import FileStore

AWS_S3_ENDPOINT = 's3.amazonaws.com'


class S3FileStore(FileStore):
def __init__(self, endpoint: str = AWS_S3_ENDPOINT) -> None:
def __init__(self) -> None:
access_key = os.getenv('AWS_ACCESS_KEY_ID')
secret_key = os.getenv('AWS_SECRET_ACCESS_KEY')
endpoint = os.getenv('AWS_S3_ENDPOINT', 's3.amazonaws.com')
secure = os.getenv('AWS_S3_SECURE', 'true').lower() == 'true'
self.bucket = os.getenv('AWS_S3_BUCKET')
self.client = Minio(endpoint, access_key, secret_key)
self.client = Minio(endpoint, access_key, secret_key, secure=secure)

def write(self, path: str, contents: str) -> None:
self.client.put_object(self.bucket, path, contents)
as_bytes = contents.encode('utf-8')
stream = io.BytesIO(as_bytes)
try:
self.client.put_object(self.bucket, path, stream, len(as_bytes))
except Exception as e:
raise FileNotFoundError(f'Failed to write to S3 at path {path}: {e}')

def read(self, path: str) -> str:
return self.client.get_object(self.bucket, path).data.decode('utf-8')
try:
return self.client.get_object(self.bucket, path).data.decode('utf-8')
except Exception as e:
raise FileNotFoundError(f'Failed to read from S3 at path {path}: {e}')

def list(self, path: str) -> list[str]:
return [obj.object_name for obj in self.client.list_objects(self.bucket, path)]
try:
return [
obj.object_name for obj in self.client.list_objects(self.bucket, path)
]
except Exception as e:
raise FileNotFoundError(f'Failed to list S3 objects at path {path}: {e}')

def delete(self, path: str) -> None:
self.client.remove_object(self.bucket, path)
try:
self.client.remove_object(self.bucket, path)
except Exception as e:
raise FileNotFoundError(f'Failed to delete S3 object at path {path}: {e}')

0 comments on commit 641a153

Please sign in to comment.