not really known
Go to file
Huaweicloud-SDK 5565af7b2a release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-all release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-antiddos release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-aom release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-aos release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-apig release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-apm release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-as release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-bcs release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-bms release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-bss release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-bssintl release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cae release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-campusgo release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cbh release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cbr release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cbs release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cc release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cce release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-ccm release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cdm release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cdn release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-ces release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cfw release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cgs release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-classroom release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cloudartifact release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cloudbuild release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-clouddeploy release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cloudide release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cloudpipeline release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cloudrtc release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cloudtable release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cloudtest release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-codecheck release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-codecraft release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-codehub release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-core release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cph release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cpts release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cse release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-csms release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-css release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-cts release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-das release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-dbss release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-dc release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-dcs release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-ddm release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-dds release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-deh release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-devsecurity release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-devstar release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-dgc release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-dlf release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-dli release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-dms release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-dns release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-dris release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-drs release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-dsc release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-dwr release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-dws release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-ecs release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-eg release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-eihealth release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-eip release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-elb release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-eps release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-er release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-evs release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-frs release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-functiongraph release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-ga release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-gaussdb release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-gaussdbfornosql release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-gaussdbforopengauss release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-ges release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-gsl release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-hilens release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-hss release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-iam release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-iec release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-ief release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-ies release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-image release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-imagesearch release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-ims release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-iotanalytics release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-iotda release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-iotedge release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-ivs release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-kafka release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-kms release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-kps release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-live release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-lts release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-mapds release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-mas release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-meeting release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-moderation release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-mpc release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-mrs release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-nat release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-nlp release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-ocr release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-oms release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-osm release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-projectman release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-rabbitmq release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-rds release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-res release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-rms release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-rocketmq release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-roma release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-sa release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-scm release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-sdrs release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-servicestage release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-sfsturbo release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-sis release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-smn release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-sms release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-swr release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-tms release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-ugo release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-vas release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-vcm release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-vod release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-vpc release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-vpcep release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-vss release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-waf release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
huaweicloud-sdk-workspace release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
CHANGELOG.md release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
CHANGELOG_CN.md release 3.1.18 source code for python 2022-12-22 17:46:54 +08:00
LICENSE release 3.0.1 beta code for python 2020-05-18 15:34:40 +08:00
README.md release 3.0.98 source code for python 2022-07-14 16:55:18 +08:00
README_CN.md release 3.0.98 source code for python 2022-07-14 16:55:18 +08:00
README_PYPI.md release 3.0.8-beta source code for python 2020-08-15 17:41:31 +08:00
__init__.py release 3.0.29-beta source code for python 2020-12-31 18:02:03 +08:00

README.md

English | 简体中文

Huawei Cloud Python Software Development Kit (Python SDK)

The Huawei Cloud Python SDK allows you to easily work with Huawei Cloud services such as Elastic Compute Service (ECS) and Virtual Private Cloud (VPC) without the need to handle API related tasks.

This document introduces how to obtain and use Huawei Cloud Python SDK.

Requirements

  • To use Huawei Cloud Python SDK, you must have Huawei Cloud account as well as the Access Key (AK) and Secret key (SK) of the Huawei Cloud account. You can create an Access Key in the Huawei Cloud console. For more information, see My Credentials.

  • To use Huawei Cloud Python SDK to access the APIs of specific service, please make sure you do have activated the service in Huawei Cloud console if needed.

  • Huawei Cloud Python SDK requires Python 3.3 or later, run command python --version to check the version of Python.

Install Python SDK

You could use pip or source code to install dependencies.

Individual Cloud Service

Take using VPC SDK for example, you need to install huaweicloudsdkvpc library:

  • Use python pip
# Install the VPC management library
pip install huaweicloudsdkvpc
  • Install from source code
# Install the VPC management library
cd huaweicloudsdkvpc-${version}
python setup.py install

Cloud Service Collection Package

You can install huaweicloudsdkall, which will install all SDK supported service packages:

  • Use python pip
pip install huaweicloudsdkall
  • Install from source code
