diff --git a/cubicweb_s3storage/storages.py b/cubicweb_s3storage/storages.py index 2be8dfd91689bb2cd01eebfb883826003b2380c3_Y3ViaWN3ZWJfczNzdG9yYWdlL3N0b3JhZ2VzLnB5..d3ee11166a5b2016f4f6d52c223e273306ed7d94_Y3ViaWN3ZWJfczNzdG9yYWdlL3N0b3JhZ2VzLnB5 100644 --- a/cubicweb_s3storage/storages.py +++ b/cubicweb_s3storage/storages.py @@ -33,6 +33,7 @@ class S3Storage(Storage): is_source_callback = True + KEY_SEPARATOR = '#' def __init__(self, bucket, suffix='.tmp'): self.s3cnx = self._s3_client() @@ -87,10 +88,6 @@ else: oldkey = self.get_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 # https://github.com/boto/s3transfer/issues/80 # .read() is required since Binary can't wrap itself @@ -108,6 +105,9 @@ Key=upload_key, **extra_args) 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 # remove temporary key on rollback @@ -160,6 +160,24 @@ source.doexec(cnx, sql, attrs) 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}{self.KEY_SEPARATOR}{version_id}" + def get_s3_key(self, entity, attr): """ Return the S3 key of the S3 object storing the content of attribute