Python SDK參考
更新時間:
本文為您介紹如何安裝并使用Hologres SDK for Python。
在線調(diào)試和生成SDK示例
OpenAPI門戶提供在線調(diào)用云產(chǎn)品API、動態(tài)生成SDK示例代碼和快速檢索接口等功能,可以顯著降低使用API的難度。
前提條件
已創(chuàng)建AccessKey,詳情請參見創(chuàng)建AccessKey。
已安裝Python環(huán)境。
授予賬號相應(yīng)權(quán)限,詳情請參見授予RAM用戶權(quán)限。
安裝Alibaba Cloud SDK for Python
您可以通過pip安裝Python SDK,詳細說明請參見其他方式和開始使用。
pip install alibabacloud_hologram20220601==1.0.5
請求步驟
導(dǎo)入相關(guān)產(chǎn)品的SDK。
from alibabacloud_hologram20220601.client import Client as Hologram20220601Client from alibabacloud_tea_openapi import models as open_api_models from alibabacloud_hologram20220601 import models as hologram_20220601_models from alibabacloud_tea_util import models as util_models from alibabacloud_tea_util.client import Client as UtilClient
設(shè)置AccessKey和域名,域名詳情請參見Hologres服務(wù)區(qū)域列表。
說明建議使用環(huán)境變量的方式調(diào)用用戶名和密碼,降低密碼泄露風險。
config = open_api_models.Config( # 您的AccessKey ID, access_key_id=os.environ['ALIBABA_CLOUD_USER'], # 您的AccessKey Secret, access_key_secret=os.environ['ALIBABA_CLOUD_PASSWORD'] ) # 訪問的域名 config.endpoint = 'hologram.cn-hangzhou.aliyuncs.com' client = Client(config)
新建AcsClient。
client = Client(config)
創(chuàng)建Request對象(以查詢實例詳情GetInstance為例,1.0.3及以上版本無GetInstanceRequest),并設(shè)置請求參數(shù)。
get_instance_request = GetInstanceRequest()
調(diào)用返回結(jié)果。
# 1.0.3之前版本 response = client.get_instance('your_instanceId', get_instance_request) print(response) # 1.0.3及以上版本 response = client.get_instance('your_instanceId') print(response)
參考示例
查詢實例詳情請求示例(適用于1.0.3及以上版本):
# -*- coding: utf-8 -*-
import sys
import os
from typing import List
from alibabacloud_hologram20220601.client import Client as Hologram20220601Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
class Sample:
def __init__(self):
pass
@staticmethod
def create_client(
access_key_id: str,
access_key_secret: str,
) -> Hologram20220601Client:
"""
使用AK&SK初始化賬號Client
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# 必填,您的 AccessKey ID,
access_key_id=access_key_id,
# 必填,您的 AccessKey Secret,
access_key_secret=access_key_secret
)
# 訪問的域名
config.endpoint = f'hologram.cn-hangzhou.aliyuncs.com'
return Hologram20220601Client(config)
@staticmethod
def main(
args: List[str],
) -> None:
# 請確保代碼運行環(huán)境設(shè)置了環(huán)境變量 ALIBABA_CLOUD_USER 和 ALIBABA_CLOUD_PASSWORD。
# 工程代碼泄露可能會導(dǎo)致 AccessKey 泄露,并威脅賬號下所有資源的安全性。以下代碼示例使用環(huán)境變量獲取 AccessKey 的方式進行調(diào)用,僅供參考,建議使用更安全的 STS 方式。
client = Sample.create_client(os.environ['ALIBABA_CLOUD_USER'],
os.environ['ALIBABA_CLOUD_PASSWORD'])
try:
# 復(fù)制代碼運行請自行打印 API 的返回值
client.get_instance('instanceId')
except Exception as error:
# 如有需要,請打印 error
print("error: ", error)
@staticmethod
async def main_async(
args: List[str],
) -> None:
# 請確保代碼運行環(huán)境設(shè)置了環(huán)境變量 ALIBABA_CLOUD_USER 和 ALIBABA_CLOUD_PASSWORD。
# 工程代碼泄露可能會導(dǎo)致 AccessKey 泄露,并威脅賬號下所有資源的安全性。以下代碼示例使用環(huán)境變量獲取 AccessKey 的方式進行調(diào)用,僅供參考,建議使用更安全的 STS 方式。
client = Sample.create_client(os.environ['ALIBABA_CLOUD_USER'],
os.environ['ALIBABA_CLOUD_PASSWORD'])
try:
# 復(fù)制代碼運行請自行打印 API 的返回值
await client.get_instance('instanceId')
except Exception as error:
# 如有需要,請打印 error
print("error: ", error)
if __name__ == '__main__':
Sample.main(sys.argv[1:])
返回結(jié)果示例:
{
"RequestId": "1E8EFA1E-xxxx-xxxx-xxxx-xxxx",
"Instance": {
"SuspendReason": "",
"ZoneId": "cn-hangzhou-h",
"InstanceId": "hgpostcn-cn-xxxxxxxxxxxx",
"InstanceChargeType": "PostPaid",
"Memory": 64,
"InstanceOwner": "1239xxxxxxxxxxxx",
"ColdStorage": 0,
"Cpu": 16,
"EnableHiveAccess": false,
"InstanceName": "xxxx",
"ComputeNodeCount": 1,
"Endpoints": [
{
"Type": "Intranet",
"Endpoint": "hgpostcn-cn-xxxxxxxxxxxx-cn-hangzhou-internal.hologres.aliyuncs.com:80",
"Enabled": true
},
{
"Type": "VPCSingleTunnel",
"Endpoint": "hgpostcn-cn-xxxxxxxxxxxx-cn-hangzhou-vpc-st.hologres.aliyuncs.com:80",
"VpcId": "vpc-xxxxxxxxxxxxxxxx",
"VSwitchId": "vsw-xxxxxxxxxxxxxxxx",
"Enabled": true,
"VpcInstanceId": "hgpostcn-cn-xxxxxxxxxxxx-frontend-st"
}
],
"AutoRenewal": false,
"Version": "r1.3.43",
"InstanceStatus": "Running",
"CommodityCode": "hologram_postpay_public_cn",
"CreationTime": "2023-04-12T03:41:59.036Z",
"InstanceType": "Standard",
"Disk": 0,
"Tags": [
{
"Value": "rg-xxxxxxxxxxxx",
"Key": "acs:rm:rgId"
}
]
},
"HttpStatusCode": 200,
"Success": true
}
文檔內(nèi)容是否對您有幫助?