cd huaweicloudsdkall-${version}
python setup.py install

Code example

  • The following example shows how to query a list of VPC in a specific region, you need to substitute your real {Service}Client for VpcClient in actual use.
  • Substitute the values for {your ak string}, {your sk string}, {your endpoint string} and {your project id}.
# coding: utf-8


from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcore.http.http_config import HttpConfig
# import specified service library huaweicloudsdk{service}, take vpc for example
from huaweicloudsdkvpc.v2 import *


def list_vpc(client):
    try:
        request = ListVpcsRequest(limit=1)
        response = client.list_vpcs(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)


if __name__ == "__main__":
    ak = "{your ak string}"
    sk = "{your sk string}"
    endpoint = "{your endpoint}"
    project_id = "{your project id}"

    config = HttpConfig.get_default_config()
    config.ignore_ssl_verification = True
    credentials = BasicCredentials(ak, sk, project_id)

    vpc_client = VpcClient.new_builder() \
        .with_http_config(config) \
        .with_credentials(credentials) \
        .with_endpoint(endpoint) \
        .build()

    list_vpc(vpc_client)

Online Debugging

API Explorer provides api retrieval and online debugging, supports full fast retrieval, visual debugging, help document viewing, and online consultation.

Changelog

Detailed changes for each released version are documented in the CHANGELOG.md.

User Manual 🔝

1. Client Configuration 🔝

1.1 Default Configuration 🔝

from huaweicloudsdkcore.http.http_config import HttpConfig

#  Use default configuration
config = HttpConfig.get_default_config()

1.2 Network Proxy 🔝

# Use Proxy if needed
config.proxy_protocol = 'http'
config.proxy_host = 'proxy.huaweicloud.com'
config.proxy_port = 80
config.proxy_user = 'username'
config.proxy_password = 'password'

1.3 Timeout Configuration 🔝

# The default connection timeout is 60 seconds, the default read timeout is 120 seconds
# Set the connection timeout and read timeout to 120 seconds
config.timeout = 120
# Set the connection timeout to 60 seconds and the read timeout to 120 seconds
config.timeout = (60, 120)

1.4 SSL Certification 🔝

# Skip SSL certifaction checking while using https protocol if needed
config.ignore_ssl_verification = True
# Configure the server's CA certificate for the SDK to verify the legitimacy of the server
config.ssl_ca_cert = ssl_ca_cert

2. Credentials Configuration 🔝

There are two types of Huawei Cloud services, regional services and global services.

Global services contain BSS, DevStar, EPS, IAM, RMS, TMS.

For regional services' authentication, projectId is required to initialize BasicCredentials.

For global services' authentication, domainId is required to initialize GlobalCredentials.

The following authentications are supported:

  • permanent AK&SK
  • temporary AK&SK + SecurityToken
  • IdpId&IdTokenFile

2.1 Use Permanent AK&SK 🔝

Parameter description:

  • ak is the access key ID for your account.
  • sk is the secret access key for your account.
  • project_id is the ID of your project depending on your region which you want to operate.
  • domain_id is the account ID of Huawei Cloud.
# Regional services
basic_credentials = BasicCredentials(ak, sk, project_id)

# Global services
global_credentials = GlobalCredentials(ak, sk, domain_id)

Notice:

  • project_id/domain_id supports automatic acquisition in version 3.0.26-beta or later, if you want to use this feature, you need to provide the ak and sk of your account and the id of the region, and then build your client instance with method with_region(), detailed example could refer to 3.2 Initialize the client with specified Region .

2.2 Use Temporary AK&SK 🔝

It's required to obtain temporary AK&SK and security token first, which could be obtained through permanent AK&SK or through an agency.

Parameter description:

  • ak is the access key ID for your account.
  • sk is the secret access key for your account.
  • security_token is the security token when using temporary AK/SK.
  • project_id is the ID of your project depending on your region which you want to operate.
  • domain_id is the account ID of Huawei Cloud.

