From 4c7a6acf8b3f253d22f632dc763630785a16a289 Mon Sep 17 00:00:00 2001 From: Patrick Tombez Date: Fri, 12 Nov 2021 11:46:43 +0100 Subject: [PATCH] [13.0][IMP] attachment_azure: Write and read storage name along filename for extended usability --- attachment_azure/README.rst | 3 +++ attachment_azure/models/ir_attachment.py | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/attachment_azure/README.rst b/attachment_azure/README.rst index f3837eb..f46a616 100644 --- a/attachment_azure/README.rst +++ b/attachment_azure/README.rst @@ -51,6 +51,9 @@ Activate Azure Blob storage: One container will be created per database using the `RUNNING_ENV` environment variable and the name of the database. By default, `RUNNING_ENV` is set to `dev`. +The container name will also be stored in the database for each attachment, +and will be used to access the right container in the storage. + This addon must be added in the server wide addons with (``--load`` option): ``--load=web,attachment_azure`` diff --git a/attachment_azure/models/ir_attachment.py b/attachment_azure/models/ir_attachment.py index 3b6fb3f..bd35d7f 100644 --- a/attachment_azure/models/ir_attachment.py +++ b/attachment_azure/models/ir_attachment.py @@ -127,8 +127,9 @@ class IrAttachment(models.Model): return str.lower(storage_name)[:63] @api.model - def _get_azure_container(self): - container_name = self._get_container_name() + def _get_azure_container(self, container_name=None): + if not container_name: + container_name = self._get_container_name() blob_service_client = self._get_blob_service_client() container_client = blob_service_client.get_container_client(container_name) try: @@ -144,8 +145,12 @@ class IrAttachment(models.Model): @api.model def _store_file_read(self, fname, bin_size=False): if fname.startswith("azure://"): - container_client = self._get_azure_container() key = fname.replace("azure://", "", 1).lower() + if '/' in key: + container_name, key = key.split('/') + else: + container_name = None + container_client = self._get_azure_container(container_name) try: blob_client = container_client.get_blob_client(key) read = blob_client.download_blob().readall() @@ -161,11 +166,11 @@ class IrAttachment(models.Model): location = self.env.context.get("storage_location") or self._storage() if location == "azure": container_client = self._get_azure_container() + filename = "azure://%s/%s" % (container_client.container_name, key) with io.BytesIO() as file: blob_client = container_client.get_blob_client(key.lower()) file.write(bin_data) file.seek(0) - filename = "azure://%s" % (key) try: blob_client.upload_blob(file, blob_type="BlockBlob") except ResourceExistsError: @@ -184,8 +189,12 @@ class IrAttachment(models.Model): @api.model def _store_file_delete(self, fname): if fname.startswith("azure://"): - container_client = self._get_azure_container() key = fname.replace("azure://", "", 1).lower() + if '/' in key: + container_name, key = key.split('/') + else: + container_name = None + container_client = self._get_azure_container(container_name) # delete the file only if it is on the current configured container # otherwise, we might delete files used on a different environment try: