|
| 1 | + |
| 2 | +__version__ = '0.1.0' |
| 3 | +__author__ = 'Juan Batiz-Benet' |
| 4 | + |
| 5 | +__doc__ = ''' |
| 6 | +aws datastore implementation. |
| 7 | +
|
| 8 | +Tested with: |
| 9 | +* boto 2.5.2 |
| 10 | +
|
| 11 | +''' |
| 12 | + |
| 13 | +#TODO: Implement queries using a key index. |
| 14 | +#TODO: Implement TTL (and key configurations) |
| 15 | + |
| 16 | + |
| 17 | +from boto.s3.key import Key as S3Key |
| 18 | +from boto.exception import S3ResponseError |
| 19 | + |
| 20 | +import datastore |
| 21 | + |
| 22 | + |
| 23 | +class S3BucketDatastore(datastore.Datastore): |
| 24 | + '''Simple aws s3 datastore. Does not support queries. |
| 25 | +
|
| 26 | + The s3 interface is very similar to datastore's. The only differences are: |
| 27 | + - values must be strings (SerializerShimDatastore) |
| 28 | + - keys must be converted into strings |
| 29 | + ''' |
| 30 | + |
| 31 | + def __init__(self, s3bucket): |
| 32 | + '''Initialize the datastore with given s3 bucket `s3bucket`. |
| 33 | +
|
| 34 | + Args: |
| 35 | + s3bucket: An s3 bucket to use. |
| 36 | +
|
| 37 | + Example:: |
| 38 | +
|
| 39 | + from boto.s3.connection import S3Connection |
| 40 | + s3conn = S3Connection('<aws access key>', '<aws secret key>') |
| 41 | + s3bucket = s3conn.get_bucket('<bucket name>') |
| 42 | + s3ds = S3BucketDatastore(s3bucket) |
| 43 | + ''' |
| 44 | + self._s3bucket = s3bucket |
| 45 | + |
| 46 | + def _s3key(self, key): |
| 47 | + '''Return an s3 key for given datastore key.''' |
| 48 | + k = S3Key(self._s3bucket) |
| 49 | + k.key = str(key) |
| 50 | + return k |
| 51 | + |
| 52 | + @classmethod |
| 53 | + def _s3keys_get_contents_as_string_gen(cls, s3keys): |
| 54 | + '''s3 content retriever generator.''' |
| 55 | + for s3key in s3keys: |
| 56 | + yield s3key.get_contents_as_string() |
| 57 | + |
| 58 | + |
| 59 | + def get(self, key): |
| 60 | + '''Return the object named by key or None if it does not exist. |
| 61 | +
|
| 62 | + Args: |
| 63 | + key: Key naming the object to retrieve |
| 64 | +
|
| 65 | + Returns: |
| 66 | + object or None |
| 67 | + ''' |
| 68 | + try: |
| 69 | + return self._s3key(key).get_contents_as_string() |
| 70 | + except S3ResponseError, e: |
| 71 | + return None |
| 72 | + |
| 73 | + def put(self, key, value): |
| 74 | + '''Stores the object `value` named by `key`. |
| 75 | +
|
| 76 | + Args: |
| 77 | + key: Key naming `value` |
| 78 | + value: the object to store. |
| 79 | + ''' |
| 80 | + self._s3key(key).set_contents_from_string(value) |
| 81 | + |
| 82 | + def delete(self, key): |
| 83 | + '''Removes the object named by `key`. |
| 84 | +
|
| 85 | + Args: |
| 86 | + key: Key naming the object to remove. |
| 87 | + ''' |
| 88 | + self._s3key(key).delete() |
| 89 | + |
| 90 | + def query(self, query): |
| 91 | + '''Returns an iterable of objects matching criteria expressed in `query` |
| 92 | +
|
| 93 | + Implementations of query will be the largest differentiating factor |
| 94 | + amongst datastores. All datastores **must** implement query, even using |
| 95 | + query's worst case scenario, see :ref:class:`Query` for details. |
| 96 | +
|
| 97 | + Args: |
| 98 | + query: Query object describing the objects to return. |
| 99 | +
|
| 100 | + Raturns: |
| 101 | + iterable cursor with all objects matching criteria |
| 102 | + ''' |
| 103 | + allkeys = self._s3bucket.list(prefix=str(query.key).strip('/')) |
| 104 | + iterable = self._s3keys_get_contents_as_string_gen(allkeys) |
| 105 | + return query(iterable) # must apply filters, order, etc naively. |
| 106 | + |
| 107 | + |
| 108 | + def contains(self, key): |
| 109 | + '''Returns whether the object named by `key` exists. |
| 110 | +
|
| 111 | + Args: |
| 112 | + key: Key naming the object to check. |
| 113 | +
|
| 114 | + Returns: |
| 115 | + boalean whether the object exists |
| 116 | + ''' |
| 117 | + return self._s3key(key).exists() |
| 118 | + |
| 119 | + |
| 120 | +''' |
| 121 | +Hello World: |
| 122 | +
|
| 123 | + >>> import datastore.aws |
| 124 | + >>> from boto.s3.connection import S3Connection |
| 125 | + >>> |
| 126 | + >>> s3conn = S3Connection('<aws access key>', '<aws secret key>') |
| 127 | + >>> s3bucket = s3conn.get_bucket('<bucket name>') |
| 128 | + >>> ds = datastore.aws.S3BucketDatastore(s3bucket) |
| 129 | + >>> |
| 130 | + >>> hello = datastore.Key('hello') |
| 131 | + >>> ds.put(hello, 'world') |
| 132 | + >>> ds.contains(hello) |
| 133 | + True |
| 134 | + >>> ds.get(hello) |
| 135 | + 'world' |
| 136 | + >>> ds.delete(hello) |
| 137 | + >>> ds.get(hello) |
| 138 | + None |
| 139 | +
|
| 140 | +''' |
0 commit comments