After the temporary AK&SK&SecurityToken is successfully obtained, you can use the following example to initialize the authentication:

# Regional services
basic_credentials = BasicCredentials(ak, sk, project_id).with_security_token(security_token)

# Global services
global_credentials = GlobalCredentials(ak, sk, domain_id).with_security_token(security_token)

2.3 Use IdpId&IdTokenFile 🔝

Obtain a federated identity authentication token using an OpenID Connect ID token, refer to the Obtaining a Token with an OpenID Connect ID Token

Parameter description:

  • idp_id Identity provider ID.
  • id_token_file Id token file path. Id token is constructed by the enterprise IdP to carry the identity information of federated users.
  • project_id is the ID of your project depending on your region which you want to operate.
  • domain_id is the account ID of Huawei Cloud.
from huaweicloudsdkcore.auth.credentials import BasicCredentials, GlobalCredentials

# Regional service
basic_cred = BasicCredentials() \
    .with_idp_id(idp_id) \
    .with_id_token_file(id_token_file) \
    .with_project_id(project_id)

# Global service
global_cred = GlobalCredentials() \
    .with_idp_id(idp_id) \
    .with_id_token_file(id_token_file) \
    .with_domain_id(domain_id)

2.4 Authentication Management 🔝

Getting Authentication from providers is supported since v3.0.98

Regional services use XxxCredentialProvider.get_basic_credential_xxx_provider

Global services use XxxCredentialProvider.get_global_credential_xxx_provider

2.4.1 Environment Variables 🔝

AK/SK Auth

Environment Variables Notice
HUAWEICLOUD_SDK_AK RequiredAccessKey
HUAWEICLOUD_SDK_SK RequiredSecretKey
HUAWEICLOUD_SDK_SECURITY_TOKEN Optional, this parameter needs to be specified when using temporary ak/sk
HUAWEICLOUD_SDK_PROJECT_ID Optional, used for regional services, required in multi-ProjectId scenarios
HUAWEICLOUD_SDK_DOMAIN_ID Optional, used for global services

Configure environment variables:

// Linux
export HUAWEICLOUD_SDK_AK=YOUR_AK
export HUAWEICLOUD_SDK_SK=YOUR_SK

// Windows
set HUAWEICLOUD_SDK_AK=YOUR_AK
set HUAWEICLOUD_SDK_SK=YOUR_SK

Get the credentials from configured environment variables:

from huaweicloudsdkcore.auth.provider import EnvCredentialProvider

# basic
basic_provider = EnvCredentialProvider.get_basic_credential_env_provider()
basic_cred = basic_provider.get_credentials()

# global
global_provider = EnvCredentialProvider.get_global_credential_env_provider()
global_cred = global_provider.get_credentials()

IdpId/IdTokenFile Auth

Environment Variables Notice
HUAWEICLOUD_SDK_IDP_ID Required, identity provider Id
HUAWEICLOUD_SDK_ID_TOKEN_FILE Required, id token file path
HUAWEICLOUD_SDK_PROJECT_ID For basic credentials, this parameter is required
HUAWEICLOUD_SDK_DOMAIN_ID For global credentials, this parameter is required

Configure environment variables:

// Linux
export HUAWEICLOUD_SDK_IDP_ID=YOUR_IDP_ID
export HUAWEICLOUD_SDK_ID_TOKEN_FILE=/some_path/your_token_file
export HUAWEICLOUD_SDK_PROJECT_ID=YOUR_PROJECT_ID // For basic credentials, this parameter is required
export HUAWEICLOUD_SDK_DOMAIN_ID=YOUR_DOMAIN_ID // For global credentials, this parameter is required

// Windows
set HUAWEICLOUD_SDK_IDP_ID=YOUR_IDP_ID
set HUAWEICLOUD_SDK_ID_TOKEN_FILE=/some_path/your_token_file
set HUAWEICLOUD_SDK_PROJECT_ID=YOUR_PROJECT_ID // For basic credentials, this parameter is required
set HUAWEICLOUD_SDK_DOMAIN_ID=YOUR_DOMAIN_ID // For global credentials, this parameter is required

