Created
June 14, 2020 10:45
-
-
Save vlcinsky/bbeda4321208aa98745afc29b58e90ac to your computer and use it in GitHub Desktop.
S3JsonBucket to dump/load data to S3 bucket. Comment to https://stackoverflow.com/a/50101751/346478
This file contains hidden or 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 json | |
import boto3 | |
class S3JsonBucket: | |
def __init__(self, bucket_name): | |
self.bucket = boto3.resource("s3").Bucket(bucket_name) | |
def load(self, key): | |
return json.load(self.bucket.Object(key=key).get()["Body"]) | |
def dump(self, key, obj): | |
return self.bucket.Object(key=key).put(Body=json.dumps(obj)) | |
def test_it(): | |
jsbucket = S3JsonBucket("your_bucket_name") | |
org_data = {"test": 12345} | |
jsbucket.dump("test/data.json", org_data) | |
read_data = jsbucket.load("test/data.json") | |
assert read_data == org_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment