Commit a522f6ff authored by Elouan Martinet's avatar Elouan Martinet
Browse files

[storages] Regenerate the key on each update

parent 55de1e24ea46
Pipeline #36616 passed with stage
in 1 minute and 44 seconds
......@@ -84,9 +84,8 @@ class S3Storage(Storage):
entity.cw_edited.edited_attribute(attr, None)
self.entity_deleted(entity, attr)
else:
key = self.get_s3_key(entity, attr)
if key is None:
key = self.new_s3_key(entity, attr)
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')))
......@@ -108,6 +107,9 @@ class S3Storage(Storage):
(self, key, entity.eid, attr))
self.info('Temporary uploaded %s.%s (%s/%s) to S3',
entity.eid, attr, self.bucket, suffixed_key)
if oldkey is not None and oldkey != key:
# remove unneeded old file
self.delay_deletion(entity, attr, oldkey)
return binary
def get_upload_extra_args(self, _entity, _attr, _key):
......@@ -123,11 +125,14 @@ class S3Storage(Storage):
def entity_deleted(self, entity, attr):
"""an entity using this storage for attr has been deleted"""
key = self.get_s3_key(entity, attr)
if key is None:
# no key to remove
return
self.delay_deletion(entity, attr, key)
def delay_deletion(self, entity, attr, key):
if entity._cw.repo.config['s3-auto-delete']:
key = self.get_s3_key(entity, attr)
if key is None:
# no key to remove
return
# delete key in a post commit event
S3DeleteFileOp.get_instance(entity._cw).add_data(
(self, key, entity.eid, attr))
......
......@@ -58,14 +58,14 @@ class S3StorageTC(testing.S3StorageTestMixin, CubicWebTC):
with self.admin_access.client_cnx() as cnx:
eid = create_image(cnx, b'some content').eid
cnx.commit()
key = cnx.execute('Any STKEY(D) WHERE X is Image, X data D, '
'X eid %(eid)s', {'eid': eid}).rows[0][0]
key = key.getvalue().decode()
with self.admin_access.client_cnx() as cnx:
fobj = cnx.find('Image', eid=eid).one()
fobj.cw_set(data=Binary(b'something else'))
cnx.commit()
# retrieve key now as it will have changed by the modification
key = cnx.execute('Any STKEY(D) WHERE X is Image, X data D, '
'X eid %(eid)s', {'eid': eid}).rows[0][0]
key = key.getvalue().decode()
data = self.s3_bucket.Object(key).get()['Body'].read()
self.assertEqual(data, b'something else')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment