Skip to content

Instantly share code, notes, and snippets.

@girasquid
Created February 9, 2010 19:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save girasquid/299584 to your computer and use it in GitHub Desktop.
Save girasquid/299584 to your computer and use it in GitHub Desktop.
Turn S3 into a key/value store for JSON objects.
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))
@bjinwright
Copy link

Thanks I used this gist as an early reference when creating kev.

@lorengordon
Copy link

lorengordon commented Sep 20, 2017

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