Skip to content

Commit 7f9a1da

Browse files
committed
6.0.0.4
1 parent d60be24 commit 7f9a1da

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

pynamodb_mate/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "6.0.0.3"
1+
__version__ = "6.0.0.4"
22

33
if __name__ == "__main__": # pragma: no cover
44
print(__version__)

pynamodb_mate/helpers.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def batch_delete_s3_objects(
9191

9292
groups = group_by(pairs, get_key=lambda x: x[0])
9393
for bucket, bucket_key_pairs in groups.items():
94-
s3_client.delete_objects(
95-
Bucket=bucket,
96-
Delete=dict(Objects=[dict(Key=key) for _, key in bucket_key_pairs]),
97-
)
94+
if len(bucket_key_pairs):
95+
s3_client.delete_objects(
96+
Bucket=bucket,
97+
Delete=dict(Objects=[dict(Key=key) for _, key in bucket_key_pairs]),
98+
)

pynamodb_mate/patterns/large_attribute/impl.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ def get_s3_key(
4646
prefix: str,
4747
) -> str:
4848
"""
49+
Figure out the S3 key location for the large attribute based on the DynamoDB
50+
item's partition key, sort key, attribute name, and the value of the attribute.
51+
4952
:param pk: partition key.
5053
:param sk: sort key, use None if no sort key.
5154
:param attr: large attribute name.
5255
:param value: large attribute value in binary format.
5356
:param prefix: common S3 prefix.
5457
5558
:return: example "${prefix}/pk={pk}/sk={sk}/attr={attr}/md5={md5}"
56-
5759
"""
5860
parts = list()
5961
if prefix: # pragma: no cover
@@ -71,6 +73,18 @@ def get_s3_key(
7173
return "/".join(parts)
7274

7375

76+
T_S3_KEY_GETTER = T.Callable[
77+
[
78+
T.Union[str, int],
79+
T.Optional[T.Union[str, int]],
80+
str,
81+
bytes,
82+
str,
83+
],
84+
str,
85+
]
86+
87+
7488
def split_s3_uri(s3_uri: str) -> T.Tuple[str, str]:
7589
parts = s3_uri.split("/", 3)
7690
return parts[2], parts[3]
@@ -224,6 +238,7 @@ def put_s3(
224238
prefix: str,
225239
update_at: datetime,
226240
s3_put_object_kwargs: T.Optional[T.Dict[str, T.Dict[str, T.Any]]] = None,
241+
s3_key_getter: T_S3_KEY_GETTER = get_s3_key,
227242
) -> PutS3Response:
228243
"""
229244
Put large attribute data to S3.
@@ -263,7 +278,7 @@ def put_s3(
263278
s3_put_object_kwargs = dict()
264279
put_s3_response = PutS3Response(actions=[])
265280
for attr, value in kvs.items():
266-
s3_key = get_s3_key(pk=pk, sk=sk, attr=attr, value=value, prefix=prefix)
281+
s3_key = s3_key_getter(pk, sk, attr, value, prefix)
267282
s3_uri = join_s3_uri(bucket, s3_key)
268283
if is_s3_object_exists(s3_client, bucket=bucket, key=s3_key):
269284
put_executed = False
@@ -303,6 +318,7 @@ def create_large_attribute_item(
303318
prefix: str,
304319
update_at: datetime,
305320
s3_put_object_kwargs: T.Optional[T.Dict[str, T.Dict[str, T.Any]]] = None,
321+
s3_key_getter: T_S3_KEY_GETTER = get_s3_key,
306322
attributes: T.Optional[T.Dict[str, T.Any]] = None,
307323
clean_up_when_failed: bool = True,
308324
_error: T.Optional[Exception] = None,
@@ -340,6 +356,7 @@ def create_large_attribute_item(
340356
prefix=prefix,
341357
update_at=update_at,
342358
s3_put_object_kwargs=s3_put_object_kwargs,
359+
s3_key_getter=s3_key_getter,
343360
)
344361
try:
345362
# this is for unit test purpose to simulate the DynamoDB operation failed
@@ -370,6 +387,7 @@ def update_large_attribute_item(
370387
prefix: str,
371388
update_at: datetime,
372389
s3_put_object_kwargs: T.Optional[T.Dict[str, T.Dict[str, T.Any]]] = None,
390+
s3_key_getter: T_S3_KEY_GETTER = get_s3_key,
373391
update_actions: T.Optional[T.List[Action]] = None,
374392
consistent_read: bool = False,
375393
clean_up_when_succeeded: bool = True,
@@ -412,6 +430,7 @@ def update_large_attribute_item(
412430
prefix=prefix,
413431
update_at=update_at,
414432
s3_put_object_kwargs=s3_put_object_kwargs,
433+
s3_key_getter=s3_key_getter,
415434
)
416435
got_old_model = (
417436
False # IDE show warning that this line is useless, but we do need it

release-history.rst

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ Backlog
1717
**Miscellaneous**
1818

1919

20+
6.0.0.4 (2024-07-19)
21+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22+
**Minor Improvements**
23+
24+
- Now most of :meth:`pynamodb_mate.patterns.large_attribute.impl.LargeAttributeMixin` methods allow to customize the ``s3_key_getter`` function. So that developer can customize how to convert a DynamoDB item into an S3 key for large attribute
25+
26+
2027
6.0.0.3 (2024-06-06)
2128
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2229
**Bugfixes**

0 commit comments

Comments
 (0)