Python3 boto3 put and put_object to s3 1

Python3 boto3 put and put_object to s3

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')

Here is what the above code is Doing:
1. We’re creating a resource object for the S3 service.
2. We’re creating an object resource for the bucket and key we want to put our data into.
3. We’re calling the put() method on the object resource, passing in the data we want to store.
4. We’re creating a client object for the S3 service.
5. We’re calling the put_object() method on the client object, passing in the data we want to store, the name of the bucket, and the key we want to store it under.

Similar Posts