Skip to content

Commit

Permalink
6.0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
MacHu-GWU committed Jul 19, 2024
1 parent d60be24 commit 7f9a1da
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pynamodb_mate/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "6.0.0.3"
__version__ = "6.0.0.4"

if __name__ == "__main__": # pragma: no cover
print(__version__)
9 changes: 5 additions & 4 deletions pynamodb_mate/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def batch_delete_s3_objects(

groups = group_by(pairs, get_key=lambda x: x[0])
for bucket, bucket_key_pairs in groups.items():
s3_client.delete_objects(
Bucket=bucket,
Delete=dict(Objects=[dict(Key=key) for _, key in bucket_key_pairs]),
)
if len(bucket_key_pairs):
s3_client.delete_objects(
Bucket=bucket,
Delete=dict(Objects=[dict(Key=key) for _, key in bucket_key_pairs]),
)
23 changes: 21 additions & 2 deletions pynamodb_mate/patterns/large_attribute/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ def get_s3_key(
prefix: str,
) -> str:
"""
Figure out the S3 key location for the large attribute based on the DynamoDB
item's partition key, sort key, attribute name, and the value of the attribute.
:param pk: partition key.
:param sk: sort key, use None if no sort key.
:param attr: large attribute name.
:param value: large attribute value in binary format.
:param prefix: common S3 prefix.
:return: example "${prefix}/pk={pk}/sk={sk}/attr={attr}/md5={md5}"
"""
parts = list()
if prefix: # pragma: no cover
Expand All @@ -71,6 +73,18 @@ def get_s3_key(
return "/".join(parts)


T_S3_KEY_GETTER = T.Callable[
[
T.Union[str, int],
T.Optional[T.Union[str, int]],
str,
bytes,
str,
],
str,
]


def split_s3_uri(s3_uri: str) -> T.Tuple[str, str]:
parts = s3_uri.split("/", 3)
return parts[2], parts[3]
Expand Down Expand Up @@ -224,6 +238,7 @@ def put_s3(
prefix: str,
update_at: datetime,
s3_put_object_kwargs: T.Optional[T.Dict[str, T.Dict[str, T.Any]]] = None,
s3_key_getter: T_S3_KEY_GETTER = get_s3_key,
) -> PutS3Response:
"""
Put large attribute data to S3.
Expand Down Expand Up @@ -263,7 +278,7 @@ def put_s3(
s3_put_object_kwargs = dict()
put_s3_response = PutS3Response(actions=[])
for attr, value in kvs.items():
s3_key = get_s3_key(pk=pk, sk=sk, attr=attr, value=value, prefix=prefix)
s3_key = s3_key_getter(pk, sk, attr, value, prefix)
s3_uri = join_s3_uri(bucket, s3_key)
if is_s3_object_exists(s3_client, bucket=bucket, key=s3_key):
put_executed = False
Expand Down Expand Up @@ -303,6 +318,7 @@ def create_large_attribute_item(
prefix: str,
update_at: datetime,
s3_put_object_kwargs: T.Optional[T.Dict[str, T.Dict[str, T.Any]]] = None,
s3_key_getter: T_S3_KEY_GETTER = get_s3_key,
attributes: T.Optional[T.Dict[str, T.Any]] = None,
clean_up_when_failed: bool = True,
_error: T.Optional[Exception] = None,
Expand Down Expand Up @@ -340,6 +356,7 @@ def create_large_attribute_item(
prefix=prefix,
update_at=update_at,
s3_put_object_kwargs=s3_put_object_kwargs,
s3_key_getter=s3_key_getter,
)
try:
# this is for unit test purpose to simulate the DynamoDB operation failed
Expand Down Expand Up @@ -370,6 +387,7 @@ def update_large_attribute_item(
prefix: str,
update_at: datetime,
s3_put_object_kwargs: T.Optional[T.Dict[str, T.Dict[str, T.Any]]] = None,
s3_key_getter: T_S3_KEY_GETTER = get_s3_key,
update_actions: T.Optional[T.List[Action]] = None,
consistent_read: bool = False,
clean_up_when_succeeded: bool = True,
Expand Down Expand Up @@ -412,6 +430,7 @@ def update_large_attribute_item(
prefix=prefix,
update_at=update_at,
s3_put_object_kwargs=s3_put_object_kwargs,
s3_key_getter=s3_key_getter,
)
got_old_model = (
False # IDE show warning that this line is useless, but we do need it
Expand Down
7 changes: 7 additions & 0 deletions release-history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ Backlog
**Miscellaneous**


6.0.0.4 (2024-07-19)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**Minor Improvements**

- 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


6.0.0.3 (2024-06-06)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**Bugfixes**
Expand Down

0 comments on commit 7f9a1da

Please sign in to comment.