Skip to content

Commit

Permalink
fix: add_obj_to_bucket s3 method all args
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Nov 21, 2023
1 parent 72ef564 commit 9e26166
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/backend/app/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import sys
from io import BytesIO
from typing import Any

from loguru import logger as log
from minio import Minio
Expand Down Expand Up @@ -36,16 +37,31 @@ def add_file_to_bucket(bucket_name: str, file_path: str, s3_path: str):
client.fput_object(bucket_name, file_path, s3_path)


def add_obj_to_bucket(bucket_name: str, file_obj: BytesIO, s3_path: str):
def add_obj_to_bucket(
bucket_name: str,
file_obj: BytesIO,
s3_path: str,
content_type: str = "application/octet-stream",
**kwargs: dict[str, Any],
):
"""Upload a BytesIO object to an S3 bucket.
Args:
bucket_name (str): The name of the S3 bucket.
file_obj (BytesIO): A BytesIO object containing the data to be uploaded.
s3_path (str): The path in the S3 bucket where the data will be stored.
content_type (str, optional): The content type of the uploaded file.
Default application/octet-stream.
kwargs (dict[str, Any]): Any other arguments to pass to client.put_object.
"""
client = s3_client()
result = client.put_object(bucket_name, file_obj, s3_path)
# Set BytesIO object to start, prior to .read()
file_obj.seek(0)

result = client.put_object(
bucket_name, s3_path, file_obj, file_obj.getbuffer().nbytes, **kwargs
)
log.debug(
f"Created {result.object_name} object; etag: {result.etag}, "
f"version-id: {result.version_id}"
Expand Down

0 comments on commit 9e26166

Please sign in to comment.