changeset 1157:e4f2d6a4b401

Rework S3 connection logic for latest versions of Python 2.7. Had to make these changes for Ubuntu 16.04. Seems backward compatible with production.
author Brian Neal <bgneal@gmail.com>
date Thu, 19 Jan 2017 18:35:53 -0600 (2017-01-20)
parents 8c901ab0df62
children 364f8ec48612
files core/s3.py
diffstat 1 files changed, 14 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/core/s3.py	Mon Jan 16 13:23:46 2017 -0600
+++ b/core/s3.py	Thu Jan 19 18:35:53 2017 -0600
@@ -3,7 +3,8 @@
 This module provides Amazon S3 convenience wrappers.
 
 """
-from boto.s3.connection import S3Connection
+from boto.s3 import connect_to_region
+from boto.s3.connection import OrdinaryCallingFormat
 from boto.s3.key import Key
 
 
@@ -11,14 +12,24 @@
     """This class abstracts an Amazon S3 bucket.
 
     """
-    def __init__(self, access_key, secret_key, base_url, bucket_name):
-        self.conn = S3Connection(access_key, secret_key)
+    def __init__(self, access_key, secret_key, base_url, bucket_name,
+                 region_name='us-west-1'):
+        self.region_name = region_name
+        self.conn = self._get_connection(access_key, secret_key)
         self.bucket = self.conn.get_bucket(bucket_name, validate=False)
         self.base_url = base_url
         if not base_url.endswith('/'):
             self.base_url += '/'
         self.name = bucket_name
 
+    def _get_connection(self, access_key, secret_key):
+        conn = connect_to_region(
+                    self.region_name,
+                    aws_access_key_id=access_key,
+                    aws_secret_access_key=secret_key,
+                    calling_format=OrdinaryCallingFormat())
+        return conn
+
     def upload_from_file(self, key_name, fp, metadata=None, public=True):
         """Uploads data from the file object fp to a new key named
         key_name. metadata, if not None, must be a dict of metadata key / value