Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage): add gcs as a storage client #958

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 52 additions & 3 deletions backend/chainlit/data/storage_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import TYPE_CHECKING, Optional, Dict, Union, Any
from azure.storage.filedatalake import DataLakeServiceClient, FileSystemClient, DataLakeFileClient, ContentSettings
import boto3 # type: ignore
from google.cloud import storage

if TYPE_CHECKING:
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential, TokenCredential
Expand Down Expand Up @@ -39,20 +40,68 @@ async def upload_file(self, object_key: str, data: Union[bytes, str], mime: str
class S3StorageClient(BaseStorageClient):
"""
Class to enable Amazon S3 storage provider

params:
bucket: Name of the S3 bucket
access_key: AWS access key
secret_key: AWS secret key
session_token: AWS session token
"""
def __init__(self, bucket: str):
def __init__(self, bucket: str, access_key: Optional[str] = None, secret_key: Optional[str] = None, session_token: Optional[str] = None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick trick here is to accept the s3 client itself as an optional arg, and if it's null make a default one. This allows true customization for the end user. For example, they may want to use the Stubber for unittests or pass in a botocore.config.Config to customize the behavior. Also may want to change the endpoint url to point to localstack etc etc.

try:
self.bucket = bucket
self.client = boto3.client("s3")
if access_key and secret_key and session_token:
self.client = boto3.client("s3", aws_access_key_id=access_key, aws_secret_access_key=secret_key, aws_session_token=session_token)
else:
self.client = boto3.client("s3")
logger.info("S3StorageClient initialized")
except Exception as e:
logger.warn(f"S3StorageClient initialization error: {e}")

async def upload_file(self, object_key: str, data: Union[bytes, str], mime: str = 'application/octet-stream', overwrite: bool = True) -> Dict[str, Any]:
async def upload_file(self, object_key: str, data: Union[bytes, str], mime: str = 'application/octet-stream', **kwargs) -> Dict[str, Any]:
"""
Upload file to S3 bucket

params:
object_key: Key to store the object in the bucket
data: Data to be stored
mime: Mime type of the object"""
try:
self.client.put_object(Bucket=self.bucket, Key=object_key, Body=data, ContentType=mime)
url = f"https://{self.bucket}.s3.amazonaws.com/{object_key}"
return {"object_key": object_key, "url": url}
except Exception as e:
logger.warn(f"S3StorageClient, upload_file error: {e}")
return {}

class GoogleCloudClient(BaseStorageClient):
"""
Class to enable Google Cloud Storage

params:
bucket: Name of the GCS bucket
"""
def __init__(self, bucket: str):
try:
self.client = storage.Client()
self.bucket = self.client.bucket(bucket)
logger.info("GoogleCloudClient initialized")
except Exception as e:
logger.warn(f"GoogleCloudClient initialization error: {e}")

async def upload_file(self, object_key: str, data: Union[bytes, str], mime: str = 'application/octet-stream', **kwargs) -> Dict[str, Any]:
"""
Upload file to GCS bucket

params:
object_key: Key to store the object in the bucket
data: Data to be stored
mime: Mime type of the object"""
try:
blob = self.bucket.blob(object_key)
blob.upload_from_string(data, content_type=mime)
url = f"https://storage.googleapis.com/{self.bucket.name}/{object_key}"
return {"object_key": object_key, "url": url}
except Exception as e:
logger.warn(f"GoogleCloudClient, upload_file error: {e}")
return {}
1 change: 1 addition & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ SQLAlchemy = "^2.0.28"
boto3 = "^1.34.73"
azure-identity = "^1.14.1"
azure-storage-file-datalake = "^12.14.0"
google-cloud-storage = "^2.16.0"

[build-system]
requires = ["poetry-core"]
Expand Down