From a3c032298238733691951513d3ae503754f73bee Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Mon, 13 Nov 2017 11:24:14 +0100 Subject: [PATCH] Add AWS_REGION to connect to bucket with specific region --- README.md | 2 +- attachment_s3/README.rst | 1 + attachment_s3/models/ir_attachment.py | 27 ++++++++++++++++----------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 2c75580..34bdcd9 100644 --- a/README.md +++ b/README.md @@ -70,11 +70,11 @@ The exact naming is important, because the `cloud_platform` addon rely on these * prod: stored RW in the object storage * `AWS_HOST`: depends of the platform + * `AWS_REGION`: region's endpoint * `AWS_ACCESS_KEY_ID`: depends of the platform * `AWS_SECRET_ACCESS_KEY`: depends of the platform * `AWS_BUCKETNAME`: `-odoo-prod` * integration: - * `AWS_HOST`: depends of the platform * `AWS_ACCESS_KEY_ID`: depends of the platform * `AWS_SECRET_ACCESS_KEY`: depends of the platform * `AWS_BUCKETNAME`: `-odoo-integration` diff --git a/attachment_s3/README.rst b/attachment_s3/README.rst index 9a887f2..03423eb 100644 --- a/attachment_s3/README.rst +++ b/attachment_s3/README.rst @@ -15,6 +15,7 @@ Activate S3 storage: Configure accesses with environment variables: * ``AWS_HOST`` (not required if using AWS services) +* ``AWS_REGION`` (required if using AWS services) * ``AWS_ACCESS_KEY_ID`` * ``AWS_SECRET_ACCESS_KEY`` * ``AWS_BUCKETNAME`` diff --git a/attachment_s3/models/ir_attachment.py b/attachment_s3/models/ir_attachment.py index 84318db..9a61303 100644 --- a/attachment_s3/models/ir_attachment.py +++ b/attachment_s3/models/ir_attachment.py @@ -7,7 +7,6 @@ import base64 import logging import os import xml.dom.minidom -from functools import partial from odoo import _, api, exceptions, models from ..s3uri import S3Uri @@ -37,6 +36,7 @@ class IrAttachment(models.Model): The following environment variables can be set: * ``AWS_HOST`` + * ``AWS_REGION`` * ``AWS_ACCESS_KEY_ID`` * ``AWS_SECRET_ACCESS_KEY`` * ``AWS_BUCKETNAME`` @@ -46,17 +46,23 @@ class IrAttachment(models.Model): """ host = os.environ.get('AWS_HOST') - if host: - connect_s3 = partial(boto.connect_s3, host=host) - else: - connect_s3 = boto.connect_s3 - + region_name = os.environ.get('AWS_REGION') access_key = os.environ.get('AWS_ACCESS_KEY_ID') secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY') - if name: - bucket_name = name + bucket_name = name or os.environ.get('AWS_BUCKETNAME') + + params = { + 'aws_access_key_id': access_key, + 'aws_secret_access_key': secret_key, + } + if host: + params['host'] = host + if region_name: + # needs specific method for region + connect_s3 = boto.s3.connect_to_region + params['region_name'] = region_name else: - bucket_name = os.environ.get('AWS_BUCKETNAME') + connect_s3 = boto.connect_s3 if not (access_key and secret_key and bucket_name): msg = _('If you want to read from the %s S3 bucket, the following ' 'environment variables must be set:\n' @@ -72,8 +78,7 @@ class IrAttachment(models.Model): raise exceptions.UserError(msg) try: - conn = connect_s3(aws_access_key_id=access_key, - aws_secret_access_key=secret_key) + conn = connect_s3(**params) except S3ResponseError as error: # log verbose error from s3, return short message for user