Volcengine Python SDK

repository·master·Indexed 41 days ago

https://github.com/volcengine/volcengine-python-sdk

Programmatic access to Volcengine cloud services, including ECS, Auto Scaling, and IAM. Version 5.0.50 provides a structured, object-oriented interface for managing resources, featuring comprehensive credential management via providers like Static, STS, and EcsRole, as well as flexible configuration for endpoints, transport, and error handling. Requires Python >= 2.7 for the standard SDK and >= 3.6 for the Ark Runtime Service.

Tokens
34.9K
Snippets
75
Records
99
Agent score
51%

What's inside Volcengine

  1. Install and use the Volcengine Python SDK

    master

    The Volcengine Python SDK is designed to simplify API integration and reduce maintenance costs. To use the SDK, ensure your environment meets the following requirements:

    • Standard SDK: Python version >= 2.7.
    • Ark Runtime (volcenginesdkarkruntime): Python version >= 3.6 is required.

    Integration involves configuring credentials, endpoints, transport settings, and error handling strategies as detailed in the documentation.

  2. Overview of Volcengine Credential Providers

    master

    The Volcengine Python SDK uses CredentialProvider objects to resolve authentication credentials. Providers can be static or support automatic background refreshing of temporary credentials.

    ProviderPurposeRefresh SupportTypical Scenario
    StaticCredentialProviderStatic AK/SK(/Token)NoLong-lived server-side credentials
    StsCredentialProviderSTS AssumeRoleYesRole-based temporary credentials
    StsOidcCredentialProviderSTS AssumeRoleWithOIDCYesOIDC federation
    StsSamlCredentialProviderSTS AssumeRoleWithSAMLYesSAML federation
    EnvironmentVariableCredentialProviderRead from env varsNoCI/CD and container env injection
    CLIConfigCredentialProviderRead from ~/.volcengine/config.jsonDepends on modeReuse CLI login/profile
    EcsRoleCredentialProviderRead from ECS IMDSYesECS instance role credentials
    DefaultCredentialProviderChain wrapperDepends on delegated providerNo AK/SK in application code
  3. Overview of CredentialProviders

    master

    The Volcengine Python SDK supports two ways to handle credentials: explicit credentials and automatic parsing via CredentialProvider. Different providers suit different scenarios:

    ProviderPurposeAuto-refreshesTypical Scenario
    StaticCredentialProviderStatic AK/SK/TokenNoLong-term server credentials
    StsCredentialProviderSTS AssumeRoleYesTemporary role-based credentials
    StsOidcCredentialProviderSTS AssumeRoleWithOIDCYesOIDC federated identity
    StsSamlCredentialProviderSTS AssumeRoleWithSAMLYesSAML federated identity
    EnvironmentVariableCredentialProviderReads environment variablesNoCI/CD, container injection
    CLIConfigCredentialProviderReads ~/.volcengine/config.jsonDepends on modeReusing CLI configuration
    EcsRoleCredentialProviderReads ECS IMDSYesECS instance roles
    DefaultCredentialProviderDefault chain wrapperDepends on providerWhen business code shouldn't handle AK/SK directly
  4. Override global configuration with RuntimeOption

    master

    The SDK supports two levels of configuration: global configuration via volcenginesdkcore.Configuration and request-level configuration via volcenginesdkcore.interceptor.RuntimeOption.

    Settings provided in RuntimeOption will override the global configuration for that specific API call. This is useful for changing the scheme or enabling/disabling client_side_validation on a per-request basis.

    import volcenginesdkcore,volcenginesdkecs
    from volcenginesdkcore.rest import ApiException
    from volcenginesdkcore.interceptor import RuntimeOption
    
    # Global configuration
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "Your ak"
    configuration.sk = "Your sk"
    configuration.scheme="http" # Set global scheme
    volcenginesdkcore.Configuration.set_default(configuration)
    
    # Request-level runtime parameters (overrides global configuration)
    runtime_options = RuntimeOption(
        scheme="http",
        client_side_validation=True, # Enable client-side validation (default is True)
    )
    
    api_instance = volcenginesdkecs.ECSApi()
    create_command_request = volcenginesdkecs.CreateCommandRequest(
        command_content="ls -l",
        description="Your command description",
        name="Your command name",
        type="command",
        _configuration=runtime_options,  # Apply runtime parameters here
    )
    
    try:
        api_instance.create_command(create_command_request)
    except ApiException as e:
        pass
  5. How automated Endpoint addressing works

    master

    By default, the SDK uses an automated addressing mechanism. If you do not specify an Endpoint, the SDK automatically constructs the service address based on the service name and the Region.

    Addressing Logic

    1. Region Addressing: The SDK uses a built-in list of regions for automatic addressing. For regions not in the preset list, it defaults to open.volcengineapi.com. You can extend this list via the custom_bootstrap_region attribute in Configuration or the VOLC_BOOTSTRAP_REGION_LIST_CONF environment variable.
    2. DualStack (IPv6) Support: When enabled, the domain suffix changes from volcengineapi.com to volcengine-api.com.
      • Priority: use_dual_stack parameter > VOLC_ENABLE_DUALSTACK environment variable.
    3. Service Types:
      • Global Services (e.g., CDN, IAM): Uses {Service}.volcengineapi.com.
      • Regional Services (e.g., ECS, RDS): Uses {Service}.{region}.volcengineapi.com.

    Standard Addressing Rules

    Service TypeDualStackFormat
    GlobalYes{Service}.volcengine-api.com
    GlobalNo{Service}.volcengineapi.com
    RegionalYes{Service}.{region}.volcengine-api.com
    RegionalNo{Service}.{region}.volcengineapi.com
    import volcenginesdkcore
    
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "Your ak"
    configuration.sk = "Your sk"
    configuration.use_dual_stack = True # Enables IPv4 + IPv6 access
    configuration.custom_bootstrap_region = {
        "custom_example_region1": {},
        "custom_example_region2": {},
    }
    volcenginesdkcore.Configuration.set_default(configuration)
  6. Understand the default credential provider chain

    master

    If credentials are not explicitly provided in your code, the SDK attempts to find them using the following providers in order. The first successful provider is used:

    1. Environment Variable Provider: Looks for VOLCENGINE_ACCESS_KEY, VOLCENGINE_SECRET_KEY, and optionally VOLCENGINE_SESSION_TOKEN.
    2. OIDC Provider: Reads configuration from VOLCENGINE_OIDC_* variables.
    3. CLI Config Provider: Reads from the file specified by VOLCENGINE_CLI_CONFIG_FILE (defaults to ~/.volcengine/config.json).
    4. ECS IMDS Provider: Automatically discovers credentials from the ECS Instance Metadata Service.
  7. Specify the Request Scheme (HTTP vs HTTPS)

    master

    The SDK defaults to https. You can change the communication scheme globally via the Configuration object or per-request using RuntimeOption.

    Note: Settings provided via RuntimeOption at the API/Request level will override the global Configuration settings.

    import volcenginesdkcore, volcenginesdkecs
    from volcenginesdkcore.rest import ApiException
    from volcenginesdkcore.interceptor import RuntimeOption
    
    # Global configuration
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "Your ak"
    configuration.sk = "Your sk"
    configuration.scheme = "http" # Specify scheme
    volcenginesdkcore.Configuration.set_default(configuration)
    
    # API-level runtime parameter settings, overrides global configuration
    runtime_options = RuntimeOption(
        scheme="http",
        client_side_validation=True, # Enable client-side validation, enabled by default,
    )
    api_instance = volcenginesdkecs.ECSApi()
    create_command_request = volcenginesdkecs.CreateCommandRequest(
        command_content="ls -l",
        description="Your command description",
        name="Your command name",
        type="command",
        _configuration=runtime_options,  # Set runtime parameters
    )
    try:
        api_instance.create_command(create_command_request)
    except ApiException as e:
        pass
  8. How retry conditions work

    master

    A retry condition determines if a specific error or response warrants a retry attempt.

    DefaultRetryCondition

    The DefaultRetryCondition triggers a retry if:

    1. A network error occurs.
    2. A server-side rate-limiting error occurs.
    3. The error code is present in the user-defined retry_error_codes set.

    Customizing Retry Conditions

    You can implement custom logic by inheriting from RetryCondition or extending DefaultRetryCondition to reuse its existing logic.

    Implementing a base RetryCondition:

    from volcenginesdkcore.retryer.retry_condition import RetryCondition
    
    class CustomRetryCondition(RetryCondition):
        def should_retry(self, response, err):
            # type: (RESTResponse, Exception) -> bool
            # Access user-defined error codes via self.retry_error_codes
            return False

    Extending DefaultRetryCondition:

    from volcenginesdkcore.retryer.retry_condition import DefaultRetryCondition
    
    class CustomRetryCondition(DefaultRetryCondition):
        def should_retry(self, response, err):
            # type: (RESTResponse, Exception) -> bool
            # Use super() to keep default logic (network/rate-limit/custom codes)
            should_retry = super(CustomRetryCondition, self).should_retry(response, err)
            # Add additional custom logic here
            return should_retry or False
    from volcenginesdkcore.retryer.retry_condition import RetryCondition
    class CustomRetryCondition(RetryCondition):
        def should_retry(
            self,
            response,
            err
        ):
            # type: (RESTResponse, Exception) -> bool
            retry_error_codes = self.retry_error_codes # 可以获取到用户自定义的错误码
            #................实现自己逻辑
    
            return False
  9. How automatic endpoint resolution works

    master

    By default, the SDK automatically constructs the service address based on the service name and region.

    Resolution Logic

    1. Region Resolution: The SDK uses a built-in list of preset regions. If a region is not in the preset list and not user-configured, it defaults to open.volcengineapi.com. You can extend the list using the VOLC_BOOTSTRAP_REGION_LIST_CONF environment variable or the custom_bootstrap_region configuration key.
    2. DualStack (IPv6) Support: When use_dual_stack is set to True in configuration or the VOLC_ENABLE_DUALSTACK environment variable is set, the domain suffix changes from volcengineapi.com to volcengine-api.com.
    3. Endpoint Construction:
      • Global services (e.g., CDN, IAM): <ServiceName>.volcengineapi.com (or volcengine-api.com for dual-stack).
      • Regional services (e.g., ECS, RDS): <ServiceName>.<Region>.volcengineapi.com (or volcengine-api.com for dual-stack).

    Configuration Example

    import volcenginesdkcore
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "Your ak"
    configuration.sk = "Your sk"
    configuration.use_dual_stack = True # Enable dual-stack network (IPv4 + IPv6) access, default is False
    configuration.custom_bootstrap_region = {
        "custom_example_region1": {},
        "custom_example_region2": {},
    } # Custom auto-resolution region list
    volcenginesdkcore.Configuration.set_default(configuration)
  10. Request interceptor retry lifecycle

    master

    By default, request interceptors execute before every HTTP attempt, including retries.

    If you have a custom interceptor that should only run once per logical SDK call (rather than once per network attempt), set run_on_retry = False when defining the interceptor.

    Note that built-in interceptors for request building, runtime parameters, and endpoint parsing are designed to execute only before the first attempt.

  11. How request interceptor retry lifecycle works

    master

    Request interceptors run once for every HTTP attempt, including retries.

    Important for Custom Interceptors: If you are writing a custom interceptor that should only execute once per logical SDK call (rather than once per network attempt), you must set run_on_retry = False.

    Note that the SDK's internal interceptors for request building, runtime options, and endpoint resolution are optimized to run only on the first attempt.

  12. Classify and handle Volcengine SDK errors

    master

    When calling APIs, errors can be categorized into four main types. You should implement different handling strategies (like retrying for network errors or fixing parameters for client errors) based on these classifications:

    1. Network/Timeout Errors: Occur during DNS resolution or request timeouts. These are typically raised as exceptions from urllib3 or socket.
    2. Client Errors: Occur before the request reaches the server due to parameter validation or authentication issues. These manifest as volcenginesdkcore.rest.ApiException or ValueError.
    3. Server Errors: Occur when the request reaches the server but the business logic fails. These are returned as volcenginesdkcore.rest.ApiException where the status attribute is not 0.
    4. Other Errors: A catch-all for any other Exception types.