Get the credentials from configured environment variables:

from huaweicloudsdkcore.auth.provider import EnvCredentialProvider

# basic
basic_provider = EnvCredentialProvider.get_basic_credential_env_provider()
basic_cred = basic_provider.get_credentials()

# global
global_provider = EnvCredentialProvider.get_global_credential_env_provider()
global_cred = global_provider.get_credentials()
2.4.2 Profile 🔝

The profile will be read from the user's home directory by default, linux~/.huaweicloud/credentials,windowsC:\Users\USER_NAME\.huaweicloud\credentials, the path to the profile can be modified by configuring the environment variable HUAWEICLOUD_SDK_CREDENTIALS_FILE

AK/SK Auth

Configuration Parameters Notice
ak RequiredAccessKey
sk RequiredSecretKey
security_token Optional, this parameter needs to be specified when using temporary ak/sk
project_id Optional, used for regional services, required in multi-ProjectId scenarios
domain_id Optional, used for global services
iam_endpoint optional, endpoint for authentication, default is https://iam.myhuaweicloud.com

The content of the profile is as follows:

[basic]
ak = your_ak
sk = your_sk

[global]
ak = your_ak
sk = your_sk

Get the credentials from profile:

from huaweicloudsdkcore.auth.provider import ProfileCredentialProvider

# basic
basic_provider = ProfileCredentialProvider.get_basic_credential_profile_provider()
basic_cred = basic_provider.get_credentials()

# global
global_provider = ProfileCredentialProvider.get_global_credential_profile_provider()
global_cred = global_provider.get_credentials()

IdpId/IdTokenFile Auth

Configuration Parameters Notice
idp_id Required, identity provider Id
id_token_file Required, id token file path
project_id For basic credentials, this parameter is required
domain_id For global credentials, this parameter is required
iam_endpoint optional, endpoint for authentication, default is https://iam.myhuaweicloud.com

The content of the profile is as follows:

[basic]
idp_id = your_idp_id
id_token_file = /some_path/your_token_file
project_id = your_project_id

[global]
idp_id = your_idp_id
id_token_file = /some_path/your_token_file
domainId = your_domain_id

Get the credentials from profile:

from huaweicloudsdkcore.auth.provider import ProfileCredentialProvider

# basic
basic_provider = ProfileCredentialProvider.get_basic_credential_profile_provider()
basic_cred = basic_provider.get_credentials()

# global
global_provider = ProfileCredentialProvider.get_global_credential_profile_provider()
global_cred = global_provider.get_credentials()
2.4.3 Metadata 🔝

Get temporary AK/SK and securitytoken from instance's metadata. Refer to the Obtaining Metadata for more information.

Manually obtain authentication from instance metadata:

from huaweicloudsdkcore.auth.provider import MetadataCredentialProvider

# basic
basic_provider = MetadataCredentialProvider.get_basic_credential_metadata_provider()
basic_cred = basic_provider.get_credentials()

# global
global_provider = MetadataCredentialProvider.get_global_credential_metadata_provider()
global_cred = global_provider.get_credentials()
2.4.4 Provider Chain 🔝

When creating a service client without credentials, try to load authentication in the order Environment Variables -> Profile -> Metadata

Get authentication from provider chain:

from huaweicloudsdkcore.auth.provider import CredentialProviderChain

# basic
basic_chain = CredentialProviderChain.get_basic_credential_provider_chain()
basic_cred = basic_chain.get_credentials()

# global
global_chain = CredentialProviderChain.get_global_credential_provider_chain()
global_cred = global_chain.get_credentials()

Custom credentials provider chain is supported:

from huaweicloudsdkcore.auth.provider import CredentialProviderChain, ProfileCredentialProvider, MetadataCredentialProvider

providers = [
    ProfileCredentialProvider.get_basic_credential_profile_provider(),
    MetadataCredentialProvider.get_basic_credential_metadata_provider()
]

chain = CredentialProviderChain(providers)
credentials = chain.get_credentials()

