Volcengine TOS SDK for Python

2.9.0 · active · verified Thu Apr 16

The `tos` Python SDK enables developers to interact with Volcengine Object Storage (TOS), allowing for operations on buckets and objects. It provides a programmatic interface for cloud storage functionalities like uploading, downloading, and managing files. The library is actively maintained, with version 2.9.0 released recently, indicating a consistent development cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `TosClientV2` using environment variables for authentication and endpoint configuration. It then performs a basic operation like listing buckets and checking for an object, including essential error handling for both client-side and server-side issues.

import os
import tos
from tos.exceptions import TosClientError, TosServerError

# Ensure environment variables are set for authentication
# os.environ['TOS_ACCESS_KEY'] = 'YOUR_ACCESS_KEY'
# os.environ['TOS_SECRET_KEY'] = 'YOUR_SECRET_KEY'
# os.environ['TOS_ENDPOINT'] = 'http://tos-cn-beijing.volces.com' # Example endpoint
# os.environ['TOS_REGION'] = 'cn-beijing' # Example region

ak = os.environ.get('TOS_ACCESS_KEY', '')
sk = os.environ.get('TOS_SECRET_KEY', '')
endpoint = os.environ.get('TOS_ENDPOINT', '')
region = os.environ.get('TOS_REGION', '')

bucket_name = 'your-example-bucket'
object_key = 'your-example-object'

client = None
try:
    # Initialize TosClientV2 with credentials and endpoint
    client = tos.TosClientV2(ak, sk, endpoint, region)
    
    # Example: List buckets
    print(f"Listing buckets for endpoint: {endpoint}")
    response = client.list_buckets()
    print("Buckets:")
    for bucket in response.buckets:
        print(f"- {bucket.name}")

    # Example: Check if a specific object exists (simplified for quickstart)
    try:
        response = client.head_object(bucket_name, object_key)
        print(f"Object '{object_key}' exists in bucket '{bucket_name}'.")
    except TosServerError as e:
        if e.status == 404:
            print(f"Object '{object_key}' not found in bucket '{bucket_name}'.")
        else:
            raise # Re-raise other server errors

except TosClientError as e:
    print(f"Client-side error: {e}")
except TosServerError as e:
    print(f"Server-side error: Status {e.status}, Code {e.code}, Message: {e.message}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    if client: # Ensure client is closed if it manages connections
        pass # No explicit close method documented for TosClientV2 in examples, usually handled internally

view raw JSON →