OpenStack Object Storage (Swift) Client

4.10.0 · active · verified Mon Apr 13

python-swiftclient is the official Python client library for interacting with the OpenStack Object Storage (Swift) API. It provides both a low-level Connection API for fine-grained control and a higher-level SwiftService API for common operations, including a command-line interface. Currently at version 4.10.0, the library is actively maintained with frequent updates, typically aligning with OpenStack release cycles.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to OpenStack Swift using the low-level `Connection` API, authenticate, list existing containers, create a new container, and upload an object. It relies on standard OpenStack environment variables for authentication details to avoid hardcoding credentials.

import os
from swiftclient.client import Connection

# --- Environment Variables for Authentication ---
# Set these environment variables before running, e.g., using an OpenStack RC file
# or directly:
# export OS_AUTH_URL="http://your-auth-url:5000/v3"
# export OS_USERNAME="your-username"
# export OS_PASSWORD="your-password"
# export OS_PROJECT_NAME="your-project-name"
# export OS_USER_DOMAIN_NAME="Default" # Or your specific user domain
# export OS_PROJECT_DOMAIN_NAME="Default" # Or your specific project domain
# export OS_REGION_NAME="RegionOne" # Or your specific region

auth_url = os.environ.get('OS_AUTH_URL', 'http://localhost:5000/v3')
username = os.environ.get('OS_USERNAME', 'test_user')
password = os.environ.get('OS_PASSWORD', 'test_password')
project_name = os.environ.get('OS_PROJECT_NAME', 'test_project')
user_domain_name = os.environ.get('OS_USER_DOMAIN_NAME', 'Default')
project_domain_name = os.environ.get('OS_PROJECT_DOMAIN_NAME', 'Default')
region_name = os.environ.get('OS_REGION_NAME', 'RegionOne')

# Ensure auth_version is specified, especially for Keystone v3
# os_options are crucial for domain-scoped authentication
os_options = {
    'user_domain_name': user_domain_name,
    'project_domain_name': project_domain_name,
    'project_name': project_name,
    'region_name': region_name
}

try:
    # Establish connection
    conn = Connection(
        authurl=auth_url,
        user=username,
        key=password,
        os_options=os_options,
        auth_version='3' # Crucial: always specify auth_version for v3 or v2 auth
    )

    # Example: List containers
    _resp_headers, containers = conn.get_account()
    print(f"Successfully connected. Found {len(containers)} containers:")
    for container in containers:
        print(f"  - {container['name']} (Bytes: {container['bytes']}, Count: {container['count']})")

    # Example: Create a new container
    container_name = "my-new-test-container"
    try:
        conn.put_container(container_name)
        print(f"Container '{container_name}' created (or already exists).")
    except Exception as e:
        print(f"Error creating container {container_name}: {e}")
    
    # Example: Upload an object (simple string content)
    object_name = "hello_world.txt"
    object_content = b"Hello, Swift Object Storage!"
    try:
        conn.put_object(container_name, object_name, object_content)
        print(f"Object '{object_name}' uploaded to '{container_name}'.")
    except Exception as e:
        print(f"Error uploading object {object_name}: {e}")

finally:
    # Always close the connection
    if 'conn' in locals():
        conn.close()
        print("Connection closed.")

view raw JSON →