[PATCH 1 of 2 s3storage] [storages] Make S3 key pattern configurable

Elouan Martinet elouan.martinet at logilab.fr
Wed Feb 12 13:48:29 CET 2020


# HG changeset patch
# User Elouan Martinet <elouan.martinet at logilab.fr>
# Date 1581510799 -3600
#      Wed Feb 12 13:33:19 2020 +0100
# Node ID 3186594e47be27e75596cc716730b902c6c6598a
# Parent  c35e1a12698152addf3dec053db579af0e3bf4c0
# EXP-Topic s3-key-pattern
[storages] Make S3 key pattern configurable

diff -r c35e1a126981 -r 3186594e47be cubicweb_s3storage/site_cubicweb.py
--- a/cubicweb_s3storage/site_cubicweb.py	Wed Jan 29 16:53:01 2020 +0100
+++ b/cubicweb_s3storage/site_cubicweb.py	Wed Feb 12 13:33:19 2020 +0100
@@ -10,6 +10,12 @@
         'help': 'Delete S3 objects on entity deletion',
         'group': 's3',
         'level': 2}),
+    ('s3-key-pattern', {
+        'type': 'string',
+        'default': '$uuid',
+        'help': 'Define how S3 keys are generated',
+        'group': 's3',
+        'level': 2}),
     )
 
 
diff -r c35e1a126981 -r 3186594e47be cubicweb_s3storage/storages.py
--- a/cubicweb_s3storage/storages.py	Wed Jan 29 16:53:01 2020 +0100
+++ b/cubicweb_s3storage/storages.py	Wed Feb 12 13:33:19 2020 +0100
@@ -21,9 +21,10 @@
 import uuid
 from logging import getLogger
 
-from six import PY3
+from six import PY3, text_type
 import os
 import boto3
+from string import Template
 
 from cubicweb import Binary, set_log_methods
 from cubicweb.server.sources.storages import Storage
@@ -133,10 +134,15 @@
         return self.new_s3_key(entity, attr)
 
     def new_s3_key(self, entity, attr):
-        """Generate a new key for given entity attr.
-
-        This implemenation just return a random UUID"""
-        return str(uuid.uuid1())
+        """Generate a new key for given entity attr."""
+        key_pattern = entity._cw.repo.config.get('s3-key-pattern', '$uuid')
+        template = Template(key_pattern)
+        return template.substitute({
+            'attr': attr,
+            'eid': text_type(entity.eid),
+            'etype': entity.cw_etype,
+            'uuid': uuid.uuid1(),  # FIXME generates uuid even if unused
+        })
 
 
 set_log_methods(S3Storage,



More information about the cubicweb-devel mailing list