3. Client Initialization 🔝

There are two ways to initialize the {Service}Client, you could choose one you preferred.

3.1 Initialize the {Service}Client with specified Endpoint 🔝

# Specify the endpoint, take the endpoint of VPC service in region of cn-north-4 for example
endpoint = "https://vpc.cn-north-4.myhuaweicloud.com"

# Initialize the credentials, you should provide project_id or domain_id in this way, take initializing BasicCredentials for example
basic_credentials = BasicCredentials(ak, sk, project_id)

# Initialize specified service client instance, take initializing the regional service VPC's VpcClient for example
client = VpcClient.new_builder() \
    .with_http_config(config) \
    .with_credentials(basic_credentials) \
    .with_endpoint(endpoint) \
    .build()

where:

  • endpoint varies by services and regions, see Regions and Endpoints to obtain correct endpoint.

  • When you meet some trouble in getting projectId using the specified region way, you could use this way instead.

# dependency for region module
from huaweicloudsdkiam.v3.region.iam_region import IamRegion

# Initialize the credentials, project_id or domain_id could be unassigned in this situation
# Take initializing GlobalCredentials for example
global_credentials = GlobalCredentials(ak, sk)

# Initialize specified service client instance
# Take initializing the global service IAM's IamClient for example
client = IamClient.new_builder() \
    .with_http_config(config) \
    .with_credentials(global_credentials) \
    .with_region(IamRegion.CN_NORTH_4) \
    .build()

Notice:

  • If you use {Service}Region to initialize {Service}Client, project_id/domain_id supports automatic acquisition, you don't need to configure it when initializing Credentials.

  • Multiple ProjectId situation is not supported.

  • Supported region list: af-south-1, ap-southeast-1, ap-southeast-2, ap-southeast-3, cn-east-2, cn-east-3, cn-north-1, cn-north-4, cn-south-1, cn-southwest-2, ru-northwest-2. You may get exception such as Unsupported regionId if your region don't in the list above.

Comparison of the two ways:

Initialization Advantages Disadvantage
Specified Endpoint The API can be invoked successfully once it has been published in the environment. You need to prepare projectId and endpoint yourself.
Specified Region No need for projectId and endpoint, it supports automatic acquisition if you configure it in the right way. The supported services and regions are limited.

3.3 Custom Configuration

Notice: Supported since v3.0.93

3.3.1 IAM endpoint configuration

Automatically acquiring projectId/domainId will invoke the KeystoneListProjects /KeystoneListAuthDomains interface of IAM service. The default iam enpoint is https://iam.myhuaweicloud.com, you can modify the endpoint in the following two ways:

3.3.1.1 Global scope

This configuration takes effect globally, specified by environment variable HUAWEICLOUD_SDK_IAM_ENDPOINT

//linux
export HUAWEICLOUD_SDK_IAM_ENDPOINT=https://iam.cn-north-4.myhuaweicloud.com

//windows
set HUAWEICLOUD_SDK_IAM_ENDPOINT=https://iam.cn-north-4.myhuaweicloud.com
3.3.1.2 Credentials scope

This configuration is only valid for a credential, and it will override the global configuration

from huaweicloudsdkcore.auth.credentials import BasicCredentials

iam_endpoint = "https://iam.cn-north-4.myhuaweicloud.com"
credentials = BasicCredentials(ak, sk).with_iam_endpoint(iam_endpoint)
3.3.2 Region configuration
3.3.2.1 Environment variable

Specified by environment variable, the format is HUAWEICLOUD_SDK_REGION_{SERIVCE_NAME}_{REGION_ID}={endpoint}

Notice: the name of environment variable is UPPER-CASE, replacing hyphens with underscores.

// Take ECS and IoTDA services as examples

// linux
export HUAWEICLOUD_SDK_REGION_ECS_CN_NORTH_99=https://ecs.cn-north-99.myhuaweicloud.com
export HUAWEICLOUD_SDK_REGION_IOTDA_AP_SOUTHEAST_10=https://iotda.ap-southwest-10.myhuaweicloud.com

