Skip to content

Commit

Permalink
adding file obj upload function for s3
Browse files Browse the repository at this point in the history
  • Loading branch information
avishmehta68710 committed Sep 19, 2024
1 parent 908a25a commit 0ccb296
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
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.12'
__copyright__ = 'Copyright [2017] - [2024] Basepair INC'


Expand Down
28 changes: 28 additions & 0 deletions basepair/modules/aws/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,34 @@ 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 {}
mimetype, _ = mimetypes.guess_type(file_obj)
if mimetype:
extra_args['ContentType'] = mimetype

# 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(self, fileobj, key, **kwargs):
"""Upload file object to storage"""
return self.s3_service.upload_file_obj(fileobj, key, **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_fileobj(self, fileobj, uri):
"""Upload file object to storage"""
return self.driver.upload_fileobj(fileobj, uri)

0 comments on commit 0ccb296

Please sign in to comment.