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

adding file obj upload function for s3 #136

Open
wants to merge 4 commits into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion basepair/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Exposing the storage wrapper

__title__ = 'basepair'
__version__ = '2.1.11'
__version__ = '2.1.12b'
__copyright__ = 'Copyright [2017] - [2024] Basepair INC'


Expand Down
25 changes: 25 additions & 0 deletions basepair/modules/aws/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,31 @@ def upload_file(self, file_name, full_path, extra_args=None, force=False, show_l
'path': full_path,
'url': self.get_self_signed(full_path),
}

def upload_file_obj(self, file_obj, full_path, extra_args=None, force=False, show_log=True):
'''Upload a file to an S3 bucket'''
extra_args = extra_args or {}

# Upload the file
try:
if force or not self.get_object_head(full_path, show_log=show_log):
self.client.upload_fileobj(file_obj, self.bucket, full_path, ExtraArgs=extra_args)
else:
print(f'Skipping file {full_path} because already exist in S3.')
except ClientError as error:
response = self.get_log_msg({
'exception': error,
'msg': f'Not able to upload file {file_obj}.',
})
if ExceptionHandler.is_throttled_error(exception=error):
raise error
return response

# get url by key
return {
'path': full_path,
'url': self.get_self_signed(full_path),
}

@staticmethod
def convert_to_v2(rules):
Expand Down
4 changes: 4 additions & 0 deletions basepair/modules/storage/drivers/aws_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,7 @@ def set_lifecycle(self, **kwargs):
def upload(self, file_name, full_path, **kwargs):
"""Upload file to storage"""
return self.s3_service.upload_file(file_name, full_path, **kwargs)

def upload_file_obj(self, fileobj, file_path, **kwargs):
"""Upload file object to storage"""
return self.s3_service.upload_file_obj(fileobj, file_path, **kwargs)
4 changes: 4 additions & 0 deletions basepair/modules/storage/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@ def set_lifecycle(self, **kwargs):
def upload(self, file_name, full_path, **kwargs):
"""Upload file to storage"""
return self.driver.upload(file_name, full_path, **kwargs)

def upload_file_obj(self, file_obj, file_path, **kwargs):
"""Upload file object to storage"""
return self.driver.upload_file_obj(file_obj, file_path, **kwargs)
Loading