Mercurial > public > sg101
comparison core/s3.py @ 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 |
parents | bf5340705d0c |
children |
comparison
equal
deleted
inserted
replaced
1156:8c901ab0df62 | 1157:e4f2d6a4b401 |
---|---|
1 """s3.py | 1 """s3.py |
2 | 2 |
3 This module provides Amazon S3 convenience wrappers. | 3 This module provides Amazon S3 convenience wrappers. |
4 | 4 |
5 """ | 5 """ |
6 from boto.s3.connection import S3Connection | 6 from boto.s3 import connect_to_region |
7 from boto.s3.connection import OrdinaryCallingFormat | |
7 from boto.s3.key import Key | 8 from boto.s3.key import Key |
8 | 9 |
9 | 10 |
10 class S3Bucket(object): | 11 class S3Bucket(object): |
11 """This class abstracts an Amazon S3 bucket. | 12 """This class abstracts an Amazon S3 bucket. |
12 | 13 |
13 """ | 14 """ |
14 def __init__(self, access_key, secret_key, base_url, bucket_name): | 15 def __init__(self, access_key, secret_key, base_url, bucket_name, |
15 self.conn = S3Connection(access_key, secret_key) | 16 region_name='us-west-1'): |
17 self.region_name = region_name | |
18 self.conn = self._get_connection(access_key, secret_key) | |
16 self.bucket = self.conn.get_bucket(bucket_name, validate=False) | 19 self.bucket = self.conn.get_bucket(bucket_name, validate=False) |
17 self.base_url = base_url | 20 self.base_url = base_url |
18 if not base_url.endswith('/'): | 21 if not base_url.endswith('/'): |
19 self.base_url += '/' | 22 self.base_url += '/' |
20 self.name = bucket_name | 23 self.name = bucket_name |
24 | |
25 def _get_connection(self, access_key, secret_key): | |
26 conn = connect_to_region( | |
27 self.region_name, | |
28 aws_access_key_id=access_key, | |
29 aws_secret_access_key=secret_key, | |
30 calling_format=OrdinaryCallingFormat()) | |
31 return conn | |
21 | 32 |
22 def upload_from_file(self, key_name, fp, metadata=None, public=True): | 33 def upload_from_file(self, key_name, fp, metadata=None, public=True): |
23 """Uploads data from the file object fp to a new key named | 34 """Uploads data from the file object fp to a new key named |
24 key_name. metadata, if not None, must be a dict of metadata key / value | 35 key_name. metadata, if not None, must be a dict of metadata key / value |
25 pairs which will be added to the key. | 36 pairs which will be added to the key. |