Initializing an Instance of ObsClient
API Description
ObsClient functions as the Python client for accessing OBS. It offers users a series of APIs for interaction with OBS. These APIs are used for managing resources, such as buckets and objects, stored in OBS.
Method Definition
ObsClient(
access_key_id='*** Provide your Access Key ***',
secret_access_key='*** Provide your Secret Key ***',
server='https://your-endpoint'
)
Constructor Parameter Description
|
Parameter |
Description |
Recommended Value |
|---|---|---|
|
access_key_id |
Access key ID (AK) The default value is an empty string, indicating an anonymous user. |
N/A |
|
secret_access_key |
Secret access key (SK) The default value is an empty string, indicating an anonymous user. |
N/A |
|
security_token |
Security token in the temporary access keys |
N/A |
|
server |
Endpoint for accessing OBS, which can contain the protocol type, domain name, and port number. For example, https://your-endpoint:443. For security purposes, you are advised to use HTTPS. |
N/A |
|
max_retry_count |
Maximum number of retries when an HTTP/HTTPS connection is abnormal. The default value is 3. |
[1,5] |
|
max_redirect_count |
Maximum number of times that the HTTP/HTTPS request is redirected. The default value is 10. |
[1,10] |
|
timeout |
Timeout period (in seconds) of an HTTP/HTTPS request. The default value is 60. |
[10,60] |
|
ssl_verify |
Whether to verify server-side certificates. Possible values are:
The default value is False. |
N/A |
|
chunk_size |
Chunk size (in bytes) set for reading and writing socket streams. The default value is 65536. |
Default |
|
long_conn_mode |
Whether to enable the persistent connection mode. The default value is False. |
N/A |
|
proxy_host |
Host IP address of the proxy server. This value is null by default. |
N/A |
|
proxy_port |
Port number of the proxy server. This value is null by default. |
N/A |
|
proxy_username |
User name used for connecting to the proxy server. This value is null by default. |
N/A |
|
proxy_password |
Password used for connecting to the proxy server. This value is null by default. |
N/A |
|
is_cname |
Whether to use self-defined domain name to access OBS. The default value is False. |
N/A |
|
security_providers |
Specifies the allowed access key search methods. The default value is None.
NOTE:
The value of security_providers must be in a list. The default value None indicates the default search methods to obtain the access keys from the environment variables and from ECSs. If this parameter is specified, the default search methods are not provided. Instead, the search methods specified by security_providers are used. |
N/A |
|
security_provider_policy |
Specifies the allowed access key search policy. The default value is None.
NOTE:
|
N/A |
- Parameters whose recommended value is N/A need to be set according to the actual conditions.
- If the network is unstable, you are advised to set a larger value for timeout.
- If the value of server does not contain any protocol, HTTPS is used by default.
- If the persistent connection mode is enabled, you must call ObsClient.close to close ObsClient explicitly to reclaim connection resources.
- For the sake of high DNS resolution performance and OBS reliability, you can set server only to the domain name of OBS, instead of the IP address.
Sample Code
- You can create an instance of ObsClient by using a constructor function. Sample code for creating an instance of ObsClient using permanent access keys (AK/SK):
# Import the module. from obs import ObsClient # Create an instance of ObsClient. obsClient = ObsClient( access_key_id='*** Provide your Access Key ***', secret_access_key='*** Provide your Secret Key ***', server='https://your-endpoint' ) # Use the instance to access OBS. # Close ObsClient. obsClient.close() - Sample code for creating an instance of ObsClient using temporary access keys (AK/SK and security token):
# Import the module. from obs import ObsClient # Create an instance of ObsClient. obsClient = ObsClient( access_key_id='*** Provide your Access Key ***', secret_access_key='*** Provide your Secret Key ***', security_token='*** Provide your Security Token ***', server='https://your-endpoint' ) # Use the instance to access OBS. # Close ObsClient. obsClient.close() - You can also create an instance of ObsClient by using temporary access keys obtained by configuring system environment variables or by accessing an ECS.
- Sample code for creating an instance of ObsClient using ENV:
# Import the module. from obs import ObsClient from obs import loadtoken # Create an instance of ObsClient. # Provide ENV to obtain the access keys. obsClient = ObsClient( server='https://your-endpoint', security_provider_policy='ENV' ) # Use the instance to access OBS. # Close ObsClient. obsClient.close()
In the preceding method, access keys are searched from the environment variables of the current system. The OBS_ACCESS_KEY_ID and OBS_SECRET_ACCESS_KEY fields need to be defined in the corresponding environment variables. If temporary access keys are used, the OBS_SECURITY_TOKEN field must also be defined in the environment variables.
- Sample code for creating an instance of ObsClient using ECS:
# Import the module. from obs import ObsClient from obs import loadtoken # Create an instance of ObsClient. # Provide ECS to obtain the temporary access keys. obsClient = ObsClient( server='https://your-endpoint', security_provider_policy='ECS' ) # Use the instance to access OBS. # Close ObsClient. obsClient.close()
When an application is deployed on an ECS, temporary access keys can be obtained automatically using the preceding methods and updated periodically.
When obtaining temporary access keys using this method, ensure that the UTC time of the server is the same as that of the environment where the application is deployed. Otherwise, the temporary access keys may fail to be updated.
- Sample code for creating an instance of ObsClient using ENV:
- In addition to the preceding methods, you can also search in sequence to obtain the corresponding access keys from the environment variables and ECSs.
- You can set security_provider_policy to OBS_DEFAULT to specify that ObsClient searches for access keys in sequence.
# Import the module. from obs import ObsClient from obs import loadtoken # Create an instance of ObsClient. # Search for access keys from environment variables and ECSs in sequence. obsClient = ObsClient( server='https://your-endpoint', security_provider_policy='OBS_DEFAULT' ) # Use the instance to access OBS. # Close ObsClient. obsClient.close()
In the preceding method, security_provider_policy is set to OBS_DEFAULT, which specifies that ObsClient searches for access keys in sequence from the predefined list. By default, the system provides two predefined search methods: obtaining the access keys from the environment variables and obtaining from ECSs. ObsClient searches for the access keys from the environment variables first and then from ECSs. In this case, ObsClient is created using the first pair of access keys obtained in the search.
- You can set security_provider_policy to OBS_DEFAULT to specify that ObsClient searches for access keys in sequence.
- The project can contain one or more instances of ObsClient.
- ObsClient is thread secure and can be simultaneously used by multiple threads.
Last Article: Initialization
Next Article: Initializing a BucketClient Instance
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.