-
Notifications
You must be signed in to change notification settings - Fork 17
/
result_storage.py
215 lines (180 loc) · 6.21 KB
/
result_storage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/python
# -*- coding: utf-8 -*-
# thumbor aws extensions
# https://github.com/thumbor/thumbor-aws
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2021 Bernardo Heynemann heynemann@gmail.com
from datetime import datetime, timezone
from deprecated import deprecated
from thumbor.engines import BaseEngine
from thumbor.result_storages import BaseStorage, ResultStorageResult
from thumbor.utils import logger
from thumbor_aws.config import Config
from thumbor_aws.s3_client import S3Client
from thumbor_aws.utils import normalize_path
Config.define(
"AWS_RESULT_STORAGE_REGION_NAME",
"us-east-1",
"Region where thumbor's objects are going to be stored.",
"AWS Result Storage",
)
Config.define(
"AWS_RESULT_STORAGE_BUCKET_NAME",
"thumbor",
"S3 Bucket where thumbor's objects are going to be stored.",
"AWS Result Storage",
)
Config.define(
"AWS_RESULT_STORAGE_S3_SECRET_ACCESS_KEY",
None,
"Secret access key for S3 to allow thumbor to store objects there.",
"AWS Result Storage",
)
Config.define(
"AWS_RESULT_STORAGE_S3_ACCESS_KEY_ID",
None,
"Access key ID for S3 to allow thumbor to store objects there.",
"AWS Result Storage",
)
Config.define(
"AWS_RESULT_STORAGE_S3_ENDPOINT_URL",
None,
"Endpoint URL for S3 API. Very useful for testing.",
"AWS Result Storage",
)
Config.define(
"AWS_RESULT_STORAGE_ROOT_PATH",
"/rs",
"Result Storage prefix path.",
"AWS Result Storage",
)
Config.define(
"AWS_RESULT_STORAGE_S3_ACL",
None,
"ACL to use for storing items in S3.",
"AWS Result Storage",
)
class Storage(BaseStorage, S3Client):
def __init__(self, context):
BaseStorage.__init__(self, context)
S3Client.__init__(self, context)
if self.compatibility_mode:
self.configuration["region_name"] = self.config.TC_AWS_REGION
self.configuration["endpoint_url"] = self.config.TC_AWS_ENDPOINT
self.configuration[
"bucket_name"
] = self.config.TC_AWS_RESULT_STORAGE_BUCKET
self.configuration[
"root_path"
] = self.config.TC_AWS_RESULT_STORAGE_ROOT_PATH
@property
def region_name(self) -> str:
return self.configuration.get(
"region_name", self.context.config.AWS_RESULT_STORAGE_REGION_NAME
)
@property
def secret_access_key(self) -> str:
return self.configuration.get(
"secret_access_key",
self.context.config.AWS_RESULT_STORAGE_S3_SECRET_ACCESS_KEY,
)
@property
def access_key_id(self) -> str:
return self.configuration.get(
"access_key_id",
self.context.config.AWS_RESULT_STORAGE_S3_ACCESS_KEY_ID,
)
@property
def endpoint_url(self) -> str:
return self.configuration.get(
"endpoint_url",
self.context.config.AWS_RESULT_STORAGE_S3_ENDPOINT_URL,
)
@property
def bucket_name(self) -> str:
return self.configuration.get(
"bucket_name",
self.context.config.AWS_RESULT_STORAGE_BUCKET_NAME,
)
@property
def file_acl(self) -> str:
return self.configuration.get(
"file_acl",
self.context.config.AWS_RESULT_STORAGE_S3_ACL,
)
@property
def root_path(self) -> str:
"""Defines the path prefix for all result storage images in S3"""
return self.configuration.get(
"root_path",
self.context.config.AWS_RESULT_STORAGE_ROOT_PATH,
)
async def put(self, image_bytes: bytes) -> str:
file_abspath = normalize_path(self.prefix, self.context.request.url)
logger.debug("[RESULT_STORAGE] putting at %s", file_abspath)
content_type = BaseEngine.get_mimetype(image_bytes)
response = await self.upload(
file_abspath,
image_bytes,
content_type,
self.context.config.AWS_DEFAULT_LOCATION,
)
logger.info(
"[RESULT_STORAGE] Image uploaded successfully to %s", file_abspath
)
return response
@property
def is_auto_webp(self) -> bool:
"""
Identifies the current request if it's
being auto converted to webp
"""
return (
self.context.config.AUTO_WEBP and self.context.request.accepts_webp
)
@property
def prefix(self) -> str:
return self.root_path + ("/auto_webp" if self.is_auto_webp else "")
async def get(self) -> ResultStorageResult:
path = self.context.request.url
file_abspath = normalize_path(self.prefix, path)
logger.debug("[RESULT_STORAGE] getting from %s", file_abspath)
exists = await self.object_exists(file_abspath)
if not exists:
logger.debug(
"[RESULT_STORAGE] image not found at %s", file_abspath
)
return None
status, body, last_modified = await self.get_data(
self.bucket_name, file_abspath
)
if status != 200 or self._is_expired(last_modified):
logger.debug(
"[RESULT_STORAGE] cached image has expired (status %s)", status
)
return None
logger.info(
"[RESULT_STORAGE] Image retrieved successfully at %s.",
file_abspath,
)
return ResultStorageResult(
buffer=body,
metadata={
"LastModified": last_modified.replace(tzinfo=timezone.utc),
"ContentLength": len(body),
"ContentType": BaseEngine.get_mimetype(body),
},
)
@deprecated(version="7.0.0", reason="Use result's last_modified instead")
async def last_updated( # pylint: disable=invalid-overridden-method
self,
) -> datetime:
path = self.context.request.url
file_abspath = normalize_path(self.prefix, path)
logger.debug("[RESULT_STORAGE] getting from %s", file_abspath)
response = await self.get_object_metadata(file_abspath)
return datetime.strptime(
response["ResponseMetadata"]["HTTPHeaders"]["last-modified"],
"%a, %d %b %Y %H:%M:%S %Z",
)