To use Simple Message Queue (SMQ) SDK for Python to initiate requests, you must configure endpoints and access credentials. Alibaba Cloud services use access credentials to verify your identity information and access permissions. You can select different types of access credentials based on authentication and authorization requirements in different scenarios. This topic describes how to configure endpoints, temporary access credentials, and long-term access credentials.
Configure endpoints
Download the latest version of SMQ SDK for Python, decompress the package, and then go to the aliyun-mns-python-sdk subdirectory.
Open the sample.cfg file and configure the endpoint.
You can view the endpoints of SMQ in the Endpoint section of the Queue Details or Topic Details page.
The endpoints of SMQ vary based on the region.
Configure access credentials
The following types of access credentials are supported:
Long-term access credentials: To ensure data security, we recommend that you do not use long-term access credentials. For scenarios that require convenience, long-term access credentials eliminate the need for multiple refreshes within an extended period of time. We recommend that you change your long-term access credentials every three months to ensure the security of your Alibaba Cloud account. If long-term access credentials are leaked or no longer used, you can delete or disable the long-term access credentials to reduce security risks.
Temporary access credentials: For scenarios that require high security, such as temporarily authorizing an application to access SMQ, we recommend that you use temporary access credentials. Temporary access credentials are valid only within a specific period of time to prevent credential leaks. Temporary access credentials support fine-grained access control to prevent security risks caused by excessive permissions.
Configure long-term access credentials
If you want to access SMQ in an application or a service for a long period of time, you can use the AccessKey pair of a Resource Access Management (RAM) user.
Obtain the AccessKey pair of a RAM user.
For more information, see the Create an AccessKey pair for a RAM user section of the "Create an AccessKey pair" topic.
ImportantWe recommend that you rotate the AccessKey pair of a RAM user that is used for more than three months. We recommend that you disable and delete the AccessKey pair of a RAM user that is not used for a long period of time. This reduces the risk of AccessKey pair leaks.
Configure the AccessKey pair of the RAM user as access credentials.
Configure environment variables
Run the following command to configure environment variables:
macOS
Open the terminal.
Run the following command:
nano ~/.bash_profile
Add the AccessKey pair of the RAM user to the end of the file.
export ALIBABA_CLOUD_ACCESS_KEY_ID=LTA**** export ALIBABA_CLOUD_ACCESS_KEY_SECRET=moiEs****
Press
Ctrl
+X
, pressY
to save the file, and then pressEnter
to close the file.Run the following command to make the changes take effect:
source ~/.bash_profile
Run the following command to verify the configurations of the environment variables:
echo $ALIBABA_CLOUD_ACCESS_KEY_ID echo $ALIBABA_CLOUD_ACCESS_KEY_SECRET
Sample success response:
LTA**** moiEs****
Linux
Open the terminal.
Run the following command:
sudo vim /etc/profile
Add the AccessKey pair of the RAM user to the end of the file.
export ALIBABA_CLOUD_ACCESS_KEY_ID=LTA**** export ALIBABA_CLOUD_ACCESS_KEY_SECRET=moiEs****
Press
Esc
to exit the edit mode. Then, enter:wq
and pressEnter
to save and close the file.Run the following command to make the changes take effect:
source /etc/profile
Run the following command to verify the configurations of the environment variables:
echo $ALIBABA_CLOUD_ACCESS_KEY_ID echo $ALIBABA_CLOUD_ACCESS_KEY_SECRET
Sample success response:
LTA**** moiEs****
Windows
Use the GUI
In this example, Windows 10 is used. You can perform the following steps to configure the AccessKey pair of the RAM user by using environment variables on the GUI:
On the Windows desktop, right-click This PC and select Properties. On the page that appears, click Advanced system settings. In the System Properties dialog box, click Environment Variables on the Advanced tab. In the Environment Variables dialog box, click New in the User variables or System variables section.
Configure the environment variables. The following table describes the environment variables.
Variable
Example
ALIBABA_CLOUD_ACCESS_KEY_ID
LTA****
ALIBABA_CLOUD_ACCESS_KEY_SECRET
moiEs****
Run the following command to verify the configurations of the environment variables:
echo $ALIBABA_CLOUD_ACCESS_KEY_ID echo $ALIBABA_CLOUD_ACCESS_KEY_SECRET
Sample success response:
LTA**** moiEs****
Use CMD
Open the CLI.
Run the following command to configure the environment variables by using the AccessKey pair:
set ALIBABA_CLOUD_ACCESS_KEY_ID=LTA**** set ALIBABA_CLOUD_ACCESS_KEY_SECRET=moiEs****
Run the following command to make the changes take effect:
setx ALIBABA_CLOUD_ACCESS_KEY_ID "%MNS_ACCESS_KEY_ID%" setx ALIBABA_CLOUD_ACCESS_KEY_SECRET "%MNS_ACCESS_KEY_SECRET%"
Run the following command to verify the configurations of the environment variables:
echo %ALIBABA_CLOUD_ACCESS_KEY_ID% echo %ALIBABA_CLOUD_ACCESS_KEY_SECRET%
Sample success response:
LTA**** moiEs****
Use Windows PowerShell
Press
Win+X
.Specify the AccessKey pair of the RAM user.
Valid for the current session
In the message that appears, select Windows PowerShell.
Specify the following environment variables for the current session:
$env:ALIBABA_CLOUD_ACCESS_KEY_ID = "LTA****" $env:ALIBABA_CLOUD_ACCESS_KEY_SECRET = "moiEs****"
Valid for all new sessions
In the message that appears, select Windows PowerShell.
Specify the following environment variables for all new sessions:
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'LTA****', [System.EnvironmentVariableTarget]::User) [System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'moiEs****', [System.EnvironmentVariableTarget]::User)
Valid for all users
In the message that appears, select Windows PowerShell (Administrator).
Specify the following environment variables for all users:
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'LTA****', [System.EnvironmentVariableTarget]::Machine) [System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'moiEs****', [System.EnvironmentVariableTarget]::Machine)
Run the following command to verify the configurations of the environment variables:
Get-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_ID Get-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_SECRET
Sample success response:
LTA**** moiEs****
Obtain the AccessKey pair of the RAM user from the environment variables.
// Obtain the access credentials from the environment variables. accid = os.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") acckey = os.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
Embed access credentials in code
WarningSecurity issues may occur if you embed access credentials in code. Leaked access credentials can be used by attackers to access your resources, which causes data breaches. To ensure security, we recommend that you use more secure methods, such as obtaining access credentials from environment variables.
accid,acckey,endpoint,token = MNSSampleCommon.LoadConfig() my_account = Account(endpoint, accid, acckey, token)
Configure temporary access credentials
If you want to temporarily access SMQ, you can use Security Token Service (STS) to configure a temporary access credential provided by STS. When you use the temporary access credential, you do not need to disclose the AccessKey pair of your RAM user. This ensures secure access to SMQ.
Create a RAM user.
For more information, see Create a RAM user.
Attach the
AliyunSTSAssumeRoleAccess
policy to the RAM user.For more information, see Grant permissions to a RAM user.
Use the RAM user to call the AssumeRole operation of STS to obtain temporary access credentials.
For more information, see AssumeRole.
Configure the temporary access credentials obtained from STS.
Configure environment variables
Use the temporary access credentials obtained from STS to configure environment variables.
macOS
Open the terminal.
Run the following command:
nano ~/.bash_profile
Add the temporary access credentials, including AccessKey ID, AccessKey Secret, and security token, that are obtained from STS to the end of the file.
export MNS_ACCESS_KEY_ID=LTA**** export MNS_ACCESS_KEY_SECRET=moiEs**** export MNS_SESSION_TOKEN=CAES****
Press
Ctrl
+X
, pressY
to save the file, and then pressEnter
to close the file.Run the following command to make the changes take effect:
source ~/.bash_profile
Run the following command to verify the configurations of the environment variables:
echo $MNS_ACCESS_KEY_ID echo $MNS_ACCESS_KEY_SECRET echo $MNS_SESSION_TOKEN
Sample success response:
LTA**** moiEs**** CAES****
Linux
Open the terminal.
Run the following command:
sudo vim /etc/profile
Add the temporary access credentials, including AccessKey ID, AccessKey Secret, and security token, that are obtained from STS to the end of the file.
export MNS_ACCESS_KEY_ID=LTA**** export MNS_ACCESS_KEY_SECRET=moiEs**** export MNS_SESSION_TOKEN=CAES****
Press
Esc
to exit the edit mode. Then, enter:wq
and pressEnter
to save and close the file.Run the following command to make the changes take effect:
source /etc/profile
Run the following command to verify the configurations of the environment variables:
echo $MNS_ACCESS_KEY_ID echo $MNS_ACCESS_KEY_SECRET echo $MNS_SESSION_TOKEN
Sample success response:
LTA**** moiEs**** CAES****
Windows
Use the GUI
In this example, Windows 10 is used. You can perform the following steps to configure STS temporary access credentials by using environment variables on the GUI:
On the Windows desktop, right-click This PC and select Properties. On the page that appears, click Advanced system settings. In the System Properties dialog box, click Environment Variables on the Advanced tab. In the Environment Variables dialog box, click New in the User variables or System variables section.
Configure the environment variables. The following table describes the environment variables.
Variable
Example
MNS_ACCESS_KEY_ID
LTA****
MNS_ACCESS_KEY_SECRET
moiEs****
MNS_SESSION_TOKEN
CAES****
Run the following command to verify the configurations of the environment variables:
echo %MNS_ACCESS_KEY_ID% echo %MNS_ACCESS_KEY_SECRET% echo %MNS_SESSION_TOKEN%
Sample success response:
LTA**** moiEs**** CAES****
Use CMD
Open the CLI.
Run the following command to configure environment variables by using the temporary access credentials, including AccessKey ID, AccessKey secret, and security token:
set MNS_ACCESS_KEY_ID=LTA**** set MNS_ACCESS_KEY_SECRET=moiEs**** set MNS_SESSION_TOKEN=CAES****
Run the following command to make the changes take effect:
setx MNS_ACCESS_KEY_ID "%MNS_ACCESS_KEY_ID%" setx MNS_ACCESS_KEY_SECRET "%MNS_ACCESS_KEY_SECRET%" setx MNS_SESSION_TOKEN "%MNS_SESSION_TOKEN%"
Run the following command to verify the configurations of the environment variables:
echo %MNS_ACCESS_KEY_ID% echo %MNS_ACCESS_KEY_SECRET% echo %MNS_SESSION_TOKEN%
Sample success response:
LTA**** moiEs**** CAES****
Use Windows PowerShell
Press
Win+X
.Specify STS temporary access credentials.
Valid for the current session
In the message that appears, select Windows PowerShell.
Specify the following environment variables for the current session:
$env:MNS_ACCESS_KEY_ID = "LTA****" $env:MNS_ACCESS_KEY_SECRET = "moiEs****" $env:MNS_SESSION_TOKEN = "CAE****"
Valid for all new sessions
In the message that appears, select Windows PowerShell.
Specify the following environment variables for all new sessions:
[System.Environment]::SetEnvironmentVariable('MNS_ACCESS_KEY_ID', 'LTA****', [System.EnvironmentVariableTarget]::User) [System.Environment]::SetEnvironmentVariable('MNS_ACCESS_KEY_SECRET', 'moiEs****', [System.EnvironmentVariableTarget]::User) [System.Environment]::SetEnvironmentVariable('MNS_SESSION_TOKEN', 'CAES****', [System.EnvironmentVariableTarget]::User)
Valid for all users
In the message that appears, select Windows PowerShell (Administrator).
Specify the following environment variables for all users:
[System.Environment]::SetEnvironmentVariable('MNS_ACCESS_KEY_ID', 'LTA****', [System.EnvironmentVariableTarget]::Machine) [System.Environment]::SetEnvironmentVariable('MNS_ACCESS_KEY_SECRET', 'moiEs****', [System.EnvironmentVariableTarget]::Machine) [System.Environment]::SetEnvironmentVariable('MNS_SESSION_TOKEN', 'CAES****', [System.EnvironmentVariableTarget]::Machine)
Run the following command to verify the configurations of the environment variables:
Get-ChildItem env:MNS_ACCESS_KEY_ID Get-ChildItem env:MNS_ACCESS_KEY_SECRET Get-ChildItem env:MNS_SESSION_TOKEN
Sample success response:
LTA**** moiEs**** CAES****
Obtain the temporary access credentials obtained from STS from the environment variables.
accid = os.getenv("MNS_ACCESS_KEY_ID") acckey = os.getenv("MNS_ACCESS_KEY_SECRET") token = os.getenv("MNS_SESSION_TOKEN") or ""
Embed access credentials in code
WarningSecurity issues may occur if you embed access credentials in code. Leaked access credentials can be used by attackers to access your resources, which causes data breaches. To ensure security, we recommend that you use more secure methods, such as obtaining access credentials from environment variables.
accid = os.getenv("MNS_ACCESS_KEY_ID") acckey = os.getenv("MNS_ACCESS_KEY_SECRET") token = os.getenv("MNS_SESSION_TOKEN") or "" endpoint = "example.endpoint" my_account = Account(endpoint, accid, acckey, token)