How to write Python string to a file in S3 Bucket using boto3

To write a file from a Python string directly to an S3 bucket we need to use the boto3 package.

There are 2 ways to write a file in S3 using boto3. The first is via the boto3 client, and the second is via the boto3 resource. Both of these methods will be shown below.

S3 objects and keys

If you are new to AWS S3, you might be confused with some of the terms. So we’ll define some of them here. If you already know what objects and keys are then you can skip this section.

S3 objects are the same as files. When we run the method put_object what it means is that we are putting a file into S3.

S3 keys are the same as the filename with its full path. So if we want to create an object in S3 with the name of filename.txt within the foobar folder then the key is foobar/filename.txt.

Now that we have clarified some of the AWS S3 terms, follow the details below to start writing Python strings directly to objects in S3.

Writing Python string to S3 objects using boto3 resource

Boto3 resource is a high-level abstraction for accessing AWS resources in an object-oriented interface. You can learn more about boto3 resource here.

Below is a Python code where we write the string This is a random string. to the S3 bucket radishlogic-bucket with a key of folder/file_resource.txt.

import boto3

data_string = "This is a random string."

s3 = boto3.resource('s3')

object = s3.Object(
    bucket_name='radishlogic-bucket', 
    key='folder/file_resource.txt'
)

object.put(Body=data_string)

Below are boto3 documentation links on putting an object in S3 using boto3 resource.

Writing Python string to S3 objects using boto3 client

Boto3 client is a low-level interface to access AWS resources. I actually prefer using boto3 client since this is faster and uses fewer compute resources compared to boto3 resource. You can learn more about boto3 client here.

Below is a Python code where we write the string This is a random string. to the S3 bucket radishlogich-bucket with a key of folder/file_client.txt.

import boto3

data_string = "This is a random string."

client = boto3.client('s3')

client.put_object(
    Body=data_string, 
    Bucket='radishlogic-bucket', 
    Key='folder/file_client.txt'
)

Below are boto3 documentation links on putting an object in S3 using boto3 client.


Lambda Function to write Python string to S3 objects

Below are examples of writing a String to an S3 Object using AWS Lambda Function running Python.

S3 Resource

import boto3

def lambda_handler(event, context):
    data_string = "This is a string from a Lambda Function."
    
    s3 = boto3.resource('s3')
    
    object = s3.Object(
        bucket_name='radishlogic-bucket', 
        key='s3_folder/lambda_file_resource.txt'
    )
    
    object.put(Body=data_string)

S3 Client

import boto3

def lambda_handler(event, context):

    data_string = "This is a string from a Lambda Function."
    
    client = boto3.client('s3')
    
    client.put_object(
        Body=data_string, 
        Bucket='radishlogic-bucket', 
        Key='s3_folder/lambda_file_client.txt'
    )

String to bytes conversion

If we look at the documentation for both boto3 client and resource, it says that the Body parameter of put_object should be in b'bytes.

It did not mention that the Body parameter could be a string. But since putting string directly to the Body parameter works that is what I am recommending.

If you still want to do the string-to-bytes conversion then you can use the .encode() function of Python strings.

data_string = "This is a random string."
data_bytes = data_string.encode()

print(data_bytes)

Once you have converted the string to bytes, you can assign the data_bytes variable to the value of the Body parameter of client.put_object.

import boto3

data_string = "This is a random string."
data_bytes = data_string.encode()

client = boto3.client('s3')

client.put_object(
    Body=data_bytes, 
    Bucket='radishlogic-bucket', 
    Key='folder/file_client_bytes.txt'
)

Here is the python code if you want to convert string to bytes and use boto3 S3 resource.

import boto3

data_string = "This is a random string."
data_bytes = data_string.encode()

s3 = boto3.resource('s3')

object = s3.Object(
    bucket_name='radishlogic-bucket', 
    key='folder/file_resource_bytes.txt'
)

object.put(Body=data_bytes)

I hope the above instructions helped you with writing Python strings directly to an S3 file or object.

Let me know your experience in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.