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

后端 未结 7 778
一生所求
一生所求 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 22:02

    In boto 3, the 'Key.set_contents_from_' methods were replaced by

    • Object.put()

    • Client.put_object()

    For example:

    import boto3
    
    some_binary_data = b'Here we have some data'
    more_binary_data = b'Here we have some more data'
    
    # Method 1: Object.put()
    s3 = boto3.resource('s3')
    object = s3.Object('my_bucket_name', 'my/key/including/filename.txt')
    object.put(Body=some_binary_data)
    
    # Method 2: Client.put_object()
    client = boto3.client('s3')
    client.put_object(Body=more_binary_data, Bucket='my_bucket_name', Key='my/key/including/anotherfilename.txt')
    

    Alternatively, the binary data can come from reading a file, as described in the official docs comparing boto 2 and boto 3:

    Storing Data

    Storing data from a file, stream, or string is easy:

    # Boto 2.x
    from boto.s3.key import Key
    key = Key('hello.txt')
    key.set_contents_from_file('/tmp/hello.txt')
    
    # Boto 3
    s3.Object('mybucket', 'hello.txt').put(Body=open('/tmp/hello.txt', 'rb'))
    
    0 讨论(0)
提交回复
热议问题