Alibaba Cloud OSS SDK V2 for Python
alibabacloud-oss-v2 is the second major version of the Alibaba Cloud OSS (Object Storage Service) SDK for Python. It is a significant rewrite, aiming to simplify underlying operations like authentication, automatic request retries, and error handling, while offering flexible parameter configuration and advanced features such as paginators and transmission managers. The library is currently at version 1.2.5 and receives frequent updates, indicating active development.
Warnings
- breaking The `alibabacloud-oss-v2` SDK is a complete rewrite and is not compatible with the previous `aliyun-oss-python-sdk` (or `oss2`) SDK. Migrating from V1 to V2 will require significant code changes.
- breaking In version 1.1.3, the type of `MetaQuery.media_types` was changed to `MetaQueryMediaTypes`. This is a type-hinting or strict-typing breaking change if you were directly using this attribute.
- gotcha Starting March 20, 2025, new OSS users in Chinese mainland regions must use a custom domain name (CNAME) for data API operations, as default public endpoints are restricted. You must bind a valid SSL Certificate to your custom domain if accessing via HTTPS.
- gotcha Avoid hardcoding Alibaba Cloud credentials directly in your code. It's a security best practice to use environment variables or Alibaba Cloud RAM roles for authentication.
- gotcha For uploading large files (typically >100MB to 5GB, depending on best practices), it's highly recommended to use multipart upload for better reliability and performance.
Install
-
pip install alibabacloud-oss-v2
Imports
- Client
from alibabacloud_oss_v2 import client as oss_client from alibabacloud_oss_v2 import models as oss_models from alibabacloud_oss_v2 import config as oss_config from alibabacloud_oss_v2.credentials import EnvironmentVariableCredentialsProvider
Quickstart
import os
from alibabacloud_oss_v2 import client as oss_client
from alibabacloud_oss_v2 import models as oss_models
from alibabacloud_oss_v2 import config as oss_config
from alibabacloud_oss_v2.credentials import EnvironmentVariableCredentialsProvider
# Set environment variables for authentication (replace with your actual region and credentials)
# os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'] = 'your_access_key_id'
# os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'] = 'your_access_key_secret'
# os.environ['ALIBABA_CLOUD_REGION_ID'] = 'cn-hangzhou'
region = os.environ.get('ALIBABA_CLOUD_REGION_ID', 'cn-hangzhou')
try:
# Loading credentials from environment variables
credentials_provider = EnvironmentVariableCredentialsProvider()
# Using the SDK's default configuration
cfg = oss_config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = region
client = oss_client.Client(cfg)
# Create a Paginator for the ListBuckets operation
paginator = client.list_buckets_paginator()
print(f"Listing buckets in region: {region}")
# Iterate through the bucket pages
found_buckets = False
for page in paginator.iter_page(oss_models.ListBucketsRequest()):
for bucket_info in page.buckets:
print(f'Bucket: {bucket_info.name}, Location: {bucket_info.location}, Creation Date: {bucket_info.creation_date}')
found_buckets = True
if not found_buckets:
print("No buckets found or access denied.")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are set.")