1 # Cumulus: Efficient Filesystem Backup to the Cloud
2 # Copyright (C) 2008-2010 The Cumulus Developers
3 # See the AUTHORS file for a list of contributors.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 """Amazon S3 storage backend. Uses a URL of the form s3://BUCKET/PATH/."""
21 from __future__ import division, print_function, unicode_literals
23 import os, sys, tempfile
25 from boto.exception import S3ResponseError
26 from boto.s3.bucket import Bucket
27 from boto.s3.key import Key
31 def throw_notfound(method):
32 """Decorator to convert a 404 error into a cumulus.store.NoutFoundError."""
33 def f(*args, **kwargs):
35 return method(*args, **kwargs)
36 except S3ResponseError as e:
38 print("Got a 404:", e)
39 raise cumulus.store.NotFoundError(e)
44 class S3Store(cumulus.store.Store):
45 def __init__(self, url, **kw):
46 # Old versions of the Python urlparse library will take a URL like
47 # s3://bucket/path/ and include the bucket with the path, while new
48 # versions (2.6 and later) treat it as the netloc (which seems more
51 # But, so that we can work with either behavior, for now just combine
52 # the netloc and path together before we do any further processing
53 # (which will then split the combined path apart into a bucket and path
54 # again). If we didn't want to support Python 2.5, this would be
55 # easier as we could just use the netloc as the bucket directly.
56 path = self.netloc + '/' + self.path
57 (bucket, prefix) = path.lstrip("/").split("/", 1)
58 self.conn = boto.connect_s3(is_secure=False)
59 self.bucket = self.conn.create_bucket(bucket)
60 self.prefix = prefix.strip("/")
63 def _get_key(self, path):
65 k.key = "%s/%s" % (self.prefix, path)
70 prefix = "%s/%s/" % (self.prefix, path)
71 for i in self.bucket.list(prefix):
72 assert i.key.startswith(prefix)
73 self.scan_cache[i.key] = i
77 prefix = "%s/%s/" % (self.prefix, path)
78 for i in self.bucket.list(prefix):
79 assert i.key.startswith(prefix)
80 yield i.key[len(prefix):]
84 fp = tempfile.TemporaryFile()
85 k = self._get_key(path)
91 def put(self, path, fp):
92 k = self._get_key(path)
93 k.set_contents_from_file(fp)
96 def delete(self, path):
97 self.bucket.delete_key("%s/%s" % (self.prefix, path))
100 path = "%s/%s" % (self.prefix, path)
101 if path in self.scan_cache:
102 k = self.scan_cache[path]
104 k = self.bucket.get_key(path)
106 raise cumulus.store.NotFoundError
108 return {'size': int(k.size)}