To use Object Storage Service (OSS) SDK for Python to initiate a request, you must configure 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 the requirements for authentication and authorization. This topic describes how to configure temporary access credentials and long-term access credentials.
Prerequisites
OSS SDK for Python is installed. For more information, see Installation.
Access credentials
You can choose from the following types of access credentials:
Temporary access credentials: For scenarios that require high security, such as temporary access authorization of applications to access OSS, we recommend that you use temporary access credentials. Temporary access credentials have a validity period for access. This helps prevent access credentials from being leaked. Temporary access credentials support fine-grained access control to prevent security risks caused by excessive permissions. For more information, see Use temporary access credentials.
Long-term access credentials: To ensure security, we recommend that you do not use long-term access credentials. For scenarios that require convenience, long-term access credentials can prevent multiple refreshes in a long 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. When long-term access credentials are leaked or no longer used, you can delete or disable the long-term access credentials to prevent security risks. For more information, see Use long-term access credentials.
Use temporary access credentials
If you want to use OSS SDK for Python to temporarily access OSS resources, you can use one of the following methods to configure temporary access credentials:
Configure Security Token Service (STS) temporary access credentials: If you want to access OSS within a specific period of time, you can use STS to obtain temporary access credentials. The temporary access credentials provided by STS do not disclose the AccessKey pair of your RAM user. This ensures that your access to OSS resources is secure.
Configure a RAM role: If you want to grant a RAM user of your Alibaba Cloud account or a RAM user of another Alibaba Cloud account the permissions to access OSS resources, you can configure a RAM role for the RAM user.
Configure a RAM role for an Elastic Compute Service (ECS) instance: If you want to access OSS resources from an ECS instance, you can use a RAM role of the ECS instance to access OSS resources. You can attach a RAM role to an ECS instance to access OSS resources from the instance by using temporary credentials that are obtained from STS. STS temporary access credentials are automatically generated and updated. Applications can obtain STS temporary access credentials by using the instance metadata URL.
Configure STS temporary access credentials
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 RAM users.
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 provided by STS.
Environment variables
Use the temporary access credentials provided by STS to configure environment variables.
macOS
Open the terminal.
Run the following command:
nano ~/.bash_profile
Add the temporary access credentials (AccessKeyId, AccessKeySecret, and SecurityToken) obtained from STS to the end of the file.
export OSS_ACCESS_KEY_ID=STS.NV5ZCwphFSXqscqUHAKbH**** export OSS_ACCESS_KEY_SECRET=IrVTNZNy5yQelTETg0cZML3TQn**** export OSS_SESSION_TOKEN=CAES+wMIARKAAZhjH0EUOIhJMQBMjRywXq7MQ/cjLYg80Aho1ek0Jm63XMhr9Oc5s˙∂˙∂3qaPer8p1YaX1NTDiCFZWFkvlHf1pQhuxfKBc+mRR9KAbHUefqH+rdjZqjTF7p2m1wJXP8S6k+G2MpHrUe6TYBkJ43GhhTVFMuM3BZajY3VjZWOXBIODRIR1FKZjIiEjMzMzE0MjY0NzM5MTE4NjkxMSoLY2xpZGSSDgSDGAGESGTETqOio6c2RrLWRlbW8vKgoUYWNzOm9zczoqOio6c2RrLWRlbW9KEDExNDg5MzAxMDcyNDY4MThSBTI2ODQyWg9Bc3N1bWVkUm9sZVVzZXJgAGoSMzMzMTQyNjQ3MzkxMTg2OTExcglzZGstZGVt****
Press
Ctrl
+X
, pressY
to save the file, and then pressEnter
to close the file.Run the following command for the changes to take effect:
source ~/.bash_profile
Run the following command to verify the configurations of the environment variables:
echo $OSS_ACCESS_KEY_ID echo $OSS_ACCESS_KEY_SECRET echo $OSS_SESSION_TOKEN
Sample success response:
STS.NV5ZCwphFSXqscqUHAKbH**** IrVTNZNy5yQelTETg0cZML3TQn**** CAES+wMIARKAAZhjH0EUOIhJMQBMjRywXq7MQ/cjLYg80Aho1ek0Jm63XMhr9Oc5s˙∂˙∂3qaPer8p1YaX1NTDiCFZWFkvlHf1pQhuxfKBc+mRR9KAbHUefqH+rdjZqjTF7p2m1wJXP8S6k+G2MpHrUe6TYBkJ43GhhTVFMuM3BZajY3VjZWOXBIODRIR1FKZjIiEjMzMzE0MjY0NzM5MTE4NjkxMSoLY2xpZGSSDgSDGAGESGTETqOio6c2RrLWRlbW8vKgoUYWNzOm9zczoqOio6c2RrLWRlbW9KEDExNDg5MzAxMDcyNDY4MThSBTI2ODQyWg9Bc3N1bWVkUm9sZVVzZXJgAGoSMzMzMTQyNjQ3MzkxMTg2OTExcglzZGstZGVt****
Linux
Open the terminal.
Run the following command:
sudo vim /etc/profile
Add the temporary access credentials (AccessKeyId, AccessKeySecret, and SecurityToken) obtained from STS to the end of the file.
export OSS_ACCESS_KEY_ID=STS.NV5ZCwphFSXqscqUHAKbH**** export OSS_ACCESS_KEY_SECRET=IrVTNZNy5yQelTETg0cZML3TQn**** export OSS_SESSION_TOKEN=CAES+wMIARKAAZhjH0EUOIhJMQBMjRywXq7MQ/cjLYg80Aho1ek0Jm63XMhr9Oc5s˙∂˙∂3qaPer8p1YaX1NTDiCFZWFkvlHf1pQhuxfKBc+mRR9KAbHUefqH+rdjZqjTF7p2m1wJXP8S6k+G2MpHrUe6TYBkJ43GhhTVFMuM3BZajY3VjZWOXBIODRIR1FKZjIiEjMzMzE0MjY0NzM5MTE4NjkxMSoLY2xpZGSSDgSDGAGESGTETqOio6c2RrLWRlbW8vKgoUYWNzOm9zczoqOio6c2RrLWRlbW9KEDExNDg5MzAxMDcyNDY4MThSBTI2ODQyWg9Bc3N1bWVkUm9sZVVzZXJgAGoSMzMzMTQyNjQ3MzkxMTg2OTExcglzZGstZGVt****
Press
Esc
to exit the edit mode. Then, enter:wq
and pressEnter
to save and close the file.Run the following command for the changes to take effect:
source /etc/profile
Run the following command to verify the configurations of the environment variables:
echo $OSS_ACCESS_KEY_ID echo $OSS_ACCESS_KEY_SECRET echo $OSS_SESSION_TOKEN
Sample success response:
STS.NV5ZCwphFSXqscqUHAKbH**** IrVTNZNy5yQelTETg0cZML3TQn**** CAES+wMIARKAAZhjH0EUOIhJMQBMjRywXq7MQ/cjLYg80Aho1ek0Jm63XMhr9Oc5s˙∂˙∂3qaPer8p1YaX1NTDiCFZWFkvlHf1pQhuxfKBc+mRR9KAbHUefqH+rdjZqjTF7p2m1wJXP8S6k+G2MpHrUe6TYBkJ43GhhTVFMuM3BZajY3VjZWOXBIODRIR1FKZjIiEjMzMzE0MjY0NzM5MTE4NjkxMSoLY2xpZGSSDgSDGAGESGTETqOio6c2RrLWRlbW8vKgoUYWNzOm9zczoqOio6c2RrLWRlbW9KEDExNDg5MzAxMDcyNDY4MThSBTI2ODQyWg9Bc3N1bWVkUm9sZVVzZXJgAGoSMzMzMTQyNjQ3MzkxMTg2OTExcglzZGstZGVt****
Windows
Open the CLI.
Run the following command to configure the temporary access credentials (AccessKeyId, AccessKeySecret, and SecurityToken) obtained from STS:
set OSS_ACCESS_KEY_ID=STS.NV5ZCwphFSXqscqUHAKbH**** set OSS_ACCESS_KEY_SECRET=IrVTNZNy5yQelTETg0cZML3TQn**** set OSS_SESSION_TOKEN=CAES+wMIARKAAZhjH0EUOIhJMQBMjRywXq7MQ/cjLYg80Aho1ek0Jm63XMhr9Oc5s˙∂˙∂3qaPer8p1YaX1NTDiCFZWFkvlHf1pQhuxfKBc+mRR9KAbHUefqH+rdjZqjTF7p2m1wJXP8S6k+G2MpHrUe6TYBkJ43GhhTVFMuM3BZajY3VjZWOXBIODRIR1FKZjIiEjMzMzE0MjY0NzM5MTE4NjkxMSoLY2xpZGSSDgSDGAGESGTETqOio6c2RrLWRlbW8vKgoUYWNzOm9zczoqOio6c2RrLWRlbW9KEDExNDg5MzAxMDcyNDY4MThSBTI2ODQyWg9Bc3N1bWVkUm9sZVVzZXJgAGoSMzMzMTQyNjQ3MzkxMTg2OTExcglzZGstZGVt****
Run the following command for the changes to take effect:
setx OSS_ACCESS_KEY_ID "%OSS_ACCESS_KEY_ID%" setx OSS_ACCESS_KEY_SECRET "%OSS_ACCESS_KEY_SECRET%" setx OSS_SESSION_TOKEN "%OSS_SESSION_TOKEN%"
Run the following command to verify the configurations of the environment variables:
echo %OSS_ACCESS_KEY_ID% echo %OSS_ACCESS_KEY_SECRET% echo %OSS_SESSION_TOKEN%
Sample success response:
STS.NV5ZCwphFSXqscqUHAKbH**** IrVTNZNy5yQelTETg0cZML3TQn**** CAES+wMIARKAAZhjH0EUOIhJMQBMjRywXq7MQ/cjLYg80Aho1ek0Jm63XMhr9Oc5s˙∂˙∂3qaPer8p1YaX1NTDiCFZWFkvlHf1pQhuxfKBc+mRR9KAbHUefqH+rdjZqjTF7p2m1wJXP8S6k+G2MpHrUe6TYBkJ43GhhTVFMuM3BZajY3VjZWOXBIODRIR1FKZjIiEjMzMzE0MjY0NzM5MTE4NjkxMSoLY2xpZGSSDgSDGAGESGTETqOio6c2RrLWRlbW8vKgoUYWNzOm9zczoqOio6c2RrLWRlbW9KEDExNDg5MzAxMDcyNDY4MThSBTI2ODQyWg9Bc3N1bWVkUm9sZVVzZXJgAGoSMzMzMTQyNjQ3MzkxMTg2OTExcglzZGstZGVt****
Obtain the temporary access credentials provided by STS from the environment variables.
# -*- coding: utf-8 -*- import oss2 from oss2.credentials import EnvironmentVariableCredentialsProvider # Use the temporary AccessKey pair and security token obtained from the environment variables to configure the access credentials. auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
Embed access credentials in code
WarningSecurity issues may occur when you embed access credentials in code. If access credentials are leaked, attackers can use the access credentials to access your OSS resources, which may cause data loss. To ensure security, we recommend that you use more secure methods, such as obtaining access credentials from environment variables.
# -*- coding: utf-8 -*- import oss2 # Specify the temporary AccessKey pair obtained from STS. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. sts_access_key_id = 'OSS_ACCESS_KEY_ID' sts_access_key_secret = 'OSS_ACCESS_KEY_SECRET' # Specify the security token obtained from STS. security_token = 'OSS_SESSION_TOKEN' # Initialize the StsAuth instance based on the authentication information in the temporary access credentials. auth = oss2.StsAuth(sts_access_key_id, sts_access_key_secret, security_token)
Configure a RAM role
Run the following command to install
alibabacloud_credentials
:pip install alibabacloud_credentials
Obtain the AccessKey pair of a RAM user.
For more information, see Create an AccessKey pair.
ImportantTo reduce the risk that the AccessKey pair of a RAM user is leaked, we recommend that you rotate an AccessKey pair 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.
Obtain the RamRoleArn of the RAM role.
NoteRamRoleArn is the Alibaba Cloud Resource Name (ARN) of the RAM role, which is the ID of the RAM role to be assumed. Format: acs:ram::$accountID:role/$roleName. $accountID: the ID of the Alibaba Cloud account. $roleName: the name of the RAM role.
For more information, see View the information about a RAM role.
Configure the AccessKey pair of the RAM user and RamRoleArn of the RAM role as the temporary access credentials.
Environment variables
Configure environment variables by using the obtained AccessKey pair of the RAM user and RamRoleArn of the RAM role.
macOS
Open the terminal.
Run the following command:
nano ~/.bash_profile
Add the AccessKey pair of the RAM user and RamRoleArn of the RAM role to the end of the file.
export OSS_ACCESS_KEY_ID=LTAI4GDty8ab9W4Y1D**** export OSS_ACCESS_KEY_SECRET=IrVTNZNy5yQelTETg0cZML3TQn**** export OSS_STS_ROLE_ARN=acs:ram::17464958********:role/ossststest
Press
Ctrl
+X
, pressY
to save the file, and then pressEnter
to close the file.Run the following command for the changes to take effect:
source /etc/profile
Run the following command to verify the configurations of the environment variables:
echo $OSS_ACCESS_KEY_ID echo $OSS_ACCESS_KEY_SECRET echo $OSS_STS_ROLE_ARN
Sample success response:
LTAI4GDty8ab9W4Y1D**** IrVTNZNy5yQelTETg0cZML3TQn**** acs:ram::17464958********:role/ossststest
Linux
Open the terminal.
Run the following command:
sudo vim /etc/profile
Add the AccessKey pair of the RAM user and RamRoleArn of the RAM role to the end of the file.
export OSS_ACCESS_KEY_ID=LTAI4GDty8ab9W4Y1D**** export OSS_ACCESS_KEY_SECRET=IrVTNZNy5yQelTETg0cZML3TQn**** export OSS_STS_ROLE_ARN=acs:ram::17464958********:role/ossststest
Press
Esc
to exit the edit mode. Then, enter:wq
and pressEnter
to save and close the file.Run the following command for the changes to take effect:
source /etc/profile
Run the following command to verify the configurations of the environment variables:
echo $OSS_ACCESS_KEY_ID echo $OSS_ACCESS_KEY_SECRET echo $OSS_STS_ROLE_ARN
Sample success response:
LTAI4GDty8ab9W4Y1D**** IrVTNZNy5yQelTETg0cZML3TQn**** acs:ram::17464958********:role/ossststest
Windows
Open the CLI.
Run the following command to configure the AccessKey pair of the RAM user and RamRoleArn of the RAM role:
set OSS_ACCESS_KEY_ID=LTAI4GDty8ab9W4Y1D**** set OSS_ACCESS_KEY_SECRET=IrVTNZNy5yQelTETg0cZML3TQn**** set OSS_STS_ROLE_ARN=acs:ram::17464958********:role/ossststest
Run the following command for the changes to take effect:
setx OSS_ACCESS_KEY_ID "%OSS_ACCESS_KEY_ID%" setx OSS_ACCESS_KEY_SECRET "%OSS_ACCESS_KEY_SECRET%" setx OSS_STS_ROLE_ARN "%OSS_STS_ROLE_ARN%"
Run the following command to verify the configurations of the environment variables:
echo %OSS_ACCESS_KEY_ID% echo %OSS_ACCESS_KEY_SECRET% echo %OSS_STS_ROLE_ARN%
Sample success response:
LTAI4GDty8ab9W4Y1D**** IrVTNZNy5yQelTETg0cZML3TQn**** acs:ram::17464958********:role/ossststest
Obtain the AccessKey pair of the RAM user and RamRoleArn of the RAM role from the environment variables.
# -*- coding: utf-8 -*- import oss2 from alibabacloud_credentials.client import Client from alibabacloud_credentials.models import Config from oss2 import CredentialsProvider from oss2.credentials import Credentials import os class CredentialProviderWarpper(CredentialsProvider): def __init__(self, client): self.client = client def get_credentials(self): access_key_id = self.client.get_access_key_id() access_key_secret = self.client.get_access_key_secret() security_token = self.client.get_security_token() return Credentials(access_key_id, access_key_secret, security_token) config = Config( type='ram_role_arn', # Obtain the AccessKey pair of the RAM user from the environment variables. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. access_key_id=os.getenv('OSS_ACCESS_KEY_ID'), access_key_secret=os.getenv('OSS_ACCESS_KEY_ID'), # Obtain RamRoleArn of the RAM role from the environment variables. role_arn=os.getenv('OSS_STS_ROLE_ARN'), # Specify the session name of the RAM role. role_session_name='roleSessionName' ) cred = Client(config) credentials_provider = CredentialProviderWarpper(cred) # Configure temporary access credentials by using the AccessKey pair of the RAM user and RamRoleArn of the RAM role obtained from environment variables. auth = oss2.ProviderAuth(credentials_provider)
Embed access credentials in code
WarningSecurity issues may occur when you embed access credentials in code. If access credentials are leaked, attackers can use the access credentials to access your OSS resources, which may cause data loss. To ensure security, we recommend that you use more secure methods, such as obtaining access credentials from environment variables.
# -*- coding: utf-8 -*- import oss2 from alibabacloud_credentials.client import Client from alibabacloud_credentials.models import Config from oss2 import CredentialsProvider from oss2.credentials import Credentials import os class CredentialProviderWarpper(CredentialsProvider): def __init__(self, client): self.client = client def get_credentials(self): access_key_id = self.client.get_access_key_id() access_key_secret = self.client.get_access_key_secret() security_token = self.client.get_security_token() return Credentials(access_key_id, access_key_secret, security_token) config = Config( type='ram_role_arn', # Specify the AccessKey pair of the RAM user. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. access_key_id='OSS_ACCESS_KEY_ID', access_key_secret='OSS_ACCESS_KEY_ID', # Specify the RamRoleArn of the RAM role. role_arn='OSS_STS_ROLE_ARN', # Specify the session name of the RAM role. role_session_name='roleSessionName' ) cred = Client(config) credentials_provider = CredentialProviderWarpper(cred) # Configure temporary access credentials by using the AccessKey pair of the RAM user and RamRoleArn of the RAM role obtained from environment variables. auth = oss2.ProviderAuth(credentials_provider)
Configure a RAM role for an ECS instance
Run the following command to install
alibabacloud_credentials
:pip install alibabacloud_credentials
Attach a RAM role to an ECS instance.
For more information, see Attach an instance RAM role to an ECS instance.
Configure temporary access credentials by using the RAM role for the ECS instance.
# -*- coding: utf-8 -*- import oss2 from alibabacloud_credentials.client import Client from alibabacloud_credentials.models import Config from oss2 import CredentialsProvider from oss2.credentials import Credentials import os class CredentialProviderWarpper(CredentialsProvider): def __init__(self, client): self.client = client def get_credentials(self): access_key_id = self.client.get_access_key_id() access_key_secret = self.client.get_access_key_secret() security_token = self.client.get_security_token() return Credentials(access_key_id, access_key_secret, security_token) config = Config( type='ecs_ram_role', # Specify the type of the temporary access credentials. Set the value to ecs_ram_role. role_name='ECSRAMRole' # Specify the name of the RAM role that is attached to the ECS instance. This parameter is optional. If you do not configure this parameter, the system automatically searches this parameter. We recommended that you configure this parameter to reduce the number of requests. ) cred = Client(config) credentials_provider = CredentialProviderWarpper(cred) # Configure temporary access credentials by using the AccessKey pair of the RAM user and RamRoleArn of the RAM role obtained from environment variables. auth = oss2.ProviderAuth(credentials_provider)
Use long-term access credentials
If you want to use OSS SDK for Python to access OSS resources in an application or service for a long period of time, you can perform the following steps to configure long-term access credentials.
If you want to access OSS resources for a long period of time, you can use the AccessKey pair of a RAM user to access OSS resources.
Configure the AccessKey pair of a RAM user
Obtain the AccessKey pair of a RAM user.
For more information, see Create an AccessKey pair.
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.
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 OSS_ACCESS_KEY_ID=LTAI4GDty8ab9W4Y1D**** export OSS_ACCESS_KEY_SECRET=IrVTNZNy5yQelTETg0cZML3TQn****
Press
Ctrl
+X
, pressY
to save the file, and then pressEnter
to close the file.Run the following command for the changes to take effect:
source ~/.bash_profile
Run the following command to verify the configurations of the environment variables:
echo $OSS_ACCESS_KEY_ID echo $OSS_ACCESS_KEY_SECRET
Sample success response:
LTAI4GDty8ab9W4Y1D**** IrVTNZNy5yQelTETg0cZML3TQn****
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 OSS_ACCESS_KEY_ID=LTAI4GDty8ab9W4Y1D**** export OSS_ACCESS_KEY_SECRET=IrVTNZNy5yQelTETg0cZML3TQn****
Press
Esc
to exit the edit mode. Then, enter:wq
and pressEnter
to save and close the file.Run the following command for the changes to take effect:
source /etc/profile
Run the following command to verify the configurations of the environment variables:
echo $OSS_ACCESS_KEY_ID echo $OSS_ACCESS_KEY_SECRET
Sample success response:
LTAI4GDty8ab9W4Y1D**** IrVTNZNy5yQelTETg0cZML3TQn****
Windows
Open the CLI.
Run the following command to configure the AccessKey pair of the RAM user:
set OSS_ACCESS_KEY_ID=LTAI4GDty8ab9W4Y1D**** set OSS_ACCESS_KEY_SECRET=IrVTNZNy5yQelTETg0cZML3TQn****
Run the following command for the changes to take effect:
setx OSS_ACCESS_KEY_ID "%OSS_ACCESS_KEY_ID%" setx OSS_ACCESS_KEY_SECRET "%OSS_ACCESS_KEY_SECRET%"
Run the following command to verify the configurations of the environment variables:
echo %OSS_ACCESS_KEY_ID% echo %OSS_ACCESS_KEY_SECRET%
Sample success response:
LTAI4GDty8ab9W4Y1D**** IrVTNZNy5yQelTETg0cZML3TQn****
Obtain the AccessKey pair of the RAM user from the environment variables.
# -*- coding: utf-8 -*- import oss2 from oss2.credentials import EnvironmentVariableCredentialsProvider # Configure temporary access credentials by using the AccessKey pair of the RAM user obtained from environment variables. auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
Embed access credentials in code
WarningSecurity issues may occur when you embed access credentials in code. If access credentials are leaked, attackers can use the access credentials to access your OSS resources, which may cause data loss. To ensure security, we recommend that you use more secure methods, such as obtaining access credentials from environment variables.
# -*- coding: utf-8 -*- import oss2 # Specify the AccessKey pair of the RAM user. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. accessKeyId = 'yourAccessKeyId' accessKeySecret = 'yourAccessKeySecret' # Configure access credentials by using the AccessKey pair of the RAM user embedded in code. auth = oss2.Auth(accessKeyId, accessKeySecret)
What to do next
After you configure the access credentials, you must initialize an OSSClient instance. For more information, see Initialization.