IBM COS SDK Core for Python

2.16.1 · active · verified Sun Apr 12

ibm-cos-sdk-core provides the low-level, data-driven core functionality for the IBM Cloud Object Storage (COS) SDK for Python. It is based on `botocore` and exposes similar low-level client interfaces. The current version is 2.16.1. It has a moderate release cadence, with several patch and minor releases per year, often aligning with `botocore` updates and adding new COS features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a low-level S3-compatible client using `ibm-cos-sdk-core`'s session and client creation capabilities. It shows how to map IBM Cloud Object Storage credentials (API Key and Service Instance ID) to the `aws_access_key_id` and `aws_secret_access_key` parameters required by the S3-compatible client. The example then attempts to list available buckets. Most users are recommended to use the higher-level `ibm-cos-sdk` package for simpler interactions.

import os
from ibm_cos_sdk_core.session import get_session

# Configure IBM Cloud Object Storage credentials and endpoint
# These usually map to 'aws_access_key_id' and 'aws_secret_access_key' for S3 compatibility
IBM_API_KEY_ID = os.environ.get('IBM_COS_API_KEY_ID', 'YOUR_IBM_API_KEY')
IBM_SERVICE_INSTANCE_ID = os.environ.get('IBM_COS_SERVICE_INSTANCE_ID', 'YOUR_IBM_SERVICE_INSTANCE_ID')
IBM_COS_REGION = os.environ.get('IBM_COS_REGION', 'us-south')
IBM_COS_ENDPOINT = os.environ.get('IBM_COS_ENDPOINT', f'https://s3.{IBM_COS_REGION}.cloud-object-storage.appdomain.cloud')

if 'YOUR_' in IBM_API_KEY_ID or 'YOUR_' in IBM_SERVICE_INSTANCE_ID:
    print("Please set IBM_COS_API_KEY_ID and IBM_COS_SERVICE_INSTANCE_ID environment variables for authentication.")
    print("Falling back to dummy credentials. This example will likely fail without proper auth.")

# Get a session object
session = get_session()

# Create a low-level client for the S3-compatible service
# Note: IBM COS uses 's3' service model for its S3-compatible API
try:
    client = session.create_client(
        's3',
        region_name=IBM_COS_REGION,
        endpoint_url=IBM_COS_ENDPOINT,
        aws_access_key_id=IBM_API_KEY_ID,
        aws_secret_access_key=IBM_SERVICE_INSTANCE_ID
    )

    # Example: List buckets
    print(f"Attempting to list buckets in region {IBM_COS_REGION}...")
    response = client.list_buckets()
    print("Buckets:")
    for bucket in response.get('Buckets', []):
        print(f"  - {bucket['Name']}")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure your IBM COS credentials and endpoint are correctly configured.")

view raw JSON →