Skip to content

Commit

Permalink
Use try/with block when reading response['Body']
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmy Schleicher authored and peterrus committed Feb 23, 2022
1 parent 152863e commit 490941e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion tc_aws/loaders/s3_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ async def load(context, url):
return result

result.successful = True
result.buffer = await file_key['Body'].read()
async with file_key['Body'] as stream:
result.buffer = await stream.read()

result.metadata.update(
size=file_key['ContentLength'],
Expand Down
3 changes: 2 additions & 1 deletion tc_aws/result_storages/s3_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ async def get(self, path = None):
return None

result = ResultStorageResult()
result.buffer = await key['Body'].read()
async with key['Body'] as stream:
result.buffer = await stream.read()
result.successful = True

result.metadata = {
Expand Down
9 changes: 6 additions & 3 deletions tc_aws/storages/s3_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ async def get_crypto(self, path):
logger.warn("[STORAGE] s3 key not found at %s" % crypto_path)
return None

file_key = await file_key['Body'].read()
async with file_key['Body'] as stream:
file_key = await stream.read()

return file_key.decode('utf-8')

Expand All @@ -129,7 +130,8 @@ async def get_detector_data(self, path):
if not file_key or self.is_expired(file_key) or 'Body' not in file_key:
return None

return loads(await file_key['Body'].read())
async with file_key['Body'] as stream:
return loads(await stream.read())

async def get(self, path):
"""
Expand All @@ -142,7 +144,8 @@ async def get(self, path):
except BotoCoreError:
return None

return await file['Body'].read()
async with file['Body'] as stream:
return await stream.read()

async def exists(self, path):
"""
Expand Down

0 comments on commit 490941e

Please sign in to comment.