-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better AWS S3 storage support (#4195)
- Loading branch information
Showing
1 changed file
with
25 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}') |