Alibaba Cloud Credentials

1.0.8 · active · verified Thu Apr 09

The alibabacloud-credentials library is the core module for managing authentication credentials within the Alibaba Cloud Python SDK. It provides various credential providers (AccessKey, STS, RAM, EcsRamRole, OAuth, CloudSSO) and handles credential loading from environment variables, configuration files, and metadata services. The current version is 1.0.8, and it maintains a regular release cadence with several patches and minor features per year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `DefaultCredentialsProvider` to automatically resolve and retrieve Alibaba Cloud credentials. It first initializes a `Config` object (which can be pre-configured or left empty for automatic resolution) and then creates a `DefaultCredentialsProvider`. It attempts to fetch and print the Access Key ID, Access Key Secret, and Security Token. For actual use, ensure environment variables like `ALIBABA_CLOUD_ACCESS_KEY_ID` and `ALIBABA_CLOUD_ACCESS_KEY_SECRET` are set, or a valid `.aliyun/config.json` file is present.

import os
from alibabacloud_credentials.models import Config
from alibabacloud_credentials.client import DefaultCredentialsProvider

# Configure credentials - this provider will try to resolve credentials
# from environment variables, config files, instance metadata service, etc.
# You can explicitly set access_key_id, access_key_secret, security_token in Config
# if you don't want automatic resolution.
# For example:
# config = Config(
#     type='access_key',
#     access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', 'YOUR_AK_ID'),
#     access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'YOUR_AK_SECRET')
# )

config = Config()
provider = DefaultCredentialsProvider(config)

try:
    access_key_id = provider.get_access_key_id()
    access_key_secret = provider.get_access_key_secret()
    security_token = provider.get_security_token()

    print(f"Access Key ID: {access_key_id}")
    print(f"Access Key Secret: {access_key_secret[:4]}...{access_key_secret[-4:]}")
    if security_token:
        print(f"Security Token: {security_token[:4]}...{security_token[-4:]}")

except Exception as e:
    print(f"Error getting credentials: {e}")
    print("Please ensure ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are set,")
    print("or a valid Alibaba Cloud configuration file exists.")

view raw JSON →