Uploading a file to a bucket

Now, we need to make use of the created bucket and upload a file to it. Remember, the file representation inside the bucket is an object. So, boto3 provides some methods that contain the object as a part of it. We will start by using put_object(). This method will upload a file to the created bucket and store it as an object:

import boto3s3_resource = boto3.resource("s3")bucket = s3_resource.Bucket("my_first_bucket")with open('~/test_file.txt', 'rb') as uploaded_data:    bucket.put_object(Body=uploaded_data)

In the preceding example, the following applies:

  1. We imported the boto3 module that we installed previously.
  2. Then, we specified a resource type that we wanted to interact with, which is s3, and assigned that ...

Get Hands-On Enterprise Automation with Python. now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.