// windows
set HUAWEICLOUD_SDK_REGION_ECS_CN_NORTH_99=https://ecs.cn-north-99.myhuaweicloud.com
set HUAWEICLOUD_SDK_REGION_IOTDA_AP_SOUTHEAST_10=https://iotda.ap-southwest-10.myhuaweicloud.com
3.3.2.2 Profile

The profile will be read from the user's home directory by default, linux~/.huaweicloud/regions.yaml,windowsC:\Users\USER_NAME\.huaweicloud\regions.yaml,the default file may not exist, but if the file exists and the content format is incorrect, an exception will be thrown for parsing errors.

The path to the profile can be modified by configuring the environment variable HUAWEICLOUD_SDK_REGIONS_FILE, like HUAWEICLOUD_SDK_REGIONS_FILE=/tmp/my_regions.yml

The file content format is as follows:

# Serivce name is case-insensitive
ECS:
  - id: 'cn-north-10'
    endpoint: 'https://ecs.cn-north-10.myhuaweicloud.com'
  - id: 'cn-north-11'
    endpoint: 'https://ecs.cn-north-11.myhuaweicloud.com'
IoTDA:
  - id: 'ap-southwest-9'
    endpoint: 'https://iotda.ap-southwest-9.myhuaweicloud.com'
3.3.2.3 Region supply chain

The default order is environment variables -> profile -> region defined in SDK, if the region is not found in the above ways, an exception will be thrown.

from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion

region1 = EcsRegion.value_of("cn-north-10")
region2 = EcsRegion.value_of("cn-north-11")

4. Send Requests and Handle Responses 🔝

# Initialize a request and print response, take interface of ListVpcs for example
request = ListVpcsRequest(limit=1)

response = client.list_vpcs(request)
print(response)

4.1 Exceptions 🔝

Level 1 Notice Level 2 Notice
ConnectionException Connection error HostUnreachableException Host is not reachable
SslHandShakeException SSL certification error
RequestTimeoutException Request timeout CallTimeoutException timeout for single request
RetryOutageException no response after retrying
ServiceResponseException service response error ServerResponseException server inner error, http status code: [500,]
ClientRequestException invalid request, http status code: [400? 500)
# handle exceptions
try:
    request = ListVpcsRequest(limit=1)
    response = client.list_vpcs(request)
    print(response)
except exception.ServiceResponseException as e:
    print(e.status_code)
    print(e.request_id)
    print(e.error_code)
    print(e.error_msg)

4.2 Get Response Object 🔝

The default response format of each request is json string, if you want to obtain the response object, the Python SDK supports using method to_json_object() to get it.

request = ListVpcsRequest(limit=1)
# original response json string
response = client.list_vpcs(request)
print(response)
# response object
response_obj = response.to_json_object()
print(response_obj["vpcs"])

Notice: This method is only supported in version 3.0.34-rc or later.

5. Use Asynchronous Client 🔝

# Initialize asynchronous client, take VpcAsyncClient for example
client = VpcAsyncClient.new_builder() \
    .with_http_config(config) \
    .with_credentials(basic_credentials) \
    .with_endpoint(endpoint) \
    .build()

# send asynchronous request
request = ListVpcsRequest(limit=1)
response = client.list_vpcs_async(request)

# get asynchronous response
print(response.result())

6. Troubleshooting 🔝

SDK supports Access log and Debug log which could be configured manually.

6.1 Access Log 🔝

SDK supports print access log which could be enabled by manual configuration, the log could be output to the console or specified files.

Initialize specified service client instance, take VpcClient for example:

client = VpcClient.new_builder() \
    .with_http_config(config) \
    .with_credentials(basic_credentials) \
    .with_endpoint(endpoint) \
    .with_file_log(path="test.log", log_level=logging.INFO) \  # Write log files
    .with_stream_log(log_level=logging.INFO) \                 # Write log to console
    .build()

