How to save S3 object to a file using boto3

后端 未结 7 1739

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:11

    For those of you who would like to simulate the set_contents_from_string like boto2 methods, you can try

    import boto3
    from cStringIO import StringIO
    
    s3c = boto3.client('s3')
    contents = 'My string to save to S3 object'
    target_bucket = 'hello-world.by.vor'
    target_file = 'data/hello.txt'
    fake_handle = StringIO(contents)
    
    # notice if you do fake_handle.read() it reads like a file handle
    s3c.put_object(Bucket=target_bucket, Key=target_file, Body=fake_handle.read())
    

    For Python3:

    In python3 both StringIO and cStringIO are gone. Use the StringIO import like:

    from io import StringIO
    

    To support both version:

    try:
       from StringIO import StringIO
    except ImportError:
       from io import StringIO
    
    0 讨论(0)
提交回复
热议问题