How to save S3 object to a file using boto3

后端 未结 7 1841

I\'m trying to do a \"hello world\" with new boto3 client for AWS.

The use-case I have is fairly simple: get object from S3 and save it to the file.

In boto

7条回答
  •  暖寄归人
    2020-11-28 03:06

    # Preface: File is json with contents: {'name': 'Android', 'status': 'ERROR'}
    
    import boto3
    import io
    
    s3 = boto3.resource('s3')
    
    obj = s3.Object('my-bucket', 'key-to-file.json')
    data = io.BytesIO()
    obj.download_fileobj(data)
    
    # object is now a bytes string, Converting it to a dict:
    new_dict = json.loads(data.getvalue().decode("utf-8"))
    
    print(new_dict['status']) 
    # Should print "Error"
    

提交回复
热议问题