From 435825ccc55fb57d696a82c950a37868aeed12cf Mon Sep 17 00:00:00 2001 From: "Fanani M. Ihsan" Date: Tue, 13 Apr 2021 03:55:34 +0700 Subject: [PATCH] allows to configure generic object storage other than amazon s3 (eg: wasabi.com) --- ir_attachment_s3/models/res_config_settings.py | 13 ++++++++++++- .../views/res_config_settings_views.xml | 13 +++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ir_attachment_s3/models/res_config_settings.py b/ir_attachment_s3/models/res_config_settings.py index 86ac3fff59..67e790ae73 100644 --- a/ir_attachment_s3/models/res_config_settings.py +++ b/ir_attachment_s3/models/res_config_settings.py @@ -2,6 +2,7 @@ # Copyright 2019 Alexandr Kolushov # Copyright 2019-2020 Eugene Molotov import os +from urllib.parse import urlparse import boto3 @@ -19,6 +20,7 @@ class S3Settings(models.TransientModel): s3_access_key_id = fields.Char(string="S3 access key id") s3_secret_key = fields.Char(string="S3 secret key") s3_endpoint_url = fields.Char(string="S3 Endpoint") + s3_region = fields.Char(string="S3 Region") s3_obj_url = fields.Char(string="S3 URL") s3_condition = fields.Char( string="S3 condition", @@ -38,13 +40,18 @@ def get_s3_obj_url(self, bucket, file_id): base_url = self._get_s3_settings("s3.obj_url", "S3_OBJ_URL") if base_url: return base_url + file_id - return "https://{}.s3.amazonaws.com/{}".format(bucket.name, file_id) + + endpoint_url = self._get_s3_settings("s3.endpoint_url", "S3_ENDPOINT_URL") + bucket_name = self._get_s3_settings("s3.bucket", "S3_BUCKET") + url = urlparse(endpoint_url) + return "{}://{}.{}/{}".format(url.scheme, bucket_name, url.netloc, file_id) def get_s3_bucket(self): access_key_id = self._get_s3_settings("s3.access_key_id", "S3_ACCESS_KEY_ID") secret_key = self._get_s3_settings("s3.secret_key", "S3_SECRET_KEY") bucket_name = self._get_s3_settings("s3.bucket", "S3_BUCKET") endpoint_url = self._get_s3_settings("s3.endpoint_url", "S3_ENDPOINT_URL") + region = self._get_s3_settings("s3.region", "S3_REGION") if not access_key_id or not secret_key or not bucket_name: raise NotAllCredentialsGiven( @@ -56,6 +63,7 @@ def get_s3_bucket(self): aws_access_key_id=access_key_id, aws_secret_access_key=secret_key, endpoint_url=endpoint_url, + region_name=region ) bucket = s3.Bucket(bucket_name) if not bucket: @@ -71,6 +79,7 @@ def get_values(self): s3_access_key_id = ICPSudo.get_param("s3.access_key_id", default="") s3_secret_key = ICPSudo.get_param("s3.secret_key", default="") s3_endpoint_url = ICPSudo.get_param("s3.endpoint_url", default="") + s3_region = ICPSudo.get_param("s3.region", default="") s3_obj_url = ICPSudo.get_param("s3.obj_url", default="") s3_condition = ICPSudo.get_param("s3.condition", default="") @@ -80,6 +89,7 @@ def get_values(self): s3_secret_key=s3_secret_key, s3_condition=s3_condition, s3_endpoint_url=s3_endpoint_url, + s3_region=s3_region, s3_obj_url=s3_obj_url, ) return res @@ -91,6 +101,7 @@ def set_values(self): ICPSudo.set_param("s3.access_key_id", self.s3_access_key_id or "") ICPSudo.set_param("s3.secret_key", self.s3_secret_key or "") ICPSudo.set_param("s3.endpoint_url", self.s3_endpoint_url or "") + ICPSudo.set_param("s3.region", self.s3_region or "") ICPSudo.set_param("s3.obj_url", self.s3_obj_url or "") ICPSudo.set_param("s3.condition", self.s3_condition or "") diff --git a/ir_attachment_s3/views/res_config_settings_views.xml b/ir_attachment_s3/views/res_config_settings_views.xml index f4d2e2b0c2..d994ba2415 100644 --- a/ir_attachment_s3/views/res_config_settings_views.xml +++ b/ir_attachment_s3/views/res_config_settings_views.xml @@ -69,6 +69,19 @@ /> +
+
+
+