Created
February 9, 2010 19:46
-
-
Save girasquid/299584 to your computer and use it in GitHub Desktop.
Turn S3 into a key/value store for JSON objects.
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 | |
class S3KeyStore(object): | |
def __init__(self, access_key, secret_key, bucket): | |
self.conn = S3Connection(access_key, secret_key) | |
self.bucket = self.conn.create_bucket(bucket) | |
def get(self, key): | |
k = Key(self.bucket) | |
k.key = key | |
return simplejson.loads(k.get_contents_as_string()) | |
def set(self, key, value): | |
k = Key(self.bucket) | |
k.key = key | |
k.set_contents_from_string(simplejson.dumps(value)) |
similar idea, but with boto3:
import boto3
class S3KeyStore(object):
def __init__(self, bucket):
self.s3 = boto3.resource('s3')
self.bucket = bucket
def _obj(self, key):
return self.s3.Object(bucket_name=self.bucket, key=key)
def get(self, key):
obj = self._obj(key)
val = obj.get()["Body"].read().decode('utf-8')
return val
def put(self, key, val):
obj = self._obj(key)
obj.put(Body=bytes(val.encode('utf-8')))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks I used this gist as an early reference when creating kev.