杰瑞科技汇

Python如何使用swiftclient?

Of course! Let's dive into the python-swiftclient. This is a crucial tool for anyone working with OpenStack Object Storage, also known as Swift.

Python如何使用swiftclient?-图1
(图片来源网络,侵删)

What is python-swiftclient?

python-swiftclient is a command-line client and a Python library that allows you to interact with an OpenStack Swift object storage service.

Think of Swift as a highly scalable, durable, and redundant storage system, similar in concept to Amazon S3 or Azure Blob Storage. It's designed to store "objects" (which can be files of any type) in "containers" (which are like buckets or folders).

The python-swiftclient package gives you two main ways to work with Swift:

  1. A Command-Line Interface (CLI): A set of commands you can run directly in your terminal. This is great for quick tasks, scripting, and automation.
  2. A Python Library: A set of functions and classes you can import and use within your own Python applications. This is ideal for building custom tools, integrating Swift into larger applications, or performing complex operations.

Installation

First, you need to install the package. It's available on PyPI, so you can use pip:

pip install python-swiftclient

Prerequisites: Authentication

Before you can use swiftclient, you need to authenticate with your OpenStack cloud. This is typically done using environment variables. The client needs to know your OpenStack credentials.

You can get these from a file (usually clouds.yaml or openrc.sh) provided by your cloud administrator. The most important variables are:

  • OS_AUTH_URL: The URL of the OpenStack Identity (Keystone) service.
  • OS_PROJECT_NAME: Your project name (or OS_TENANT_NAME for older clouds).
  • OS_USERNAME: Your username.
  • OS_PASSWORD: Your password.
  • OS_REGION_NAME: The region your Swift service is in.
  • OS_USER_DOMAIN_NAME & OS_PROJECT_DOMAIN_NAME: Your domain names (required for v3 Keystone).

A common way to set these is by sourcing a file:

# This file would contain 'export OS_AUTH_URL=...', 'export OS_PROJECT_NAME=...', etc.
source path/to/your/openrc.sh

Part 1: Using the Command-Line Interface (CLI)

The CLI is accessed via the swift command after installation. Here are the most common operations.

List Containers

This shows you all the containers you have access to.

swift list

Create a Container

A container is like a bucket. You must create one before you can upload objects into it.

swift create my-test-container

Upload a File (Object)

You can upload a single file or an entire directory.

Upload a single file:

swift upload my-test-container my-local-file.txt

Upload a directory (the -r flag is for recursive):

swift upload my-test-container my-local-folder/ -r

List Objects in a Container

Shows you the files (objects) inside a specific container.

swift list my-test-container

Download an Object

Download a single object to your current directory.

swift download my-test-container my-local-file.txt

Download a Container (with -r)

Download all objects from a container.

swift download my-test-container -r

Delete an Object

Be careful! This is permanent.

swift delete my-test-container my-local-file.txt

Delete a Container

You can only delete a container if it is empty.

swift delete my-test-container

Part 2: Using the Python Library

This is where the real power lies for developers. You can import the library and perform all the same actions programmatically.

First, make sure your environment variables are set (as described in the prerequisites). The library will automatically use them.

Basic Example: Upload and Download a File

Here's a simple Python script that connects to Swift, uploads a file, lists the contents, and then downloads it back.

import swiftclient
import os
# --- Configuration ---
# The library automatically uses OS_AUTH_URL, OS_USERNAME, etc. from environment variables.
# If you need to set them manually in the script (not recommended for production):
# auth_url = 'https://your-auth-url.com:5000/v3'
# project_name = 'my-project'
# username = 'my-user'
# password = 'my-password'
# user_domain_name = 'Default'
# project_domain_name = 'Default'
# region_name = 'RegionOne'
container_name = 'my-python-container'
object_name = 'hello_from_python.txt'
local_file_to_upload = 'upload_me.txt'
downloaded_file = 'downloaded_hello.txt'
# Ensure the file to upload exists
with open(local_file_to_upload, 'w') as f:
    f.write("Hello from the python-swiftclient library!")
try:
    # 1. Authenticate and get a connection object
    # The service_type is 'object-store' for Swift.
    # The auth_version is typically 3 for modern clouds.
    conn = swiftclient.Connection(
        # auth_url=auth_url,
        # project_name=project_name,
        # user_domain_name=user_domain_name,
        # project_domain_name=project_domain_name,
        # username=username,
        # password=password,
        # region_name=region_name,
        # auth_version='3'
    )
    print("Successfully connected to Swift!")
    # 2. Create a container (it's idempotent, so no error if it already exists)
    conn.put_container(container_name)
    print(f"Container '{container_name}' created or already exists.")
    # 3. Upload a file (an object)
    with open(local_file_to_upload, 'rb') as upload_file:
        conn.put_object(container_name, object_name, contents=upload_file.read())
    print(f"File '{object_name}' uploaded to container '{container_name}'.")
    # 4. List objects in the container
    print(f"\nObjects in '{container_name}':")
    for obj in conn.get_container(container_name)[1]:
        print(f"- {obj['name']}")
    # 5. Download the object
    # get_object returns a tuple: (headers, response_body)
    headers, obj_contents = conn.get_object(container_name, object_name)
    with open(downloaded_file, 'wb') as download_file:
        download_file.write(obj_contents)
    print(f"\nFile '{object_name}' downloaded as '{downloaded_file}'.")
    # 6. Delete the object
    conn.delete_object(container_name, object_name)
    print(f"Object '{object_name}' deleted.")
    # 7. Delete the container
    conn.delete_container(container_name)
    print(f"Container '{container_name}' deleted.")
except swiftclient.ClientException as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up the local test files
    if os.path.exists(local_file_to_upload):
        os.remove(local_file_to_upload)
    if os.path.exists(downloaded_file):
        os.remove(downloaded_file)
    print("\nCleaned up local files.")

Other Useful Library Functions

  • get_account(): Get information about your account (e.g., total storage used).
  • post_account() / post_object(): Set metadata on your account or objects.
  • head_container() / head_object(): Get headers for a container or object without downloading its contents (useful for checking size, content-type, etc.).
  • upload_object(): A more advanced method for large files that supports chunking and progress callbacks.

Key Concepts vs. S3

If you're familiar with Amazon S3, here's a quick translation:

Swift Concept S3 Concept Description
Container Bucket A logical grouping for objects.
Object Object / File The item you store, including its content and metadata.
Account Account / User The top-level entity that owns containers.
URL Endpoint The base URL for accessing your objects (e.g., https://swift.example.com/v1/AUTH_account_id/container/object).

When to Use python-swiftclient?

  • CLI: For system administration, backups, data migration, and simple automation tasks.
  • Library: When you need to:
    • Build a custom data management application.
    • Integrate Swift storage into a larger Python-based platform.
    • Perform complex, conditional logic based on the contents of Swift.
    • Create a user interface for managing files in Swift.

Alternatives

  • python-openstacksdk: This is a newer, more comprehensive library from the OpenStack project. It provides a unified way to interact with all OpenStack services (Nova, Cinder, Neutron, Swift, etc.). If your application needs to interact with more than just Swift, this is often the better choice. It has a higher-level, more "Pythonic" API than swiftclient.
  • SDKs for specific clouds: Public clouds like OVH, Rackspace, etc., often provide their own SDKs which might wrap python-swiftclient or python-openstacksdk.

In summary, python-swiftclient is the direct, official, and lightweight way to interact with Swift. It's perfect for dedicated Swift tasks and is a fundamental tool in the OpenStack ecosystem.

分享:
扫描分享到社交APP
上一篇
下一篇