Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
chore: s3_loader tests working.
  • Loading branch information
gcavalcante8808 committed Apr 4, 2022
commit 6e3041048954b10617597b86c683fa71b9aae1b1
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ def readme():
install_requires=[
'python-dateutil',
'thumbor>=7.0.0a2,<8',
'aiobotocore==0.12.0',
'aiobotocore==2.2.0',
],
extras_require={
'tests': [
'coverage',
'moto[server]',
'mock',
'pytest',
'pytest-asyncio'
],
},
)
59 changes: 30 additions & 29 deletions tc_aws/aws/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Use of this source code is governed by the MIT license that can be
# found in the LICENSE file.

import aiobotocore
import aiobotocore.session
from botocore.client import Config
from thumbor.utils import logger
from thumbor.engines import BaseEngine
Expand Down Expand Up @@ -44,13 +44,8 @@ def __init__(self, bucket, region, endpoint, max_retry=None):
)
)

if self._client is None:
self._client = aiobotocore.get_session().create_client(
's3',
region_name=region,
endpoint_url=endpoint,
config=config
)
self.region_name = region
self.endpoint_url = endpoint

async def exists(self, path):
"""
Expand All @@ -71,11 +66,12 @@ async def get(self, path):
Returns object at given path
:param string path: Path or 'key' to retrieve AWS object
"""

return await self._client.get_object(
Bucket=self._bucket,
Key=self._clean_key(path),
)
async with aiobotocore.session.get_session().create_client('s3', region_name=self.region_name,
endpoint_url=self.endpoint_url) as s3_client:
return await s3_client.get_object(
Bucket=self._bucket,
Key=self._clean_key(path),
)

async def get_url(self, path, method='GET', expiry=3600):
"""
Expand All @@ -84,18 +80,19 @@ async def get_url(self, path, method='GET', expiry=3600):
:param string method: Method for requested URL
:param int expiry: URL validity time
"""
async with aiobotocore.session.get_session().create_client('s3', region_name=self.region_name,
endpoint_url=self.endpoint_url) as s3_client:
url = await s3_client.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': self._bucket,
'Key': self._clean_key(path),
},
ExpiresIn=expiry,
HttpMethod=method,
)

url = await self._client.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': self._bucket,
'Key': self._clean_key(path),
},
ExpiresIn=expiry,
HttpMethod=method,
)

return url
return url

async def put(self, path, data, metadata=None, reduced_redundancy=False, encrypt_key=False):
"""
Expand Down Expand Up @@ -123,17 +120,21 @@ async def put(self, path, data, metadata=None, reduced_redundancy=False, encrypt
if metadata is not None:
args['Metadata'] = metadata

return await self._client.put_object(**args)
async with aiobotocore.session.get_session().create_client('s3', region_name=self.region_name,
endpoint_url=self.endpoint_url) as s3_client:
return await s3_client.put_object(**args)

async def delete(self, path):
"""
Deletes key at given path
:param string path: Path or 'key' to delete
"""
return await self._client.delete_object(
Bucket=self._bucket,
Key=self._clean_key(path),
)
async with aiobotocore.session.get_session().create_client('s3', region_name=self.region_name,
endpoint_url=self.endpoint_url) as s3_client:
return await s3_client.delete_object(
Bucket=self._bucket,
Key=self._clean_key(path),
)

def _clean_key(self, path):
logger.debug('Cleaning key: {path!r}'.format(path=path))
Expand Down