TL, DR
MicroPython is a fantastic framework to write code for micro controllers. I just created a very simple library to enable those devices uploading files into a MinIO object storage.
MinIO object storage
I already wrote a couple of blog posts about MinIO (you can check them here), a great self-hosted object storage software. There are already Software Development Kits for many languages, but there was no official support for small electronic devices, such an ESP32. Until now!
MicroPython
MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimized to run on micro controllers and in constrained environments.
Setup guide
Tools
- Electronic board which supports MicroPython (see a list here)
- IDE which supports MicroPython (for instance, Thonny)
- Code from my GitHub repository
Procedure
Copy __init__.py
Create a uminio
folder in the /lib
directory of your MicroPython device and copy the __init__.py
file into it.
MinIO Credentials & Configuration
Import the MinioClient
class in your MicroPython script and configure it with your MinIO server details. You can do this by setting the following variables in your script:
from uminio import MinioClient
# --- MinIO Client Configuration ---
MINIO_ENDPOINT = "192.168.1.100:9000" # Your MinIO server IP address and port
MINIO_ACCESS_KEY = "YOUR_ACCESS_KEY" # Your MinIO access key
MINIO_SECRET_KEY = "YOUR_SECRET_KEY" # Your MinIO secret key
MINIO_REGION = "eu-east-1" # The region for your MinIO server
MINIO_USE_HTTPS = False # Set to True if your MinIO server uses HTTPS
mc = MinioClient(
endpoint=MINIO_ENDPOINT,
access_key=MINIO_ACCESS_KEY,
secret_key=MINIO_SECRET_KEY,
region=MINIO_REGION,
use_https=MINIO_USE_HTTPS,
)
Important Security Note: Hardcoding credentials directly into the script is generally not recommended for production environments. Consider alternative methods for managing secrets on your device if security is a major concern.
IAM Permissions
Ensure the MinIO user associated with the MINIO_ACCESS_KEY
and MINIO_SECRET_KEY
has the necessary permissions to put objects into the specified bucket.
Usage Example
Here’s how to use uminio
to upload a local file from your MicroPython device to MinIO:
import network
import time
from uminio import MinioClient
# --- MinIO Client Configuration ---
MINIO_ENDPOINT = "192.168.1.100:9000" # Your MinIO server IP address and port
MINIO_ACCESS_KEY = "YOUR_ACCESS_KEY" # Your MinIO access key
MINIO_SECRET_KEY = "YOUR_SECRET_KEY" # Your MinIO secret key
MINIO_REGION = "eu-east-1" # The region for your MinIO server
MINIO_USE_HTTPS = False # Set to True if your MinIO server uses HTTPS
mc = MinioClient(
endpoint=MINIO_ENDPOINT,
access_key=MINIO_ACCESS_KEY,
secret_key=MINIO_SECRET_KEY,
region=MINIO_REGION,
use_https=MINIO_USE_HTTPS,
)
# --- Network Configuration (Example for ESP32/ESP8266) ---
WIFI_SSID = "YOUR_WIFI_SSID"
WIFI_PASSWORD = "YOUR_WIFI_PASSWORD"
def connect_wifi():
sta_if = network.WLAN(network.STA_IF) #
if not sta_if.isconnected():
print("Connecting to WiFi...")
sta_if.active(True)
sta_if.connect(WIFI_SSID, WIFI_PASSWORD)
while not sta_if.isconnected():
time.sleep(1)
print("Network Config:", sta_if.ifconfig())
# --- Main Application ---
def main():
# 1. Connect to WiFi
connect_wifi()
# 2. Synchronize time (critical for MinIO authentication)
mc.sync_time() #
# 3. Create a dummy file to upload (or use an existing file)
local_file_to_upload = "data.txt"
bucket_name = "my_bucket" # Ensure this bucket exists in MinIO
s3_object_name = "my_device_data/data.txt" # Desired path and name in S3
content_type = "text/plain" #
try:
with open(local_file_to_upload, "w") as f:
f.write("Hello from MicroPython!\n")
f.write(f"Timestamp: {time.time()}\n")
print(f"Created dummy file: {local_file_to_upload}")
except OSError as e:
print(f"Error creating file: {e}")
return
# 4. Upload the file
print(f"Attempting to upload '{local_file_to_upload}' to MinIO bucket '{bucket_name}' as '{s3_object_name}'...")
if mc.upload_file(local_file_to_upload, bucket_name, s3_object_name, content_type): #
print("Upload successful!")
else:
print("Upload failed.")
if __name__ == "__main__":
main()
Related links
- MicroPython website link
- MinIO Software Development Kits link
uminio
GitHub repo link- Other MinIO posts on RDS link
Do you like our content? Check more of our posts in our blog!