Alibaba Cloud Key Management Service (KMS) SDK

2.4.0 · active · verified Fri Apr 17

The `alibabacloud-kms20160120` library provides an SDK for interacting with Alibaba Cloud's Key Management Service (KMS) API version 2016-01-20. It allows developers to programmatically manage encryption keys, perform cryptographic operations, and integrate KMS into their applications. The current version is `2.4.0`, and it typically sees updates in response to API changes or underlying SDK framework improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the KMS client using Access Key ID and Secret, and then make a basic API call to create an encryption key. It's recommended to retrieve credentials from environment variables for security. Replace `YOUR_ACCESS_KEY_ID` and `YOUR_ACCESS_KEY_SECRET` with your actual Alibaba Cloud credentials.

import os
from alibabacloud_kms20160120.client import Client as KmsClient
from alibabacloud_tea_openapi.models import Config
from alibabacloud_kms20160120.models import CreateKeyRequest
from alibabacloud_tea_util.models import RuntimeOptions

# Ensure you set these environment variables or replace with actual values
ACCESS_KEY_ID = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', 'YOUR_ACCESS_KEY_ID')
ACCESS_KEY_SECRET = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'YOUR_ACCESS_KEY_SECRET')
REGION_ID = os.environ.get('ALIBABA_CLOUD_REGION_ID', 'cn-hangzhou')

try:
    # Initialize client configuration
    config = Config(
        access_key_id=ACCESS_KEY_ID,
        access_key_secret=ACCESS_KEY_SECRET,
        region_id=REGION_ID
    )

    # Create KMS client
    client = KmsClient(config)

    # Example: Create a new encryption key
    create_key_request = CreateKeyRequest(
        description='My Test Key via Python SDK',
        key_usage='ENCRYPT/DECRYPT'
    )

    runtime = RuntimeOptions()
    response = client.create_key_with_options(create_key_request, runtime)

    print(f"Key created successfully: {response.body.key_id}")
    print(f"Key ARN: {response.body.key_arn}")

except Exception as e:
    print(f"An error occurred: {e}")
    # In production, handle specific exceptions like ClientException, ServerException

view raw JSON →