Thought leadership from the most innovative tech companies, all in one place.

Upload a File to Amazon S3 With Python

Boto3 aids you to navigate through the Amazon ecosystem

image

Amazon is still the leader in cloud computing in 2021. In your cloud journey, you need to know your way around the Amazon SDK. It allows you to do mighty things.

In this article, you'll learn more about the Amazon SDK. We'll also get our hands dirty and write some code.

By the end of this article, you'll:

  • Get some experience with AWS Lambda
  • Know how to get started with Boto3
  • Know how you can access a resource like Amazon S3 with Boto3

What is boto3?

Boto3 is the official Python Software Development Kit for AWS. Boto3 allows you to manage your AWS resources. With the SDK, you can create, update and delete, from within your Python code.

Boto3 has replaced Boto version 2, which was incompatible ever since the recent changes to the Python language. Boto got named after a type of dolphin, which swims in the Amazon river. Just like the dolphin, it allows you to navigate the Amazon ecosystem with ease.

Create an AWS Lambda Function

In this section, let me walk you through a coding example of my own. The complete snippet can be found at the end of this section. One of the best ways to learn is by example, isn't that right?

So, let's get going!

bucket_name = event['bucket_name']
file_name = event['file_name']

The first thing you'll notice is how some parameters are passed to the Lambda function by an event. For this tutorial, you'll need your own Amazon s3 bucket name (e.g. testawsfile). Apart from that, you also need to specify the filename and the content of the file. For simplicity, let's create a .txt file.

file = io.BytesIO(bytes(event['file_content'], encoding='utf-8'))

The line above reads the file in memory with the use of the standard input/output library.

s3 = boto3.resource('s3')

In the first real line of the Boto3 code, you'll register the resource. In this case, the Amazon S3 service.

bucket = s3.Bucket(bucket_name)

In the second line, the bucket is specified. You might wonder what a bucket is, but let's keep things simple. A bucket is nothing more than a folder in the cloud, with enhanced features, of course.

bucket_object = bucket.Object(file_name)
bucket_object.upload_fileobj(file)

Finally, you create a file with the specified filename inside the bucket, and the file is uploaded directly to Amazon s3.

You've successfully created a file from within a Python script. And all of that, with just a few lines of code. This AWS SDK allows you to play with your AWS resources from your computer and inside the cloud with no additional costs. You only pay for the resources you use.

Very important: Do not forget to grant yourself access to Amazon S3 with IAM. Create a role and attach a policy with write access to Amazon S3, otherwise, your lambda function will not work.

Upload a resource to Amazon S3

Conclusion

This article helps you get started with the Amazon SDK. We've seen how to upload a file to a cloud resource like Amazon S3.

Boto3 is one of the best cloud SDKs at the moment. It allows you to manage all your AWS resources with ease. On top of that, the documentation is really on point.

When you're starting with Amazon, you like to use the web console. I got to admit. I like the web console too! But after a while, things get repetitive, and the Amazon SDK allows you to overcome this.

In addition, the world is changing, and development continues to move to the cloud. Therefore, understanding how to work with an SDK is essential. It gives you more power and tools to eliminate repetition.

Thanks for reading this article.




Continue Learning