Install the Qiniu Python SDK
masterInstall the SDK using pip to include it in your Python environment.
$ pip install qiniurepository·master·Indexed 19 days ago
https://github.com/qiniu/python-sdkA 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.
Install the SDK using pip to include it in your Python environment.
$ pip install qiniuIf you encounter the error ImportError: No module named requests.auth, you need to install the requests library manually:
$ pip install requestsTo upload data, follow these steps:
qiniu.Auth with your access_key and secret_key.q.upload_token(bucket_name).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 infoYou can use the list_apps.py example script to list your Qiniu applications by providing your Access Key (AK) and Secret Key (SK) as command-line arguments.
$ python list_apps.py <YOUR_AK> <YOUR_SK>Ensure your Python version matches the requirements for the SDK version you are using:
| Qiniu SDK version | Supported Python versions |
|---|---|
| 7.x | 2.7, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9 |
| 6.x | 2.7 |
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)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. It can also be retrieved from environment variables QINIU_SANDBOX_API_KEY, QINIU_API_KEY, or E2B_API_KEY.access_token. It can also be retrieved from QINIU_SANDBOX_ACCESS_TOKEN.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')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()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'])get_valid_app_auth(app_uri) method retrieves an active QiniuMacAuth object for a specific application. It iterates through the application's keys and returns the first one with a state of 'enabled'. If no enabled key is found, it returns None.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.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')