How to write a file or data to an S3 object using boto3

后端 未结 7 829
一生所求
一生所求 2020-11-28 21:21

In boto 2, you can write to an S3 object using these methods:

  • Key.set_contents_from_string()
  • Key.set_contents_from_file()
  • Key.set_contents_fr
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 21:48

    Here's a nice trick to read JSON from s3:

    import json, boto3
    s3 = boto3.resource("s3").Bucket("bucket")
    json.load_s3 = lambda f: json.load(s3.Object(key=f).get()["Body"])
    json.dump_s3 = lambda obj, f: s3.Object(key=f).put(Body=json.dumps(obj))
    

    Now you can use json.load_s3 and json.dump_s3 with the same API as load and dump

    data = {"test":0}
    json.dump_s3(data, "key") # saves json to s3://bucket/key
    data = json.load_s3("key") # read json from s3://bucket/key
    

提交回复
热议问题