2020-06-15 07:04:23 +00:00
English | [简体中文 ](./README_CN.md )
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
< p align = "center" >
< a href = "https://www.huaweicloud.com/" > < img width = "450px" height = "102px" src = "https://console-static.huaweicloud.com/static/authui/20210202115135/public/custom/images/logo-en.svg" > < / a >
< / p >
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
< h1 align = "center" > Huawei Cloud Python Software Development Kit (Python SDK)< / h1 >
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
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.
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
This document introduces how to obtain and use Huawei Cloud Python SDK.
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
## Requirements
2020-05-18 07:34:40 +00:00
2022-07-07 13:49:54 +00:00
- 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
2021-02-27 10:37:46 +00:00
Huawei Cloud account. You can create an Access Key in the Huawei Cloud console. For more information,
see [My Credentials ](https://support.huaweicloud.com/en-us/usermanual-ca/en-us_topic_0046606340.html ).
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
- 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 ](https://console.huaweicloud.com/?locale=en-us ) if needed.
2020-05-18 07:34:40 +00:00
2021-06-25 11:57:33 +00:00
- Huawei Cloud Python SDK requires **Python 3.3** or later, run command `python --version` to check the version of Python.
2020-05-18 07:34:40 +00:00
## Install Python SDK
2021-02-27 10:37:46 +00:00
You could use **pip** or **source code** to install dependencies.
2020-05-18 07:34:40 +00:00
2022-04-28 11:01:05 +00:00
### Individual Cloud Service
2022-07-07 13:49:54 +00:00
Take using VPC SDK for example, you need to install `huaweicloudsdkvpc` library:
2020-08-15 09:41:31 +00:00
2020-05-18 07:34:40 +00:00
- Use python pip
2022-07-14 08:55:18 +00:00
```bash
2021-02-27 10:37:46 +00:00
# Install the VPC management library
pip install huaweicloudsdkvpc
```
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
- Install from source code
2020-05-18 07:34:40 +00:00
2022-07-14 08:55:18 +00:00
```bash
2021-02-27 10:37:46 +00:00
# Install the VPC management library
cd huaweicloudsdkvpc-${version}
python setup.py install
```
2020-05-18 07:34:40 +00:00
2022-04-28 11:01:05 +00:00
### Cloud Service Collection Package
You can install `huaweicloudsdkall` , which will install all SDK supported service packages:
- Use python pip
2022-07-14 08:55:18 +00:00
```bash
2022-04-28 11:01:05 +00:00
pip install huaweicloudsdkall
```
- Install from source code
2022-07-14 08:55:18 +00:00
```bash
2022-04-28 11:01:05 +00:00
cd huaweicloudsdkall-${version}
python setup.py install
```
2021-02-27 10:37:46 +00:00
## 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.
2023-11-09 09:11:11 +00:00
- Hard-coding ak and sk for authentication into the code has a great security risk. It is recommended to store the ciphertext in the profile or environment variables and decrypt it when used to ensure security.
- In this example, ak and sk are stored in environment variables. Please configure the environment variables `HUAWEICLOUD_SDK_AK` and `HUAWEICLOUD_SDK_SK` before running this example.
2020-05-18 07:34:40 +00:00
2023-05-18 12:48:10 +00:00
**Simplified Demo**
2022-07-14 08:55:18 +00:00
```python
2021-02-27 10:37:46 +00:00
# coding: utf-8
2020-05-18 07:34:40 +00:00
2023-11-09 09:11:11 +00:00
import os
2021-02-27 10:37:46 +00:00
from huaweicloudsdkcore.auth.credentials import BasicCredentials
2023-05-18 12:48:10 +00:00
from huaweicloudsdkvpc.v2 import ListVpcsRequest, VpcClient
from huaweicloudsdkvpc.v2.region.vpc_region import VpcRegion
2021-02-27 10:37:46 +00:00
from huaweicloudsdkcore.exceptions import exceptions
2020-05-18 07:34:40 +00:00
2023-05-18 12:48:10 +00:00
if __name__ == "__main__":
# Configure authentication
2023-11-09 09:11:11 +00:00
# Do not hard-code authentication information into the code, as this may pose a security risk
# Authentication can be configured through environment variables and other methods. Please refer to Chapter 2.4 Authentication Management
2023-11-16 09:47:10 +00:00
credentials = BasicCredentials(os.getenv("HUAWEICLOUD_SDK_AK"), os.getenv("HUAWEICLOUD_SDK_SK"))
2020-06-15 07:04:23 +00:00
2023-05-18 12:48:10 +00:00
# Create a service client
client = VpcClient.new_builder() \
.with_credentials(credentials) \
.with_region(VpcRegion.value_of("cn-north-4")) \
.build()
# Send the request and get the response
2021-02-27 10:37:46 +00:00
try:
2023-05-18 12:48:10 +00:00
request = ListVpcsRequest()
2021-02-27 10:37:46 +00:00
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)
2023-05-18 12:48:10 +00:00
```
2021-02-27 10:37:46 +00:00
2023-05-18 12:48:10 +00:00
**Detailed Demo**
2021-02-27 10:37:46 +00:00
2023-05-18 12:48:10 +00:00
```python
# coding: utf-8
2023-11-09 09:11:11 +00:00
import os
2023-05-18 12:48:10 +00:00
import logging
2021-02-27 10:37:46 +00:00
2023-05-18 12:48:10 +00:00
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.http.http_config import HttpConfig
from huaweicloudsdkcore.http.http_handler import HttpHandler
from huaweicloudsdkvpc.v2 import VpcClient, ListVpcsRequest
from huaweicloudsdkvpc.v2.region.vpc_region import VpcRegion
from huaweicloudsdkcore.exceptions import exceptions
2021-02-27 10:37:46 +00:00
2023-05-18 12:48:10 +00:00
if __name__ == "__main__":
# Configure authentication
2023-11-09 09:11:11 +00:00
# Do not hard-code authentication information into the code, as this may pose a security risk
# Authentication can be configured through environment variables and other methods. Please refer to Chapter 2.4 Authentication Management
2023-05-18 12:48:10 +00:00
# If project_id is not filled in, the SDK will automatically call the IAM service to query the project id corresponding to the region.
2023-11-16 09:47:10 +00:00
credentials = BasicCredentials(os.getenv("HUAWEICLOUD_SDK_AK"), os.getenv("HUAWEICLOUD_SDK_SK"), project_id="{your projectId string}") \
2023-05-18 12:48:10 +00:00
.with_iam_endpoint("https://iam.cn-north-4.myhuaweicloud.com") # Configure the SDK built-in IAM service endpoint, default is https://iam.myhuaweicloud.com
# Use default configuration
http_config = HttpConfig.get_default_config()
# Configure whether to ignore the SSL certificate verification, default is false
http_config.ignore_ssl_verification = True
# Configure CA certificate file
http_config.ssl_ca_cert = '/path/to/certfile'
# The default connection timeout is 60 seconds, the default read timeout is 120 seconds
http_config.timeout = (60, 120)
# Configure proxy as needed
2023-11-09 09:11:11 +00:00
# Replace the proxy protocol, host and port in the example according to the actual situation
2023-05-18 12:48:10 +00:00
http_config.proxy_protocol = 'http'
http_config.proxy_host = 'proxy.huaweicloud.com'
http_config.proxy_port = 80
2023-11-09 09:11:11 +00:00
# Configure the username and password if the proxy requires authentication
2023-11-16 09:47:10 +00:00
http_config.proxy_user = os.getenv("PROXY_USERNAME")
http_config.proxy_password = os.getenv("PROXY_PASSWORD")
2023-05-18 12:48:10 +00:00
# The HTTP handler is used to print the request and response, do not use it in the production environment
def response_handler(**kwargs):
response = kwargs.get("response")
request = response.request
info = "> Request %s %s HTTP/1.1" % (request.method, request.path_url) + "\n"
if len(request.headers) != 0:
info = info + "> Headers:" + "\n"
for each in request.headers:
info = info + " %s: %s" % (each, request.headers[each]) + "\n"
info = info + "> Body: %s" % request.body + "\n\n"
info = info + "< Response HTTP / 1 . 1 % s " % response . status_code + " \n"
if len(response.headers) != 0:
info = info + "< Headers: " + " \n"
for each in response.headers:
info = info + " %s: %s" % (each, response.headers[each],) + "\n"
info = info + "< Body: % s " % response . content
print(info)
http_handler = HttpHandler().add_response_handler(response_handler)
// Create a service client
client = VpcClient.new_builder() \
.with_credentials(credentials) \ # Configure authentication
.with_region(VpcRegion.value_of("cn-north-4")) \ # Configure region, it will throw a KeyError if the region does not exist
.with_http_config(http_config) \ # Configure HTTP
.with_stream_log(log_level=logging.INFO) \ # Configure request log output to console
.with_file_log(path="test.log", log_level=logging.INFO) \ # Configure request log output to file
.with_http_handler(http_handler) \ # Configure HTTP handler
2021-02-27 10:37:46 +00:00
.build()
2020-06-15 07:04:23 +00:00
2023-05-18 12:48:10 +00:00
# Send the request and get the response
try:
request = ListVpcsRequest()
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)
2021-02-27 10:37:46 +00:00
```
2020-05-18 07:34:40 +00:00
2022-07-07 13:49:54 +00:00
## Online Debugging
[API Explorer ](https://apiexplorer.developer.intl.huaweicloud.com/apiexplorer/overview ) provides api retrieval and online debugging, supports full fast retrieval, visual debugging, help document viewing, and online consultation.
2021-02-27 10:37:46 +00:00
## Changelog
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
Detailed changes for each released version are documented in
the [CHANGELOG.md ](https://github.com/huaweicloud/huaweicloud-sdk-python-v3/blob/master/CHANGELOG.md ).
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
## User Manual [:top:](#huawei-cloud-python-software-development-kit-python-sdk)
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
* [1. Client Configuration ](#1-client-configuration-top )
2022-07-07 13:49:54 +00:00
* [1.1 Default Configuration ](#11-default-configuration-top )
* [1.2 Network Proxy ](#12-network-proxy-top )
* [1.3 Timeout Configuration ](#13-timeout-configuration-top )
* [1.4 SSL Certification ](#14-ssl-certification-top )
2021-02-27 10:37:46 +00:00
* [2. Credentials Configuration ](#2-credentials-configuration-top )
2022-07-07 13:49:54 +00:00
* [2.1 Use Permanent AK&SK ](#21-use-permanent-aksk-top )
* [2.2 Use Temporary AK&SK ](#22-use-temporary-aksk-top )
2022-07-14 08:55:18 +00:00
* [2.3 Use IdpId&IdTokenFile ](#23-use-idpididtokenfile-top )
* [2.4 Authentication Management ](#24-authentication-management-top )
* [2.4.1 Environment Variables ](#241-environment-variables-top )
* [2.4.2 Profile ](#242-profile-top )
* [2.4.3 Metadata ](#243-metadata-top )
* [2.4.4 Provider Chain ](#244-provider-chain-top )
2021-02-27 10:37:46 +00:00
* [3. Client Initialization ](#3-client-initialization-top )
2022-07-07 13:49:54 +00:00
* [3.1 Initialize the client with specified Endpoint ](#31-initialize-the-serviceclient-with-specified-endpoint-top )
* [3.2 Initialize the client with specified Region (Recommended) ](#32-initialize-the-serviceclient-with-specified-region-recommended-top )
2022-06-23 11:11:56 +00:00
* [3.3 Custom Configuration ](#33-custom-configuration-top )
* [3.3.1 IAM endpoint configuration ](#331-iam-endpoint-configuration-top )
* [3.3.2 Region configuration ](#332-region-configuration-top )
2021-02-27 10:37:46 +00:00
* [4. Send Requests and Handle Responses ](#4-send-requests-and-handle-responses-top )
2022-07-07 13:49:54 +00:00
* [4.1 Exceptions ](#41-exceptions-top )
* [4.2 Get Response Object ](#42-get-response-object-top )
2021-02-27 10:37:46 +00:00
* [5. Use Asynchronous Client ](#5-use-asynchronous-client-top )
* [6. Troubleshooting ](#6-troubleshooting-top )
* [6.1 Access Log ](#61-access-log-top )
* [6.2 Original HTTP Listener ](#62-original-http-listener-top )
2023-11-16 09:47:10 +00:00
* [7. API Invoker ](#7-api-invoker-top )
* [7.1 Custom request headers ](#71-custom-request-headers-top )
* [8. Upload and download files ](#7-upload-and-download-files-top )
2020-06-15 07:04:23 +00:00
2021-02-27 10:37:46 +00:00
### 1. Client Configuration [:top:](#user-manual-top)
2020-06-15 07:04:23 +00:00
2021-02-27 10:37:46 +00:00
#### 1.1 Default Configuration [:top:](#user-manual-top)
2020-06-15 07:04:23 +00:00
2022-07-14 08:55:18 +00:00
```python
2022-07-07 13:49:54 +00:00
from huaweicloudsdkcore.http.http_config import HttpConfig
2021-02-27 10:37:46 +00:00
# Use default configuration
2023-05-18 12:48:10 +00:00
http_config = HttpConfig.get_default_config()
client = VpcClient.new_builder() \
.with_http_config(http_config) \
.build()
2021-02-27 10:37:46 +00:00
```
2020-08-15 09:41:31 +00:00
2021-02-27 10:37:46 +00:00
#### 1.2 Network Proxy [:top:](#user-manual-top)
2020-08-15 09:41:31 +00:00
2022-07-14 08:55:18 +00:00
```python
2023-05-18 12:48:10 +00:00
http_config = HttpConfig.get_default_config()
2021-02-27 10:37:46 +00:00
# Use Proxy if needed
2023-11-09 09:11:11 +00:00
# Replace the proxy protocol, host and port in the example according to the actual situation
2023-05-18 12:48:10 +00:00
http_config.proxy_protocol = 'http'
http_config.proxy_host = 'proxy.huaweicloud.com'
http_config.proxy_port = 80
2023-11-09 09:11:11 +00:00
# Configure the username and password if the proxy requires authentication
2023-11-16 09:47:10 +00:00
http_config.proxy_user = os.getenv("PROXY_USERNAME")
http_config.proxy_password = os.getenv("PROXY_PASSWORD")
2023-05-18 12:48:10 +00:00
client = VpcClient.new_builder() \
.with_http_config(http_config) \
.build()
2021-02-27 10:37:46 +00:00
```
2020-12-24 01:46:02 +00:00
2022-07-07 13:49:54 +00:00
#### 1.3 Timeout Configuration [:top:](#user-manual-top)
2020-08-15 09:41:31 +00:00
2022-07-14 08:55:18 +00:00
```python
2023-05-18 12:48:10 +00:00
http_config = HttpConfig.get_default_config()
2022-07-07 13:49:54 +00:00
# The default connection timeout is 60 seconds, the default read timeout is 120 seconds
# Set the connection timeout and read timeout to 120 seconds
2023-05-18 12:48:10 +00:00
http_config.timeout = 120
2022-07-07 13:49:54 +00:00
# Set the connection timeout to 60 seconds and the read timeout to 120 seconds
2023-05-18 12:48:10 +00:00
http_config.timeout = (60, 120)
client = VpcClient.new_builder() \
.with_http_config(http_config) \
.build()
2021-02-27 10:37:46 +00:00
```
#### 1.4 SSL Certification [:top:](#user-manual-top)
2022-07-14 08:55:18 +00:00
```python
2023-05-18 12:48:10 +00:00
http_config = HttpConfig.get_default_config()
2022-07-07 13:49:54 +00:00
# Skip SSL certifaction checking while using https protocol if needed
2023-05-18 12:48:10 +00:00
http_config.ignore_ssl_verification = True
2022-07-07 13:49:54 +00:00
# Configure the server's CA certificate for the SDK to verify the legitimacy of the server
2023-05-18 12:48:10 +00:00
http_config.ssl_ca_cert = ssl_ca_cert
client = VpcClient.new_builder() \
.with_http_config(http_config) \
.build()
2021-02-27 10:37:46 +00:00
```
### 2. Credentials Configuration [:top:](#user-manual-top)
There are two types of Huawei Cloud services, `regional` services and `global` services.
Global services contain BSS, DevStar, EPS, IAM, RMS, TMS.
2022-07-14 08:55:18 +00:00
For `regional` services' authentication, projectId is required to initialize BasicCredentials.
2022-07-07 13:49:54 +00:00
For `global` services' authentication, domainId is required to initialize GlobalCredentials.
2021-02-27 10:37:46 +00:00
2022-07-14 08:55:18 +00:00
The following authentications are supported:
- permanent AK& SK
- temporary AK& SK + SecurityToken
- IdpId& IdTokenFile
#### 2.1 Use Permanent AK&SK [:top:](#user-manual-top)
2021-02-27 10:37:46 +00:00
**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.
2022-07-14 08:55:18 +00:00
```python
2021-02-27 10:37:46 +00:00
# Regional services
2023-11-16 09:47:10 +00:00
ak = os.getenv("HUAWEICLOUD_SDK_AK")
sk = os.getenv("HUAWEICLOUD_SDK_SK")
2023-11-09 09:11:11 +00:00
project_id = "{your projectId string}"
2021-02-27 10:37:46 +00:00
basic_credentials = BasicCredentials(ak, sk, project_id)
# Global services
2023-11-16 09:47:10 +00:00
ak = os.getenv("HUAWEICLOUD_SDK_AK")
sk = os.getenv("HUAWEICLOUD_SDK_SK")
2023-11-09 09:11:11 +00:00
domain_id = "{your domainId string}"
2021-02-27 10:37:46 +00:00
global_credentials = GlobalCredentials(ak, sk, domain_id)
```
2021-03-16 13:12:01 +00:00
**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 ](#32-initialize-the-serviceclient-with-specified-region-recommended-top )
.
2021-02-27 10:37:46 +00:00
#### 2.2 Use Temporary AK&SK [:top:](#user-manual-top)
2022-07-07 13:49:54 +00:00
It's required to obtain temporary AK& SK and security token first, which could be obtained through
permanent AK& SK or through an agency.
2021-02-27 10:37:46 +00:00
2022-07-07 13:49:54 +00:00
- Obtaining a temporary access key and security token through token, you could refer to
2021-02-27 10:37:46 +00:00
document: https://support.huaweicloud.com/en-us/api-iam/iam_04_0002.html . The API mentioned in the document above
corresponds to the method of `CreateTemporaryAccessKeyByToken` in IAM SDK.
2022-07-07 13:49:54 +00:00
- Obtaining a temporary access key and security token through an agency, you could refer to
2021-02-27 10:37:46 +00:00
document: https://support.huaweicloud.com/en-us/api-iam/iam_04_0101.html . The API mentioned in the document above
corresponds to the method of `CreateTemporaryAccessKeyByAgency` in IAM SDK.
2022-07-14 08:55:18 +00:00
**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.
2022-07-07 13:49:54 +00:00
After the temporary AK& SK& SecurityToken is successfully obtained, you can use the following example to initialize the authentication:
2022-07-14 08:55:18 +00:00
```python
2021-02-27 10:37:46 +00:00
# Regional services
2023-11-16 09:47:10 +00:00
ak = os.getenv("HUAWEICLOUD_SDK_AK")
sk = os.getenv("HUAWEICLOUD_SDK_SK")
security_token = os.getenv("HUAWEICLOUD_SDK_SECURITY_TOKEN")
2023-11-09 09:11:11 +00:00
project_id = "{your projectId string}"
2021-02-27 10:37:46 +00:00
basic_credentials = BasicCredentials(ak, sk, project_id).with_security_token(security_token)
# Global services
2023-11-16 09:47:10 +00:00
ak = os.getenv("HUAWEICLOUD_SDK_AK")
sk = os.getenv("HUAWEICLOUD_SDK_SK")
security_token = os.getenv("HUAWEICLOUD_SDK_SECURITY_TOKEN")
2023-11-09 09:11:11 +00:00
domain_id = "{your domainId string}"
2021-02-27 10:37:46 +00:00
global_credentials = GlobalCredentials(ak, sk, domain_id).with_security_token(security_token)
```
2022-07-14 08:55:18 +00:00
#### 2.3 Use IdpId&IdTokenFile [:top:](#user-manual-top)
Obtain a federated identity authentication token using an OpenID Connect ID token, refer to the [Obtaining a Token with an OpenID Connect ID Token ](https://support.huaweicloud.com/intl/en-us/api-iam/iam_13_0605.html )
**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.
```python
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 [:top:](#user-manual-top)
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 [:top:](#user-manual-top)
**AK/SK Auth**
| Environment Variables | Notice |
| ------------ | ------------ |
| HUAWEICLOUD_SDK_AK | Required, AccessKey |
| HUAWEICLOUD_SDK_SK | Required, SecretKey |
| 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:
```python
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:
```python
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 [:top:](#user-manual-top)
The profile will be read from the user's home directory by default, linux`~/.huaweicloud/credentials`,windows`C:\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 | Required, AccessKey |
| sk | Required, SecretKey |
| 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:
```ini
[basic]
ak = your_ak
sk = your_sk
[global]
ak = your_ak
sk = your_sk
```
Get the credentials from profile:
```python
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:
```ini
[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:
```python
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 [:top:](#user-manual-top)
Get temporary AK/SK and securitytoken from instance's metadata. Refer to the [Obtaining Metadata ](https://support.huaweicloud.com/intl/en-us/usermanual-ecs/ecs_03_0166.html ) for more information.
Manually obtain authentication from instance metadata:
```python
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 [:top:](#user-manual-top)
When creating a service client without credentials, try to load authentication in the order **Environment Variables -> Profile -> Metadata**
Get authentication from provider chain:
```python
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:
```python
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()
```
2021-02-27 10:37:46 +00:00
### 3. Client Initialization [:top:](#user-manual-top)
There are two ways to initialize the {Service}Client, you could choose one you preferred.
#### 3.1 Initialize the {Service}Client with specified Endpoint [:top:](#user-manual-top)
2022-07-14 08:55:18 +00:00
```python
2021-03-16 13:12:01 +00:00
# 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
2023-11-16 09:47:10 +00:00
ak = os.getenv("HUAWEICLOUD_SDK_AK")
sk = os.getenv("HUAWEICLOUD_SDK_SK")
2023-11-09 09:11:11 +00:00
project_id = "{your projectId string}"
2021-03-16 13:12:01 +00:00
basic_credentials = BasicCredentials(ak, sk, project_id)
# Initialize specified service client instance, take initializing the regional service VPC's VpcClient for example
2021-02-27 10:37:46 +00:00
client = VpcClient.new_builder() \
.with_http_config(config) \
.with_credentials(basic_credentials) \
.with_endpoint(endpoint) \
.build()
```
2020-12-24 01:46:02 +00:00
2021-02-27 10:37:46 +00:00
**where:**
2020-12-24 01:46:02 +00:00
2021-02-27 10:37:46 +00:00
- `endpoint` varies by services and regions,
see [Regions and Endpoints ](https://developer.huaweicloud.com/intl/en-us/endpoint ) to obtain correct endpoint.
2020-12-24 01:46:02 +00:00
2021-03-16 13:12:01 +00:00
- When you meet some trouble in getting projectId using the specified region way, you could use this way instead.
2021-02-27 10:37:46 +00:00
#### 3.2 Initialize the {Service}Client with specified Region **(Recommended)** [:top:](#user-manual-top)
2020-12-24 01:46:02 +00:00
2022-07-14 08:55:18 +00:00
```python
2023-11-09 09:11:11 +00:00
import os
2021-02-27 10:37:46 +00:00
# dependency for region module
from huaweicloudsdkiam.v3.region.iam_region import IamRegion
2020-05-18 07:34:40 +00:00
2022-07-07 13:49:54 +00:00
# Initialize the credentials, project_id or domain_id could be unassigned in this situation
# Take initializing GlobalCredentials for example
2023-11-16 09:47:10 +00:00
ak = os.getenv("HUAWEICLOUD_SDK_AK")
sk = os.getenv("HUAWEICLOUD_SDK_SK")
2021-02-27 10:37:46 +00:00
global_credentials = GlobalCredentials(ak, sk)
2022-07-07 13:49:54 +00:00
# Initialize specified service client instance
# Take initializing the global service IAM's IamClient for example
2021-02-27 10:37:46 +00:00
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.
2021-03-16 13:12:01 +00:00
- 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.
2021-02-27 10:37:46 +00:00
2023-09-14 09:28:03 +00:00
#### 3.3 Custom Configuration [:top:](#user-manual-top)
2022-06-23 11:11:56 +00:00
2022-07-07 13:49:54 +00:00
**Notice:** Supported since v3.0.93
2022-06-23 11:11:56 +00:00
2023-09-14 09:28:03 +00:00
##### 3.3.1 IAM endpoint configuration [:top:](#user-manual-top)
2022-06-23 11:11:56 +00:00
2023-11-30 09:17:24 +00:00
Automatically acquiring projectId/domainId will invoke the [KeystoneListProjects ](https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=IAM&api=KeystoneListProjects ) /[KeystoneListAuthDomains](https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=IAM& api=KeystoneListAuthDomains) interface of IAM service. The default iam enpoint is `https://iam.myhuaweicloud.com` , **European station users need to specify the endpoint as https://iam.eu-west-101.myhuaweicloud.eu** , you can modify the endpoint in the following two ways:
2022-06-23 11:11:56 +00:00
2023-09-14 09:28:03 +00:00
###### 3.3.1.1 Global scope [:top:](#user-manual-top)
2022-06-23 11:11:56 +00:00
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
```
2023-09-14 09:28:03 +00:00
###### 3.3.1.2 Credentials scope [:top:](#user-manual-top)
2022-06-23 11:11:56 +00:00
This configuration is only valid for a credential, and it will override the global configuration
```python
2023-11-09 09:11:11 +00:00
import os
2022-06-23 11:11:56 +00:00
from huaweicloudsdkcore.auth.credentials import BasicCredentials
2023-11-16 09:47:10 +00:00
ak = os.getenv("HUAWEICLOUD_SDK_AK")
sk = os.getenv("HUAWEICLOUD_SDK_SK")
2022-06-23 11:11:56 +00:00
iam_endpoint = "https://iam.cn-north-4.myhuaweicloud.com"
credentials = BasicCredentials(ak, sk).with_iam_endpoint(iam_endpoint)
```
2023-09-14 09:28:03 +00:00
##### 3.3.2 Region configuration [:top:](#user-manual-top)
2022-06-23 11:11:56 +00:00
2023-09-14 09:28:03 +00:00
###### 3.3.2.1 Environment variable [:top:](#user-manual-top)
2022-06-23 11:11:56 +00:00
2023-10-26 08:21:45 +00:00
Specified by environment variable, the format is `HUAWEICLOUD_SDK_REGION_{SERVICE_NAME}_{REGION_ID}={endpoint}`
2022-06-23 11:11:56 +00:00
Notice: the name of environment variable is UPPER-CASE, replacing hyphens with underscores.
```
// Take ECS and IoTDA services as examples
// linux
2023-10-19 08:19:16 +00:00
export HUAWEICLOUD_SDK_REGION_ECS_CN_NORTH_9=https://ecs.cn-north-9.myhuaweicloud.com
export HUAWEICLOUD_SDK_REGION_IOTDA_AP_SOUTHEAST_1=https://iotda.ap-southwest-1.myhuaweicloud.com
2022-06-23 11:11:56 +00:00
// windows
2023-10-19 08:19:16 +00:00
set HUAWEICLOUD_SDK_REGION_ECS_CN_NORTH_9=https://ecs.cn-north-9.myhuaweicloud.com
set HUAWEICLOUD_SDK_REGION_IOTDA_AP_SOUTHEAST_1=https://iotda.ap-southwest-1.myhuaweicloud.com
2022-06-23 11:11:56 +00:00
```
2023-10-19 08:19:16 +00:00
A region corresponding to multiple endpoints is supported since **v3.1.60** , if the main endpoint cannot be connected, it will automatically switch to the backup endpoint.
2023-10-26 08:21:45 +00:00
The format is `HUAWEICLOUD_SDK_REGION_{SERVICE_NAME}_{REGION_ID}={endpoint1},{endpoint2}` , separate multiple endpoints with commas, such as `HUAWEICLOUD_SDK_REGION_ECS_CN_NORTH_9=https://ecs.cn-north-9.myhuaweicloud.com,https://ecs.cn-north-9.myhuaweicloud.cn`
2023-10-19 08:19:16 +00:00
2023-09-14 09:28:03 +00:00
###### 3.3.2.2 Profile [:top:](#user-manual-top)
2022-06-23 11:11:56 +00:00
The profile will be read from the user's home directory by default, linux`~/.huaweicloud/regions.yaml`,windows`C:\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:
```yaml
# Serivce name is case-insensitive
ECS:
2023-10-19 08:19:16 +00:00
- id: 'cn-north-1'
endpoint: 'https://ecs.cn-north-1.myhuaweicloud.com'
- id: 'cn-north-9'
endpoint: 'https://ecs.cn-north-9.myhuaweicloud.com'
2022-06-23 11:11:56 +00:00
IoTDA:
2023-10-19 08:19:16 +00:00
- id: 'ap-southwest-1'
endpoint: 'https://iotda.ap-southwest-1.myhuaweicloud.com'
```
A region corresponding to multiple endpoints is supported since v3.1.62, if the main endpoint cannot be connected, it will automatically switch to the backup endpoint.
```yaml
ECS:
- id: 'cn-north-1'
endpoints:
- 'https://ecs.cn-north-1.myhuaweicloud.com'
- 'https://ecs.cn-north-1.myhuaweicloud.cn'
2022-06-23 11:11:56 +00:00
```
2023-09-14 09:28:03 +00:00
###### 3.3.2.3 Region supply chain [:top:](#user-manual-top)
2022-06-23 11:11:56 +00:00
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.
```python
from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion
2023-10-19 08:19:16 +00:00
region1 = EcsRegion.value_of("cn-north-1")
region2 = EcsRegion.value_of("cn-north-9")
2022-06-23 11:11:56 +00:00
```
2021-02-27 10:37:46 +00:00
### 4. Send Requests and Handle Responses [:top:](#user-manual-top)
2022-07-14 08:55:18 +00:00
```python
2021-02-27 10:37:46 +00:00
# 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 [:top:](#user-manual-top)
| 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) |
2022-07-14 08:55:18 +00:00
```python
2021-02-27 10:37:46 +00:00
# handle exceptions
try:
request = ListVpcsRequest(limit=1)
2020-07-30 11:19:13 +00:00
response = client.list_vpcs(request)
2021-02-27 10:37:46 +00:00
print(response)
except exception.ServiceResponseException as e:
print(e.status_code)
print(e.request_id)
print(e.error_code)
print(e.error_msg)
```
2020-06-15 07:04:23 +00:00
2021-02-27 10:37:46 +00:00
#### 4.2 Get Response Object [:top:](#user-manual-top)
2020-06-30 12:45:36 +00:00
2021-02-27 10:37:46 +00:00
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.
2020-06-30 12:45:36 +00:00
2022-07-14 08:55:18 +00:00
```python
2021-02-27 10:37:46 +00:00
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"])
```
2020-06-30 12:45:36 +00:00
2021-02-27 10:37:46 +00:00
**Notice:** This method is only supported in version `3.0.34-rc` or later.
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
### 5. Use Asynchronous Client [:top:](#user-manual-top)
2020-05-18 07:34:40 +00:00
2022-07-14 08:55:18 +00:00
```python
2021-02-27 10:37:46 +00:00
# Initialize asynchronous client, take VpcAsyncClient for example
client = VpcAsyncClient.new_builder() \
.with_http_config(config) \
.with_credentials(basic_credentials) \
.with_endpoint(endpoint) \
.build()
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
# send asynchronous request
request = ListVpcsRequest(limit=1)
response = client.list_vpcs_async(request)
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
# get asynchronous response
print(response.result())
```
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
### 6. Troubleshooting [:top:](#user-manual-top)
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
SDK supports `Access` log and `Debug` log which could be configured manually.
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
#### 6.1 Access Log [:top:](#user-manual-top)
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
SDK supports print access log which could be enabled by manual configuration, the log could be output to the console or
specified files.
2020-08-15 09:41:31 +00:00
2022-07-07 13:49:54 +00:00
Initialize specified service client instance, take VpcClient for example:
2021-02-27 10:37:46 +00:00
2022-07-14 08:55:18 +00:00
```python
2021-02-27 10:37:46 +00:00
client = VpcClient.new_builder() \
.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:
2022-07-14 08:55:18 +00:00
```text
2021-02-27 10:37:46 +00:00
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:
2022-07-14 08:55:18 +00:00
```python
2021-02-27 10:37:46 +00:00
%(asctime)s %(thread)d %(name)s %(filename)s %(lineno)d %(levelname)s %(message)s
```
#### 6.2 Original HTTP Listener [:top:](#user-manual-top)
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: 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.
2022-07-14 08:55:18 +00:00
```python
2021-02-27 10:37:46 +00:00
from huaweicloudsdkcore.http.http_handler import HttpHandler
def response_handler(**kwargs):
response = kwargs.get("response")
request = response.request
2023-05-18 12:48:10 +00:00
info = "> Request %s %s HTTP/1.1" % (request.method, request.path_url) + "\n"
2021-02-27 10:37:46 +00:00
if len(request.headers) != 0:
2023-05-18 12:48:10 +00:00
info = info + "> Headers:" + "\n"
2021-02-27 10:37:46 +00:00
for each in request.headers:
2023-05-18 12:48:10 +00:00
info = info + " %s: %s" % (each, request.headers[each]) + "\n"
info = info + "> Body: %s" % request.body + "\n\n"
2021-02-27 10:37:46 +00:00
2023-05-18 12:48:10 +00:00
info = info + "< Response HTTP / 1 . 1 % s " % response . status_code + " \n"
2021-02-27 10:37:46 +00:00
if len(response.headers) != 0:
2023-05-18 12:48:10 +00:00
info = info + "< Headers: " + " \n"
2021-02-27 10:37:46 +00:00
for each in response.headers:
2023-05-18 12:48:10 +00:00
info = info + " %s: %s" % (each, response.headers[each],) + "\n"
info = info + "< Body: % s " % response . content
print(info)
2021-02-27 10:37:46 +00:00
if __name__ == "__main__":
2023-05-18 12:48:10 +00:00
http_handler = HttpHandler().add_response_handler(response_handler)
2021-02-27 10:37:46 +00:00
client = VpcClient.new_builder() \
2023-05-18 12:48:10 +00:00
.with_http_handler(http_handler) \
2021-02-27 10:37:46 +00:00
.build()
```
2020-05-18 07:34:40 +00:00
2021-02-27 10:37:46 +00:00
**Notice:**
2020-08-15 09:41:31 +00:00
2021-02-27 10:37:46 +00:00
HttpHandler supports method `add_request_handler` and `add_response_handler` .
2021-12-10 10:12:27 +00:00
2023-11-16 09:47:10 +00:00
### 7. API Invoker [:top:](#user-manual-top)
#### 7.1 Custom request headers [:top:](#user-manual-top)
You can flexibly configure request headers as needed. **Do not** specify common request headers such as `Host` , `Authorization` , `User-Agent` , `Content-Type` unless necessary, as this may cause the errors.
2023-11-30 09:17:24 +00:00
**Sync invoke**
2023-11-16 09:47:10 +00:00
2023-11-30 09:17:24 +00:00
```python
2023-11-16 09:47:10 +00:00
client = VpcClient.new_builder() \
.with_credentials(credentials) \
.with_region(VpcRegion.value_of("cn-north-4")) \
.build()
2023-11-30 09:17:24 +00:00
request = ListVpcsRequest()
response = client.list_vpcs_invoker(request) \
# Custom request headers
.add_header("key1", "value1") \
.add_header("key2", "value2") \
.invoke()
print(response)
```
**Async invoke**
```python
client = VpcAsyncClient.new_builder() \
.with_credentials(credentials) \
.with_region(VpcRegion.value_of("cn-north-4")) \
.build()
request = ListVpcsRequest()
response = client.list_vpcs_async_invoker(request) \
# Custom request headers
.add_header("key1", "value1") \
.add_header("key2", "value2") \
.invoke().result()
print(response)
2023-11-16 09:47:10 +00:00
```
### 8. Upload and download files [:top:](#user-manual-top)
2021-12-10 10:12:27 +00:00
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:
```python
# coding: utf-8
2023-11-09 09:11:11 +00:00
import os
2021-12-10 10:12:27 +00:00
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__":
2023-11-16 09:47:10 +00:00
ak = os.getenv("HUAWEICLOUD_SDK_AK")
sk = os.getenv("HUAWEICLOUD_SDK_SK")
2021-12-10 10:12:27 +00:00
endpoint = "{your endpoint}"
project_id = "{your project id}"
config = HttpConfig.get_default_config()
config.ignore_ssl_verification = True
credentials = BasicCredentials(ak, sk, project_id)
2022-04-28 11:01:05 +00:00
dsc_client = DscClient.new_builder() \
2021-12-10 10:12:27 +00:00
.with_http_config(config) \
.with_credentials(credentials) \
.with_endpoint(endpoint) \
.build()
create_image_watermark(dsc_client)
```