OpenDAL Python Binding

0.46.0 · active · verified Fri Apr 17

OpenDAL provides a unified data access layer, allowing Python applications to interact with various storage services (e.g., S3, Azure Blob, GCS, local filesystem) through a single API. It's a binding to the Apache OpenDAL Rust core, currently at version 0.46.0 of the Python package. The Python package typically follows the Rust core, though with some release lag, and provides both asynchronous and synchronous interfaces.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize an OpenDAL S3 operator and perform basic asynchronous operations: write, read, get metadata, and delete. Ensure your S3 bucket, region, and credentials are set as environment variables or provided directly in the configuration. For a simpler start, replace the config with `op = opendal.Operator("memory")`.

import opendal
import asyncio
import os

async def main():
    # Configure OpenDAL for S3. Replace with your actual credentials or use a different scheme.
    # Using os.environ.get for security and flexibility.
    config = {
        "scheme": "s3",
        "bucket": os.environ.get("OPENDAL_S3_BUCKET", "your-s3-bucket"),
        "region": os.environ.get("OPENDAL_S3_REGION", "us-east-1"),
        "access_key_id": os.environ.get("OPENDAL_S3_ACCESS_KEY_ID", ""),
        "secret_access_key": os.environ.get("OPENDAL_S3_SECRET_ACCESS_KEY", ""),
    }
    
    try:
        # Initialize the asynchronous Operator
        op = opendal.Operator(config)
        key = "hello_opendal.txt"
        content_to_write = b"Hello from OpenDAL Python!"
        
        # Write data asynchronously
        await op.write(key, content_to_write)
        print(f"Successfully wrote '{content_to_write.decode()}' to {key}")
        
        # Read data asynchronously
        read_content = await op.read(key)
        print(f"Successfully read '{read_content.decode()}' from {key}")
        
        # Get metadata asynchronously
        metadata = await op.stat(key)
        print(f"Metadata for {key}: size={metadata.content_length}, last_modified={metadata.last_modified}")
        
        # Clean up
        await op.delete(key)
        print(f"Successfully deleted {key}")

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

if __name__ == "__main__":
    # Ensure the environment variables are set for S3 or use a simpler scheme like 'memory'.
    # Example for 'memory' scheme (no credentials needed, replace config):
    # op = opendal.Operator("memory")
    asyncio.run(main())

view raw JSON →