IBM Cloud Object Storage SDK for Python
The IBM Cloud Object Storage SDK for Python (ibm-cos-sdk) is a Python package allowing developers to interact with IBM Cloud Object Storage. It is a fork of the popular boto3 library, adapted for IBM Cloud's IAM authentication, and provides an S3-compatible API. The library is actively maintained with frequent updates, currently at version 2.16.1, adding features like S3 Object Lock Governance Mode and Checksums Support.
Warnings
- breaking Migration from 1.x to 2.x versions of ibm-cos-sdk requires changing import statements from `boto3` to `ibm_boto3`. This was introduced in version 2.0 to allow co-existence with the official `boto3` library for AWS services.
- gotcha When creating a client, ensure all required IBM-specific parameters (`ibm_api_key_id`, `ibm_service_instance_id`, `ibm_auth_endpoint`, `endpoint_url`) are provided, and `config=Config(signature_version='oauth')` is included for API key authentication. Missing or incorrect parameters will result in authentication failures.
- deprecated Support for Python 2.7 and Python 3.4 has been dropped. Newer versions of the SDK require Python 3.8 or higher.
- gotcha The SDK aligns with `boto3` and `botocore` versions, meaning while it offers an S3-compatible API, specific `boto3`/`botocore` versions used by `ibm-cos-sdk` might not always perfectly match the very latest AWS versions. Compatibility issues could arise if expecting exact feature parity or behavior from a specific `boto3` version.
Install
-
pip install ibm-cos-sdk
Imports
- ibm_boto3
import ibm_boto3
- Config
from ibm_botocore.client import Config
Quickstart
import os
import ibm_boto3
from ibm_botocore.client import Config
# --- Environment Variables (Replace with your actual credentials/endpoints) ---
# Get these values from your IBM Cloud Object Storage service credentials.
# Set them as environment variables or replace directly in code (not recommended for production).
API_KEY = os.environ.get('IBM_COS_API_KEY_ID', 'YOUR_API_KEY_ID')
SERVICE_INSTANCE_ID = os.environ.get('IBM_COS_RESOURCE_INSTANCE_ID', 'YOUR_SERVICE_INSTANCE_ID')
AUTH_ENDPOINT = os.environ.get('IBM_COS_AUTH_ENDPOINT', 'https://iam.cloud.ibm.com/identity/token') # e.g., 'https://iam.cloud.ibm.com/identity/token'
SERVICE_ENDPOINT = os.environ.get('IBM_COS_ENDPOINT', 'https://s3.us.cloud-object-storage.appdomain.cloud') # e.g., 'https://s3.us.cloud-object-storage.appdomain.cloud'
if not all([API_KEY, SERVICE_INSTANCE_ID, AUTH_ENDPOINT, SERVICE_ENDPOINT]):
print("Error: Missing one or more required environment variables for IBM COS configuration.")
print("Please set IBM_COS_API_KEY_ID, IBM_COS_RESOURCE_INSTANCE_ID, IBM_COS_AUTH_ENDPOINT, IBM_COS_ENDPOINT.")
exit(1)
try:
# Create client object
cos_client = ibm_boto3.client(
's3',
ibm_api_key_id=API_KEY,
ibm_service_instance_id=SERVICE_INSTANCE_ID,
ibm_auth_endpoint=AUTH_ENDPOINT,
config=Config(signature_version='oauth'),
endpoint_url=SERVICE_ENDPOINT
)
print("Successfully connected to IBM Cloud Object Storage. Listing buckets...")
# List buckets
response = cos_client.list_buckets()
for bucket in response['Buckets']:
print(f"Bucket Name: {bucket['Name']}")
# Example: Upload a simple text file
bucket_name = "my-test-bucket-12345" # Replace with an existing bucket or create a new one
object_name = "hello_cos.txt"
data = "Hello from IBM COS Python SDK!"
try:
cos_client.put_object(Bucket=bucket_name, Key=object_name, Body=data)
print(f"Successfully uploaded '{object_name}' to bucket '{bucket_name}'.")
# Example: Download the file
obj = cos_client.get_object(Bucket=bucket_name, Key=object_name)
downloaded_data = obj['Body'].read().decode('utf-8')
print(f"Downloaded '{object_name}': {downloaded_data}")
except cos_client.exceptions.NoSuchBucket:
print(f"Warning: Bucket '{bucket_name}' not found. Skipping upload/download examples.")
except Exception as e:
print(f"An error occurred during object operations: {e}")
except Exception as e:
print(f"Failed to connect to IBM Cloud Object Storage: {e}")