AWS, AWS S3, Boto3

Opening an S3 Object as a String Using Boto3 (With Encoding) – Definitive Guide

Photo of author

Updated on

by Vikram Aruchamy

AWS S3, a cornerstone of cloud storage, offers unparalleled flexibility for data management. AWS also provides SDKs to interact with their services programmatically.

Boto3 is AWS S3’s Python SDK to interact with AWS S3 using the Python language.

This tutorial explains how to open an S3 object as a String using the Boto3 Library, including the Boto3 environment setup, opening the file as a String and how to handle exceptions.

Prerequisites

  • Security credentials AWS Access Key ID and the Secret Access Key.

The Security credentials can be created by clicking your Profile name at the top right corner, Clicking the Security credentials menu, and clicking the Create access key option available under the Access keys section.

Setting Up Boto3 and Configuring Credentials

Use the following code to install the boto3 library. Prefix the % symbol to install the library from the Jupyter Notebook directly.

pip install boto3

After installing Boto3, you can configure the credentials in your system using the aws configure command.

aws configure

It will let you configure the Access Key ID and the Secret access key and make it available globally in your system.

If you do not want to configure the keys globally in your system, you can create a boto3 session in your program and create a client object from that session object, as demonstrated below. However, it is not a best practice to hardcode the security credentials in your program.

import boto3

session = boto3.Session(
    aws_access_key_id='Your Access Key ID',
    aws_secret_access_key='Your Secret Access Key'
)

s3_client = session.client('s3')

Opening an S3 Object as a String Using Boto3 Client Object

The Boto3 client creates a low-level service client using the default session.

To open an S3 object as a String using the Boto3 client object,

  • Create a Client representation of the S3 service using the default boto3 session
  • Invoke the get_object() and pass the desired bucket name and the object key’s name
  • Read the response from the response body using response['Body'].read()
  • It’ll return the bytes String representation of the content. To decode the content to a normal string representation, use the decode(‘utf-8’) method
  • It’ll return the content in the normal string format

Code

import boto3

bucket_name = 'mrcloudgurudemo'

object_name = 'test_boto3.txt'

s3_client = boto3.client('s3')

response = s3_client.get_object(Bucket=bucket_name, Key=object_name)

file_content = response['Body'].read().decode('utf-8')

print(file_content)

Output

The content of the file will be displayed.

This file is a text file used for demonstrating Boto3. 

This file contains multiple lines. 

Boto3 is a Python library that interacts with the AWS service using Python. 

This is how you can use the Boto3 Client Object to open the S3 object as a String.

Opening an S3 Object as a String Using Boto3 Resource Object

The Boto3 Resource represents an object-oriented interface to AWS services.

The AWS Python SDK team does not intend to add new features to the resources interface in boto3. Existing interfaces will continue to operate during boto3’s lifecycle. You can use the client interface to interact with the service.

To open an S3 object as a String using the Boto3 resource object,

  • Create a Resource representation of the S3 service using the default boto3 session
  • Create an object representation for the S3 resource using the s3_resource.Object() and pass the desired bucket name and the object key’s name
  • Invoke the get() method of the Object and read the response from the response body using response['Body'].read()
  • It’ll return the bytes String representation of the content. To decode the content to a normal string representation, use the decode(‘utf-8’) method
  • It’ll return the content in the normal string format

Code

import boto3

bucket_name = 'mrcloudgurudemo'

object_name = 'test_boto3.txt'

s3_resource = boto3.resource('s3')

obj = s3_resource.Object(bucket_name, object_name)

file_content = obj.get()['Body'].read().decode('utf-8')

print(file_content)

Output

The content of the file will be returned.

This file is a text file used for demonstrating Boto3. 

This file contains multiple lines. 

Boto3 is a Python library that interacts with the AWS service using Python. 

Exception Handling

Any program is prone to errors for various reasons, and handling the expected and unexpected errors in the program is essential.

While opening the S3 object as a string, you’ll pass the Bucket name and the Object name, and there are chances the bucket or the object specified doesn’t exist in S3. In this case, the read operation would fail. Hence, catching the exceptions NoSuchBucket and the NoSuchKey is a good practice. It is also good to handle the general exception for any unexpected errors.

The following code demonstrates the exception handling to be done while opening the S3 object as a String using Boto3.

Code

import boto3
import botocore.exceptions

try:
    bucket_name = 'mrcloudgurudemo'

    object_name = 'test_boto3a.txt'

    s3_client = boto3.client('s3')

    response = s3_client.get_object(Bucket=bucket_name, Key=object_name)

    if response['ResponseMetadata']['HTTPStatusCode'] == 200:
        file_content = response['Body'].read().decode('utf-8')
        print(file_content)

except botocore.exceptions.ClientError as e:

    if e.response['Error']['Code'] == 'NoSuchBucket':
        print(f"{bucket_name} ->  No Such Bucket Exists : {e}")

    elif e.response['Error']['Code'] == 'NoSuchKey':
        print(f"{object_name} ->  No such key exists : {e}")

    elif e.response['Error']['Code'] in ('NoCredentialsError', 'PartialCredentialsError'):
        print(f"Error: {e}")

    else:
        print(f"An unexpected error occurred: {e}")

except Exception as e:
    print(f"An unexpected error occurred: {e}")

Output

The object test_boto3a.txt doesn’t exist in the bucket. Hence, the error is thrown as follows.

    test_boto3a.txt ->  No such key exists: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.

Conclusion

In summary, this guide explained opening S3 objects as strings with Boto3, covering encoding and error handling for effective AWS S3 integration in Python projects and reading the file content as strings without downloading the file.

Additional Resources

Leave a Comment