Skip to content

Commit ef89bd4

Browse files
authored
reuse existing AioBotoCore session (#19)
1 parent 4620b81 commit ef89bd4

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,5 @@ test-results/
131131
cobertura.xml
132132
coverage
133133
cc-test-reporter
134+
135+
.vscode

tests/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from thumbor.importer import Importer
1616
from thumbor.testing import TestCase
1717

18-
from thumbor_aws.s3_client import S3Client
18+
import thumbor_aws.s3_client
1919

2020

2121
class BaseS3TestCase(TestCase):
@@ -106,7 +106,7 @@ def get_context(self) -> Context:
106106

107107
async def ensure_bucket(self):
108108
"""Ensures the test bucket is created"""
109-
s3client = S3Client(self.context)
109+
s3client = thumbor_aws.s3_client.S3Client(self.context)
110110
if self.context.config.THUMBOR_AWS_RUN_IN_COMPATIBILITY_MODE is True:
111111
s3client.configuration["region_name"] = self.config.TC_AWS_REGION
112112
s3client.configuration[

tests/test_loader.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from tornado.testing import gen_test
1717

1818
from tests import BaseS3TestCase
19-
from thumbor_aws.loader import load
19+
import thumbor_aws.loader
2020
from thumbor_aws.storage import Storage
2121

2222

@@ -41,23 +41,31 @@ async def test_can_load_file_from_s3(self):
4141
exists = await storage.exists(filepath)
4242
expect(exists).to_be_true()
4343

44-
result = await load(self.context, filepath)
44+
result = await thumbor_aws.loader.load(self.context, filepath)
4545

4646
expect(result.successful).to_be_true()
4747
expect(result.buffer).to_equal(expected)
4848
expect(result.metadata["size"]).to_equal(len(expected))
4949
expect(result.metadata["updated_at"]).not_to_be_null()
5050

51+
@gen_test
52+
async def test_result_false_when_file_not_in_s3(self):
53+
"""
54+
Verifies that result is false when image not present in S3
55+
"""
56+
await self.ensure_bucket()
57+
filepath = f"/test/can_put_file_{uuid4()}"
58+
59+
result = await thumbor_aws.loader.load(self.context, filepath)
60+
61+
expect(result.successful).to_be_false()
62+
5163

5264
@pytest.mark.usefixtures("test_images")
5365
class LoaderCompatibilityModeTestCase(LoaderTestCase):
5466
def get_config(self) -> Config:
5567
return self.get_compatibility_config()
5668

57-
@property
58-
def _prefix(self):
59-
return "/test-compat-st"
60-
6169
@property
6270
def bucket_name(self):
6371
"""Name of the bucket to put test files in"""
@@ -66,10 +74,10 @@ def bucket_name(self):
6674

6775
@pytest.mark.usefixtures("test_images")
6876
class EmptyBucketConfigLoaderTestCase(BaseS3TestCase):
69-
@property
70-
def bucket_name(self):
71-
"""Name of the bucket to put test files in"""
72-
return self.context.config.AWS_LOADER_BUCKET_NAME
77+
def get_config(self) -> Config:
78+
cfg = super().get_config()
79+
cfg.AWS_LOADER_BUCKET_NAME = ""
80+
return cfg
7381

7482
@gen_test
7583
async def test_can_load_file_from_s3(self):
@@ -86,14 +94,25 @@ async def test_can_load_file_from_s3(self):
8694
expect(exists).to_be_true()
8795

8896
filepath_with_bucket = (
89-
f"/{self.context.config.AWS_LOADER_BUCKET_NAME}{filepath}"
97+
f"/{self.context.config.AWS_STORAGE_BUCKET_NAME}{filepath}"
9098
)
9199

92-
self.context.config.AWS_LOADER_BUCKET_NAME = ""
93-
94-
result = await load(self.context, filepath_with_bucket)
100+
result = await thumbor_aws.loader.load(
101+
self.context, filepath_with_bucket
102+
)
95103

96104
expect(result.successful).to_be_true()
97105
expect(result.buffer).to_equal(expected)
98106
expect(result.metadata["size"]).to_equal(len(expected))
99107
expect(result.metadata["updated_at"]).not_to_be_null()
108+
109+
110+
@pytest.mark.usefixtures("test_images")
111+
class LoaderNoPrefixTestCase(LoaderTestCase):
112+
def get_config(self) -> Config:
113+
cfg = super().get_config()
114+
cfg.AWS_LOADER_BUCKET_NAME = "test-bucket-loader-no-prefix"
115+
cfg.AWS_STORAGE_BUCKET_NAME = "test-bucket-loader-no-prefix"
116+
cfg.AWS_LOADER_ROOT_PATH = ""
117+
cfg.AWS_STORAGE_ROOT_PATH = ""
118+
return cfg

thumbor_aws/s3_client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Copyright (c) 2021 Bernardo Heynemann heynemann@gmail.com
1010

1111
import datetime
12-
from typing import Any, Dict, Mapping, Optional
12+
from typing import Any, Dict, Mapping, Optional, Tuple
1313

1414
from aiobotocore.client import AioBaseClient
1515
from aiobotocore.session import AioSession, get_session
@@ -84,9 +84,9 @@ def file_acl(self) -> str:
8484
@property
8585
def session(self) -> AioSession:
8686
"""Singleton Session used for connecting with AWS"""
87-
if self.__session is None:
88-
self.__session = get_session()
89-
return self.__session
87+
if S3Client.__session is None:
88+
S3Client.__session = get_session()
89+
return S3Client.__session
9090

9191
def get_client(self) -> AioBaseClient:
9292
"""Gets a connected client to use for S3"""
@@ -145,7 +145,7 @@ async def upload(
145145

146146
async def get_data(
147147
self, bucket: str, path: str, expiration: int = _default
148-
) -> (int, bytes, bytes, Optional[datetime.datetime]):
148+
) -> Tuple[int, bytes, bytes, Optional[datetime.datetime]]:
149149
"""Gets an object's data from S3"""
150150
async with self.get_client() as client:
151151
try:
@@ -212,7 +212,7 @@ async def get_body(self, response: Any) -> bytes:
212212
async with response["Body"] as stream:
213213
return await stream.read()
214214

215-
def _get_bucket_and_path(self, path) -> (str, str):
215+
def _get_bucket_and_path(self, path) -> Tuple[str, str]:
216216
bucket = self.bucket_name
217217
real_path = path
218218
if not self.bucket_name:

0 commit comments

Comments
 (0)