Setelah menginisialisasi SDK client instance KMS, Anda dapat memanggil API GetSecretValue untuk mengambil nilai rahasia. Topik ini menyediakan contoh kode terkait.
Sumber kode Github:
Python 2: get_secret_value_sample.py.
Python 3: get_secret_value_sample.py.
Contoh dalam topik ini menggunakan Python 3.
Contoh lengkap
Penjelasan contoh
Inisialisasi client
# -*- coding: utf-8 -*-
from openapi.models import Config
from sdk.client import Client
config = Config()
# Protokol koneksi. Atur nilainya menjadi https. Layanan instance KMS hanya mengizinkan akses melalui protokol HTTPS.
config.protocol = "https"
# Client Key.
config.client_key_file = "<CLIENT_KEY_FILE>"
# Kata sandi dekripsi Client Key.
config.password = os.getenv('CLIENT_KEY_PASSWORD')
# Titik akhir instance KMS Anda. Atur nilainya dalam format berikut: <ID instance KMS Anda>.cryptoservice.kms.aliyuncs.com.
config.endpoint = "<ENDPOINT>"
client = Client(config)Panggil GetSecretValue API
def get_secret_value(secret_name):
request = GetSecretValueRequest()
request.secret_name = secret_name
runtime_options = RuntimeOptions()
# Abaikan sertifikat sisi server.
# runtime_options.ignore_ssl = True
# verify menunjukkan jalur sertifikat CA instance.
runtime_options.verify = "<CA_CERTIFICATE_FILE_PATH>"
resp = client.get_secret_value_with_options(request, runtime_options)
print(resp)