Skip to content

Commit

Permalink
Clean s3 file name and remove trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
said-moj committed Aug 10, 2023
1 parent c122d47 commit ca634d0
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions cla_backend/libs/aws/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ class StaticS3Storage(S3Boto3Storage):


class ReportsS3:
@classmethod
def clean_name(cls, name):
return name.strip("/")

@classmethod
def get_s3_connection(cls, bucket_name):
return S3Boto3Storage(bucket=bucket_name)

@classmethod
def download_file(cls, bucket_name, key):
try:
obj = cls.get_s3_connection(bucket_name).bucket.Object(key)
obj = cls.get_s3_connection(bucket_name).bucket.Object(cls.clean_name(key))
data = NamedTemporaryFile()
obj.download_fileobj(data)
data.seek(0)
Expand All @@ -29,11 +33,11 @@ def download_file(cls, bucket_name, key):

@classmethod
def save_file(cls, bucket_name, key, path):
cls.get_s3_connection(bucket_name).bucket.Object(key).upload_file(path)
cls.get_s3_connection(bucket_name).bucket.Object(cls.clean_name(key)).upload_file(path)

@classmethod
def delete_file(cls, bucket_name, key):
cls.get_s3_connection(bucket_name).delete(key)
cls.get_s3_connection(bucket_name).delete(cls.clean_name(key))

@classmethod
def save_data_to_bucket(cls, bucket_name, key, content):
Expand Down

0 comments on commit ca634d0

Please sign in to comment.