Created
September 24, 2011 03:27
-
-
Save yashh/1238913 to your computer and use it in GitHub Desktop.
A blobstore to store huge data objects in S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import simplejson | |
from boto.s3.connection import S3Connection | |
from boto.s3.key import Key | |
from boto.exception import S3ResponseError | |
AWS_ACCESS_KEY_ID = "xxx" | |
AWS_SECRET_ACCESS_KEY = "xxxxx" | |
class BlobStore(object): | |
def __init__(self, bucket_name): | |
s3_conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) | |
self._bucket = s3_conn.get_bucket(bucket_name) | |
self._key = Key(self._bucket) | |
def get_key(self, *args, **kwargs): raise Exception("Implement get_key on you BlobModel") | |
def get(self, key, default=None): | |
self._key.key = self.get_key(key) | |
try: | |
return simplejson.loads(self._key.get_contents_as_string()) | |
except S3ResponseError, err: | |
if err.status == 404: return default | |
raise err | |
def set(self, key, value): | |
self._key.key = self.get_key(key) | |
if type(value) in [list, dict]: value = simplejson.dumps(value) | |
return self._key.set_contents_from_string(value) | |
def delete(self, key): | |
return self._bucket.delete_key(self.get_key(key)) | |
class MyNewDataBlob(BlobStore): | |
def __init__(self): | |
super(MyNewDataBlob, self).__init__("s3_bucket_name") | |
def get_key(self, user_id): return "userdata:"+str(user_id) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment