Skip to content
Snippets Groups Projects
Commit fd6e8a70100a authored by Efflam Lemaillet's avatar Efflam Lemaillet :robot:
Browse files

feat: add version id to entity

Refs: #13
parent 8c41ae0c2e50
No related branches found
No related tags found
No related merge requests found
Pipeline #152566 passed with warnings
...@@ -87,10 +87,6 @@ ...@@ -87,10 +87,6 @@
else: else:
oldkey = self.get_s3_key(entity, attr) oldkey = self.get_s3_key(entity, attr)
key = self.new_s3_key(entity, attr) key = self.new_s3_key(entity, attr)
# save S3 key
entity.cw_edited.edited_attribute(
attr, Binary(key.encode('utf-8')))
# copy Binary value, workaround for boto3 bug # copy Binary value, workaround for boto3 bug
# https://github.com/boto/s3transfer/issues/80 # https://github.com/boto/s3transfer/issues/80
# .read() is required since Binary can't wrap itself # .read() is required since Binary can't wrap itself
...@@ -103,8 +99,8 @@ ...@@ -103,8 +99,8 @@
upload_key = key upload_key = key
extra_args = self.get_upload_extra_args(entity, attr, key) extra_args = self.get_upload_extra_args(entity, attr, key)
s3_object = self.bucket.put_object( put_object_result = self.s3cnx.put_object(Body=buffer,
Body=buffer, Bucket=self.bucket,
Key=upload_key, Key=upload_key,
**extra_args) **extra_args)
buffer.close() buffer.close()
...@@ -110,4 +106,7 @@ ...@@ -110,4 +106,7 @@
buffer.close() buffer.close()
version_id = put_object_result.get('VersionId', None)
# save S3 key
entity = self.save_s3_key(entity, attr, upload_key, version_id)
# when key is suffixed, move to final key in post commit event # when key is suffixed, move to final key in post commit event
# remove temporary key on rollback # remove temporary key on rollback
...@@ -160,6 +159,24 @@ ...@@ -160,6 +159,24 @@
source.doexec(cnx, sql, attrs) source.doexec(cnx, sql, attrs)
entity.cw_edited = None entity.cw_edited = None
def save_s3_key(self, entity, attr, key, version_id=None):
"""
Save the s3 key into the entity bytes attribute
"""
id_string = key
if entity._cw.repo.config['s3-activate-object-versioning'] and \
version_id is not None:
id_string = self.format_version_id_suffix(key, version_id)
entity.cw_edited.edited_attribute(attr,
Binary(id_string.encode('utf-8')))
return entity
def format_version_id_suffix(self, key, version_id):
"""
Format the string that will store key and version id
"""
return f"{key}#{version_id}"
def get_s3_key(self, entity, attr): def get_s3_key(self, entity, attr):
""" """
Return the S3 key of the S3 object storing the content of attribute Return the S3 key of the S3 object storing the content of attribute
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment