From 8f902ed7ea74bb51aeaabd7800a48dcb1f5d733e Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Thu, 12 Dec 2019 14:42:59 +0100 Subject: [PATCH 1/6] attachment_s3: Add set ACL function --- attachment_s3/models/ir_attachment.py | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/attachment_s3/models/ir_attachment.py b/attachment_s3/models/ir_attachment.py index 09c357d..c2263c5 100644 --- a/attachment_s3/models/ir_attachment.py +++ b/attachment_s3/models/ir_attachment.py @@ -106,6 +106,34 @@ class IrAttachment(models.Model): }) return bucket + @api.multi + def _store_file_set_acl(self, acl): + self.ensure_one() + fname = self.store_fname + if fname.startswith('s3://'): + s3uri = S3Uri(fname) + try: + bucket = self._get_s3_bucket(name=s3uri.bucket()) + except exceptions.UserError: + _logger.exception( + "error getting bucket from object storage" + ) + key = s3uri.item() + try: + obj = bucket.Object(key=key) + obj.Acl().put(ACL=acl) + _logger.info( + "ACL public-read successfully set on object %s" % fname + ) + return True + except ClientError: + _logger.exception( + "Cannot set ACL %s on object %s" % (acl, fname) + ) + else: + _logger.warning("Cannot set ACL on object not stored on S3") + return False + @api.model def _store_file_read(self, fname, bin_size=False): if fname.startswith('s3://'): From 1ad9a198a2607f59409e891720a2e736241ba973 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Mon, 16 Dec 2019 13:39:17 +0100 Subject: [PATCH 2/6] Update attachment_s3/models/ir_attachment.py Co-Authored-By: Patrick Tombez <35060345+p-tombez@users.noreply.github.com> --- attachment_s3/models/ir_attachment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/attachment_s3/models/ir_attachment.py b/attachment_s3/models/ir_attachment.py index c2263c5..993b1d5 100644 --- a/attachment_s3/models/ir_attachment.py +++ b/attachment_s3/models/ir_attachment.py @@ -123,7 +123,7 @@ class IrAttachment(models.Model): obj = bucket.Object(key=key) obj.Acl().put(ACL=acl) _logger.info( - "ACL public-read successfully set on object %s" % fname + "ACL %s successfully set on object %s" % (acl, fname) ) return True except ClientError: From 6de4ced0f509ec3c56f1888beae0f5b326558509 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Wed, 29 Jan 2020 12:44:26 +0100 Subject: [PATCH 3/6] fixup! attachment_s3: Add set ACL function --- attachment_s3/models/ir_attachment.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/attachment_s3/models/ir_attachment.py b/attachment_s3/models/ir_attachment.py index 993b1d5..e0b6814 100644 --- a/attachment_s3/models/ir_attachment.py +++ b/attachment_s3/models/ir_attachment.py @@ -16,6 +16,7 @@ _logger = logging.getLogger(__name__) try: import boto3 from botocore.exceptions import ClientError, EndpointConnectionError + from botocore.errorfactory import NoSuchKey except ImportError: boto3 = None # noqa ClientError = None # noqa @@ -118,6 +119,7 @@ class IrAttachment(models.Model): _logger.exception( "error getting bucket from object storage" ) + return False key = s3uri.item() try: obj = bucket.Object(key=key) @@ -126,6 +128,10 @@ class IrAttachment(models.Model): "ACL %s successfully set on object %s" % (acl, fname) ) return True + except NoSuchKey: + _logger.exception( + "Object %s does not exists on S3 bucket" % fname + ) except ClientError: _logger.exception( "Cannot set ACL %s on object %s" % (acl, fname) From 4d04588125ed9e027b419be1f28a0ee644ed38f4 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Fri, 31 Jan 2020 12:42:30 +0100 Subject: [PATCH 4/6] fixup! fixup! attachment_s3: Add set ACL function --- attachment_s3/models/ir_attachment.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/attachment_s3/models/ir_attachment.py b/attachment_s3/models/ir_attachment.py index e0b6814..f8135ba 100644 --- a/attachment_s3/models/ir_attachment.py +++ b/attachment_s3/models/ir_attachment.py @@ -16,7 +16,6 @@ _logger = logging.getLogger(__name__) try: import boto3 from botocore.exceptions import ClientError, EndpointConnectionError - from botocore.errorfactory import NoSuchKey except ImportError: boto3 = None # noqa ClientError = None # noqa @@ -128,14 +127,16 @@ class IrAttachment(models.Model): "ACL %s successfully set on object %s" % (acl, fname) ) return True - except NoSuchKey: - _logger.exception( - "Object %s does not exists on S3 bucket" % fname - ) - except ClientError: - _logger.exception( - "Cannot set ACL %s on object %s" % (acl, fname) - ) + except ClientError as e: + error_code = e.response['Error']['Code'] + if error_code == "NoSuchKey": + _logger.exception( + "Object %s does not exists on S3 bucket" % fname + ) + else: + _logger.exception( + "Cannot set ACL %s on object %s" % (acl, fname) + ) else: _logger.warning("Cannot set ACL on object not stored on S3") return False From 985fa501582fd231e19ea537082e478134524c85 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Tue, 11 Feb 2020 17:34:42 +0100 Subject: [PATCH 5/6] Support minio.io for DEV without error --- attachment_s3/models/ir_attachment.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/attachment_s3/models/ir_attachment.py b/attachment_s3/models/ir_attachment.py index f8135ba..98d960c 100644 --- a/attachment_s3/models/ir_attachment.py +++ b/attachment_s3/models/ir_attachment.py @@ -133,10 +133,15 @@ class IrAttachment(models.Model): _logger.exception( "Object %s does not exists on S3 bucket" % fname ) + elif error_code == 'NotImplemented': + _logger.exception( + "S3 storage does not implement ACL" % fname + ) else: _logger.exception( "Cannot set ACL %s on object %s" % (acl, fname) ) + return False else: _logger.warning("Cannot set ACL on object not stored on S3") return False From f51efc87f3c526185ed2fdee386aab8fef8d3526 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Thu, 28 May 2020 15:58:21 +0200 Subject: [PATCH 6/6] fixup! fixup! fixup! attachment_s3: Add set ACL function --- attachment_s3/models/ir_attachment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/attachment_s3/models/ir_attachment.py b/attachment_s3/models/ir_attachment.py index 98d960c..d54c492 100644 --- a/attachment_s3/models/ir_attachment.py +++ b/attachment_s3/models/ir_attachment.py @@ -110,7 +110,7 @@ class IrAttachment(models.Model): def _store_file_set_acl(self, acl): self.ensure_one() fname = self.store_fname - if fname.startswith('s3://'): + if fname and fname.startswith('s3://'): s3uri = S3Uri(fname) try: bucket = self._get_s3_bucket(name=s3uri.bucket())