9 azure backport (#353)

* fix: backport fix + backport modules to azure
This commit is contained in:
Vincent Renaville
2022-03-15 10:27:02 +01:00
committed by GitHub
co-authored by GitHub
parent 92bf8210ef
commit 61a7d392ff
19 changed files with 332 additions and 26 deletions
+37 -14
View File
@@ -129,25 +129,39 @@ class IrAttachment(models.Model):
return str.lower(storage_name)[:63]
@api.model
def _get_azure_container(self):
container_name = self._get_container_name()
blob_service_client = self._get_blob_service_client()
container_client = blob_service_client.get_container_client(container_name)
def _get_azure_container(self, container_name=None):
if not container_name:
container_name = self._get_container_name()
try:
# Create the container
container_client.create_container()
except ResourceExistsError:
pass
except HttpResponseError as error:
_logger.exception("Error during the creation of the Azure container")
raise exceptions.UserError(str(error))
blob_service_client = self._get_blob_service_client()
except exceptions.UserError:
_logger.exception(
"error accessing to storage '%s' please check credentials ",
container_name
)
return False
container_client = blob_service_client.get_container_client(container_name)
if not container_client.exists():
try:
# Create the container
container_client.create_container()
except HttpResponseError as error:
_logger.exception("Error during the creation of the Azure container")
raise exceptions.UserError(str(error))
return container_client
@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('/', 1)
else:
container_name = None
container_client = self._get_azure_container(container_name)
# if container cannot be retrived, abort reading from azure storage
if not container_client:
return ''
try:
blob_client = container_client.get_blob_client(key)
if bin_size:
@@ -166,11 +180,13 @@ class IrAttachment(models.Model):
location = self.env.context.get("storage_location") or self._storage()
if location == "azure":
container_client = self._get_azure_container()
if not container_client:
return ''
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:
@@ -189,8 +205,15 @@ class IrAttachment(models.Model):
@api.model
def _store_file_delete(self, fname):
if fname.startswith("azure://"):
container_client = self._get_azure_container()
# if container cannot be retrived, abort reading from azure storage
key = fname.replace("azure://", "", 1).lower()
if '/' in key:
container_name, key = key.split('/', 1)
else:
container_name = None
container_client = self._get_azure_container(container_name)
if not container_client:
return ''
# delete the file only if it is on the current configured container
# otherwise, we might delete files used on a different environment
try: