Qiniu Cloud SDK for Python

repository·master·Indexed 19 days ago

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

A Python SDK for interacting with Qiniu Cloud services. It provides tools for uploading data to buckets, managing compute service accounts via AccountClient, and interacting with the Qiniu Sandbox API using SandboxClient. Key features include application lifecycle management, sandbox template and injection rule configuration, and resource monitoring.

Tokens
2.6K
Snippets
10
Records
15
Agent score
67%

What's inside qiniu-python-sdk

  1. Upload data to Qiniu Cloud

    master

    To upload data, follow these steps:

    1. Initialize qiniu.Auth with your access_key and secret_key.
    2. Generate an upload token using q.upload_token(bucket_name).
    3. Use qiniu.put_data(token, key, data) to perform the upload.

    The put_data method returns a tuple (ret, info). If ret is not None, the upload was successful. If ret is None, info contains the error message.

    import qiniu
    
    # Initialize authentication
    q = qiniu.Auth(access_key, secret_key)
    key = 'hello'
    data = 'hello qiniu!'
    
    # Generate upload token
    token = q.upload_token(bucket_name)
    
    # Upload data
    ret, info = qiniu.put_data(token, key, data)
    
    if ret is not None:
        print('All is OK')
    else:
        print(info) # error message in info
  2. Obtain a QCOS client for an application

    master

    To manage resources within a specific application, you can obtain a QcosClient using the AccountClient.

    • get_qcos_client(app_uri): Returns a cached QcosClient for the given app_uri. Warning: The internal cache is not thread-safe.
    • create_qcos_client(app_uri): Manually creates a new QcosClient. This method automatically resolves the application's valid authentication and regional product information.
    # Assuming 'client' is an initialized AccountClient
    app_uri = 'your_app_uri'
    qcos_client = client.get_qcos_client(app_uri)
  3. Initialize the SandboxClient

    master

    The SandboxClient is the primary entry point for interacting with the Qiniu Sandbox API. It supports multiple authentication methods including API Keys, Access Tokens, and Qiniu AK/SK credentials.

    Authentication Options:

    • API Key: Pass api_key. It can also be retrieved from environment variables QINIU_SANDBOX_API_KEY, QINIU_API_KEY, or E2B_API_KEY.
    • Access Token: Pass access_token. It can also be retrieved from QINIU_SANDBOX_ACCESS_TOKEN.
    • Qiniu AK/SK: Pass access_key and secret_key. These can be retrieved from QINIU_SANDBOX_ACCESS_KEY and QINIU_SANDBOX_SECRET_KEY.

    Other Configuration:

    • endpoint or api_url: The base URL for the Sandbox API.
    • timeout: Request timeout in seconds (default: 30).
    • max_retries: Number of retries for retryable errors (default: 5, or from SANDBOX_RETRY_MAX).
    from qiniu.services.sandbox.client import SandboxClient
    
    # Using API Key
    client = SandboxClient(api_key='your_api_key')
    
    # Using Qiniu AK/SK
    client = SandboxClient(access_key='your_ak', secret_key='your_sk')
    
    # Using Access Token
    client = SandboxClient(access_token='your_token')
  4. Manage Injection Rules

    master

    Injection rules allow you to define how data or configurations are injected into sandboxes. These operations typically require Qiniu AK/SK authentication.

    • list_injection_rules(): Lists all available injection rules.
    • create_injection_rule(**opts): Creates a new rule. Pass the rule configuration via the injection key.
    • get_injection_rule(rule_id): Retrieves a specific rule.
    • update_injection_rule(rule_id, **opts): Updates an existing rule.
    • delete_injection_rule(rule_id): Deletes a rule.
    # Create an injection rule
    client.create_injection_rule(injection={'type': 'id', 'id': 'some_id'})
    
    # List rules
    rules = client.list_injection_rules()
  5. Monitor Sandbox Metrics and Logs

    master

    Retrieve operational data from running sandboxes:

    • get_metrics(sandbox_id, **opts): Gets metrics for a single sandbox.
    • get_sandboxes_metrics(sandbox_ids): Gets metrics for multiple sandboxes. sandbox_ids can be a list of strings, a list of dicts containing sandboxId, or a dict with a sandbox_ids key.
    • get_logs(sandbox_id, **opts): Retrieves logs for a specific sandbox.
    # Get logs
    logs = client.get_logs('sandbox_id_123')
    
    # Get metrics for multiple sandboxes
    metrics = client.get_sandboxes_metrics(['id1', 'id2'])
  6. Retrieve account and regional information

    master

    The AccountClient provides methods to inspect your account status and regional availability:

    • get_account_info(): Returns a tuple (result, ResponseInfo) containing the information of the account associated with the provided auth.
    • get_region_products(region): Returns product information for a specific region (e.g., region='nq').
    • get_app_region_products(app_uri): Returns the product information for the region where the specified application is located.
  7. Manage Sandbox Templates

    master

    Templates define the environment for sandboxes. You can manage them using the following methods:

    • create(template_v2=None, **opts): Creates a new template (v2 or v3).
    • list_templates(**opts): Lists available templates.
    • get_template(template_id, **opts): Gets template details.
    • update_template(template_id, **opts): Updates an existing template.
    • delete_template(template_id): Deletes a template.
    • rebuild_template(template_id, **opts): Rebuilds a template (requires access_token).
    • start_template_build(template_id, build_id, **opts): Starts a specific build process.
    • wait_for_build(template_id, build_id, interval=1, timeout=60): A helper that polls the build status and blocks until the build is ready or fails with a TemplateBuildError.
    # Create a template
    template = client.create(name='my-custom-env', config={...})
    
    # Wait for a build to complete
    client.wait_for_build(template_id='tpl_123', build_id='bld_456')