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

(Optional) Session Authentication

Overview

The session module authenticates public cloud 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='***')
    
  • Authentication using an IAM user
    Set account to your account name and username to your IAM username.
    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(account='***', username='***',  password=__PASSWORD, region_name='***', project_id='***')
    

For the concepts of the account and user, see Basic Concepts of IAM. For details about how to obtain your account and username, see Obtaining the Username, User ID, Project Name, and Project ID.

If your Huawei Cloud account has been upgraded to a HUAWEI ID, account authentication will be unavailable. In this case, create an IAM user and use it for authentication.

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:

  • To obtain access_key and secret_key, follow these steps:
    1. Log in to the management console, hover over your username in the upper right corner and choose My Credentials. The My Credentials page is displayed.
    2. Choose Access Keys and click Create Access Key.
    3. In the Create Access Key dialog box, enter the description and click OK. Click Download to download the key. The access key file will be saved in the default download folder of the browser. Open the credentials.csv file to view the access key (Access Key Id and Secret Access Key).
  • project_id indicates the project ID. To obtain the project ID, do as follows:
    On the My Credentials page, click API Credentials. In the project list, view the project ID and name. If there are multiple projects, unfold the target region and obtain the project ID from the Project ID column.
    Figure 1 Viewing a project ID
  • region_name indicates the region ID. For details about how to obtain a region ID, see Obtaining Region Information.