News
Serverspace 2024 - Year in Review
DF
January 24 2025
Updated January 24 2025

Connecting to S3-compatible storage via Amazon S3 API on Ubuntu with Python pre-installed

Python Ubuntu

This article provides a detailed walkthrough of connecting to S3-compatible storage via the Amazon S3 API on an Ubuntu system.You’ll discover how to install the required tools, set up a Python script for seamless interaction with the storage, and execute key operations such as retrieving a list of buckets, uploading files, and downloading data. Every step is thoroughly detailed, making it easy for beginners to confidently set up and test the connection without any prior experience.

Installing Python and pip

Start by updating your system and installing Python 3 along with pip:

sudo apt update sudo apt install python3 python3-pip python3 -m pip install --upgrade pip

Installing the boto3 Library

Next, install the boto3 library, which facilitates working with S3-compatible storage:

pip3 install boto3

Creating the Python Script

Create a new file (e.g., s3_client.py) using your preferred text editor:


import boto3
from botocore.client import Config

# Replace these placeholders with your credentials
access_key = 'YOUR_ACCESS_KEY'
secret_key = 'YOUR_SECRET_KEY'
endpoint_url = 'https://your.endpoint.url'

# Initialize the S3 client
s3_client = boto3.client(
's3',
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
endpoint_url=endpoint_url,
config=Config(signature_version='s3')
)

# Example: List buckets
try:
response = s3_client.list_buckets()
print("Buckets:")
for bucket in response['Buckets']:
print(f" - {bucket['Name']}")
except Exception as e:
print(f"Error: {e}")

Save the changes and exit the editor (use Ctrl + X, then press Y and Enter).

Running the Script

Run the script with the following command:

python3 s3_client.py

If configured correctly, you’ll see a list of buckets in your storage.

Additional Storage Operations

Creating a Test File

To test file uploads and downloads, create a test file:
echo "Example file content" > local_file.txt

Uploading a File

Add the following line to your script to upload the file:
s3_client.upload_file('local_file.txt', 'bucket_name', 'remote_file.txt') local_file.txt is the path to the local file. bucket_name is the name of the target bucket. remote_file.txt is the file name in the storage.

Downloading a File

Similarly, add this line to download the file:

s3_client.download_file('bucket_name', 'remote_file.txt', 'local_file.txt')
  • bucket_name is the name of the bucket.
  • remote_file.txt is the name of the file in storage.
  • local_file.txt is the path where the file will be saved.

Dependency Troubleshooting

If you encounter errors, ensure all dependencies are up to date. For example, you can manually update botocore:

pip3 install --upgrade botocore

Notes

If you don’t have an S3-compatible storage service configured, check with your provider for the necessary endpoint_url, access keys, and bucket settings. You can use alternative text editors such as vim or any editor you’re comfortable with. Ensure that your user account has the required permissions to create and manage files in the S3 storage.
This guide ensures a seamless setup process, empowering users to efficiently connect and interact with S3-compatible storage.

Explore More Python Resources

For those looking to expand their expertise, our knowledge base is packed with a variety of Python-focused materials. Whether you're just starting out or aiming to tackle advanced challenges, you'll find what you need. We offer beginner-friendly tutorials, insights on optimizing your code, and in-depth articles on topics like database integration, API development, leveraging frameworks like Django and Flask, and building machine learning-powered applications. These resources are designed to help you not only build a strong foundation in Python but also apply it effectively to solve complex, real-world problems.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.