Tüm Ürünler
Search
Belge Merkezi

Key Management Service:Gizli değer alma örnek kodu

Son Güncelleme:Oct 04, 2026

Python 3 SDK kullanarak GetSecretValue API'sini çağırın ve bir KMS örneğinden gizli değer alın.

Python 2 için GitHub kaynağına bakın. Python 3 için GitHub kaynağına bakın.

Ön koşullar

Başlamadan önce KMS örnek SDK istemcinizin başlatıldığını doğrulayın. Bkz. İstemciyi başlatma.

Tam örnek

Aşağıdaki örnek bir istemci başlatır ve adıyla bir gizli değeri almak için GetSecretValue API'sini çağırır.

# -*- coding: utf-8 -*-
"""
Retrieve a secret value from a KMS instance using the GetSecretValue API.
"""
import os

from openapi.models import Config
from openapi_util.models import RuntimeOptions
from sdk.client import Client
from sdk.models import GetSecretValueRequest

# Configure the client
config = Config()
# KMS instance services only allow access through HTTPS.
config.protocol = "https"
# Path to your Client Key file.
config.client_key_file = "<CLIENT_KEY_FILE>"
# Client Key decryption password, read from an environment variable.
config.password = os.getenv('CLIENT_KEY_PASSWORD')
# Endpoint format: <your-kms-instance-id>.cryptoservice.kms.aliyuncs.com
config.endpoint = "<ENDPOINT>"
client = Client(config)

def get_secret_value(secret_name):
    request = GetSecretValueRequest()
    request.secret_name = secret_name

    runtime_options = RuntimeOptions()
    # Ignore server-side certificate.
    # runtime_options.ignore_ssl = True
    # Path to the KMS instance CA certificate.
    runtime_options.verify = "<CA_CERTIFICATE_FILE_PATH>"

    resp = client.get_secret_value_with_options(request, runtime_options)
    print(resp)

secret_name = "<SECRET_NAME>"
get_secret_value(secret_name)

Aşağıdaki yer tutucuları gerçek değerlerinizle değiştirin:

Yer Tutucu Açıklama Örnek
<CLIENT_KEY_FILE> İstemci Anahtarı dosyanızın yolu /etc/kms/clientKey.json
<ENDPOINT> KMS örnek uç noktası kms-instance-id.cryptoservice.kms.aliyuncs.com
<CA_CERTIFICATE_FILE_PATH> KMS örnek CA sertifikasının yolu /etc/kms/ca.pem
<SECRET_NAME> Alınacak gizli değerin adı my-db-password

Örnek adım adım açıklama

İstemciyi başlatma

import os

from openapi.models import Config
from sdk.client import Client

config = Config()
# KMS instance services only allow access through HTTPS.
config.protocol = "https"
# Path to your Client Key file.
config.client_key_file = "<CLIENT_KEY_FILE>"
# Client Key decryption password, read from an environment variable.
config.password = os.getenv('CLIENT_KEY_PASSWORD')
# Endpoint format: <your-kms-instance-id>.cryptoservice.kms.aliyuncs.com
config.endpoint = "<ENDPOINT>"
client = Client(config)

CLIENT_KEY_PASSWORD değişkeni, kimlik bilgilerinin kaynak kodunuzun dışında kalmasını sağlamak amacıyla sabit kodlanmak yerine bir ortam değişkeninden okunur.

GetSecretValue API'sini çağırma

def get_secret_value(secret_name):
    request = GetSecretValueRequest()
    request.secret_name = secret_name

    runtime_options = RuntimeOptions()
    # Ignore server-side certificate.
    # runtime_options.ignore_ssl = True
    # Path to the KMS instance CA certificate.
    runtime_options.verify = "<CA_CERTIFICATE_FILE_PATH>"

    resp = client.get_secret_value_with_options(request, runtime_options)
    print(resp)

get_secret_value_with_options yöntemi bir GetSecretValueRequest nesnesi ve bir RuntimeOptions nesnesi alır. verify alanı, SDK'nın sunucu bağlantısını doğrulamak için kullandığı KMS örnek CA sertifikasını gösterir.