Help Center/ ModelArts/ SDK Reference/ Session Authentication/ (Optional) Session Authentication
Updated on 2024-06-12 GMT+08:00

(Optional) Session Authentication

Overview

The session module authenticates resources and initializes ModelArts SDK Client and OBS Client. After a session is set up, you can directly call the ModelArts SDKs.

Authentication Using the Username and Password

After installing the ModelArts SDK on the local PC, you can perform session authentication using the username and password. The sample code is as follows:

  • Authentication using an account
    Set username to your account name.
    1
    2
    3
    4
    5
    6
    7
    from modelarts.session import Session
    
    # Hardcoded or plaintext password is risky. For security, encrypt your password and store it in the configuration file or environment variables.
    # In this example, the password is stored in environment variables for identity authentication. Before running this example, set environment variable HUAWEICLOUD_SDK_PASSWORD.
    __PASSWORD = os.environ["HUAWEICLOUD_SDK_PASSWORD"]
    # Decrypt the password if it is encrypted.
    session = Session(username='***',  password=__PASSWORD, region_name='***', project_id='***')
    

AK/SK-based Authentication

After installing the ModelArts SDK on the local PC, you can perform session authentication using the AK/SK. The sample code is as follows:

1
2
3
4
5
6
7
8
from modelarts.session import Session

# Hardcoded or plaintext AK/SK is risky. For security, encrypt your AK/SK and store them in the configuration file or environment variables.
# In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, set environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK.
__AK = os.environ["HUAWEICLOUD_SDK_AK"]
__SK = os.environ["HUAWEICLOUD_SDK_SK"]
# Decrypt the password if it is encrypted.
session = Session(access_key=__AK,secret_key=__SK, project_id='***', region_name='***')

Parameters are as follows:

  • For details about how to obtain the values of access_key and secret_key, see "Obtaining an Access Key" in User Guide.
  • project_id indicates the project ID. To obtain the project ID, do as follows:
    1. Log in to the management console.
    2. In the upper right corner, click your account avatar icon and choose My Settings from the drop-down list.
    3. On the My Settings page, go to the Project List tab page, which is displayed by default. View the project ID and name in the project list.
  • region_name indicates the region ID. Contact the administrator to obtain a region ID.