文档

Python SDK参考

更新时间:

本文为您介绍如何安装并使用Hologres SDK for Python。

在线调试和生成SDK示例

OpenAPI门户提供在线调用云产品API、动态生成SDK示例代码和快速检索接口等功能,可以显著降低使用API的难度。

前提条件

安装Alibaba Cloud SDK for Python

您可以通过pip安装Python SDK,详细说明请参见其他方式开始使用

pip install alibabacloud_hologram20220601==1.0.5

请求步骤

  1. 导入相关产品的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
  2. 设置AccessKey和域名,域名详情请参见Hologres服务区域列表

    说明

    建议使用环境变量的方式调用用户名和密码,降低密码泄露风险。

    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)
  3. 新建AcsClient。

    client = Client(config)
  4. 创建Request对象(以查询实例详情GetInstance为例,1.0.3及以上版本无GetInstanceRequest),并设置请求参数。

    get_instance_request = GetInstanceRequest()
  5. 调用返回结果。

    # 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:
		# 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_USER 和 ALIBABA_CLOUD_PASSWORD。
        # 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式。
        client = Sample.create_client(os.environ['ALIBABA_CLOUD_USER'],
                                      os.environ['ALIBABA_CLOUD_PASSWORD'])      
        try:
            # 复制代码运行请自行打印 API 的返回值
            client.get_instance('instanceId')
        except Exception as error:
            # 如有需要,请打印 error
            print("error: ", error)
            

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
		# 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_USER 和 ALIBABA_CLOUD_PASSWORD。
        # 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式。
        client = Sample.create_client(os.environ['ALIBABA_CLOUD_USER'],
                                      os.environ['ALIBABA_CLOUD_PASSWORD'])
        try:
            # 复制代码运行请自行打印 API 的返回值
            await client.get_instance('instanceId')
        except Exception as error:
            # 如有需要,请打印 error
            print("error: ", error)


if __name__ == '__main__':
    Sample.main(sys.argv[1:])

返回结果示例:

{
  "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
}