Skip to content

Commit

Permalink
Add remove operations for s3 and files
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekuruu committed Sep 22, 2023
1 parent 30e4a8a commit c2b77c7
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import config
import boto3
import utils
import os
import io

class Storage:
Expand Down Expand Up @@ -252,6 +253,14 @@ def cache_replay(self, id: int, content: bytes):
expiry=timedelta(hours=5)
)

def remove_replay(self, id: int):
self.logger.debug(f'Removing replay with id "{id}"...')

if not config.S3_ENABLED:
return self.remove_file(f'/replays/{id}')
else:
return self.remove_from_s3('replays', str(id))

def get_presigned_url(self, bucket: str, key: str, expiration: int = 900) -> Optional[str]:
if not config.S3_ENABLED:
return
Expand Down Expand Up @@ -295,6 +304,18 @@ def save_to_s3(self, content: bytes, key: str, bucket: str) -> bool:

return True

def remove_from_s3(self, bucket: str, key: str) -> bool:
try:
self.s3.delete_object(
Bucket=bucket,
Key=key
)
except Exception as e:
self.logger.error(f'Failed to remove "{key}" from {bucket}: "{e}"')
return False

return True

def get_from_cache(self, name: str) -> Optional[bytes]:
return self.cache.get(name)

Expand All @@ -305,6 +326,15 @@ def get_file_content(self, filepath: str) -> Optional[bytes]:
except Exception as e:
self.logger.error(f'Failed to read file "{filepath}": {e}')

def remove_file(self, filepath: str) -> bool:
try:
os.remove(f'{config.DATA_PATH}/{filepath}')
except Exception as e:
self.logger.error(f'Failed to file "{filepath}": "{e}"')
return False

return True

def get_from_s3(self, key: str, bucket: str) -> Optional[bytes]:
buffer = io.BytesIO()

Expand Down

0 comments on commit c2b77c7

Please sign in to comment.