where:

  • with_file_log:
    • path means log file path.
    • log_level means log level, default is INFO.
    • max_bytes means size of single log file, the default value is 10485760 bytes.
    • backup_count means count of log file, the default value is 5.
  • with_stream_log:
    • stream means stream object, the default value is sys.stdout.
    • log_level means log level, the default value is INFO.

After enabled log, the SDK will print the access log by default, every request will be recorded to the console like:

2020-06-16 10:44:02,019 4568 HuaweiCloud-SDK http_handler.py 28 INFO "GET https://vpc.cn-north-1.myhuaweicloud.com/v1/0904f9e1f100d2932f94c01f9aa1cfd7/vpcs" 200 11 0:00:00.543430 b5c927ffdab8401e772e70aa49972037

The format of access log is:

%(asctime)s %(thread)d %(name)s %(filename)s %(lineno)d %(levelname)s %(message)s

6.2 Original HTTP Listener 🔝

In some situation, you may need to debug your http requests, original http request and response information will be needed. The SDK provides a listener function to obtain the original encrypted http request and response information.

⚠️ Warning: The original http log information is used in debugging stage only, please do not print the original http header or body in the production environment. These log information is not encrypted and contains sensitive data such as the password of your ECS virtual machine, or the password of your IAM user account, etc. When the response body is binary content, the body will be printed as "***" without detailed information.

import logging
from huaweicloudsdkcore.http.http_handler import HttpHandler


def response_handler(**kwargs):
    logger = kwargs.get("logger")
    response = kwargs.get("response")
    request = response.request

    base = "> Request %s %s HTTP/1.1" % (request.method, request.path_url) + "\n"
    if len(request.headers) != 0:
        base = base + "> Headers:" + "\n"
        for each in request.headers:
            base = base + "    %s : %s" % (each, request.headers[each]) + "\n"
    base = base + "> Body: %s" % request.body + "\n\n"

    base = base + "< Response HTTP/1.1 %s " % response.status_code + "\n"
    if len(response.headers) != 0:
        base = base + "< Headers:" + "\n"
        for each in response.headers:
            base = base + "    %s : %s" % (each, response.headers[each],) + "\n"
    base = base + "< Body: %s" % response.content
    logger.debug(base)


if __name__ == "__main__":
    client = VpcClient.new_builder() \
        .with_http_config(config) \
        .with_credentials(basic_credentials) \
        .with_stream_log(log_level=logging.DEBUG) \
        .with_http_handler(HttpHandler().add_response_handler(response_handler)) \
        .with_endpoint(endpoint) \
        .build()

Notice:

HttpHandler supports method add_request_handler and add_response_handler.

7. Upload and download files 🔝

Take the interface CreateImageWatermark of the service Data Security Center as an example, this interface needs to upload an image file and return the watermarked image file stream:

# coding: utf-8
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcore.http.http_config import HttpConfig
from huaweicloudsdkcore.http.formdata import FormFile
from huaweicloudsdkdsc.v1 import *


def create_image_watermark(client):

    try:
        request = CreateImageWatermarkRequest()
        # Open the file in mode "rb", create a Formfile object.
        image_file = FormFile(open("demo.jpg", "rb"))
        body = CreateImageWatermarkRequestBody(file=image_file, blind_watermark="test_watermark")
        request.body = body
        response = client.create_image_watermark(request)
        image_file.close()
        
        # Define the method of downloading files.
        def save(stream):
            with open("result.jpg", "wb") as f:
                f.write(stream.content)
        # Download the file.
        response.consume_download_stream(save)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)


if __name__ == "__main__":
    ak = "{your ak string}"
    sk = "{your sk string}"
    endpoint = "{your endpoint}"
    project_id = "{your project id}"
    config = HttpConfig.get_default_config()
    config.ignore_ssl_verification = True
    credentials = BasicCredentials(ak, sk, project_id)
    dsc_client = DscClient.new_builder() \
        .with_http_config(config) \
        .with_credentials(credentials) \
        .with_endpoint(endpoint) \
        .build()
    
    create_image_watermark(dsc_client)