ATH-MaaS/Pixelle-Video · error · RuntimeError

DASHSCOPE_API_KEY 未设置,无法使用图片上传服务

Error message

DASHSCOPE_API_KEY 未设置,无法使用图片上传服务

What it means

get_upload_policy fetches temporary upload credentials from DashScope and raises RuntimeError when self.api_key is falsy. The DashScope upload service cannot be called without DASHSCOPE_API_KEY, so the library fails fast before making a doomed network request.

Source

Thrown at pixelle_video/services/api_services/image_processor.py:275

            if all(is_black_pixel(pixels[width - 1 - x, y]) for y in range(height)):
                return True
        
        return False

    # ===== 图片上传功能 =====
    
    def get_upload_policy(self):
        """
        获取文件上传凭证
        
        Returns:
            policy_data: 包含上传所需凭证的字典
            
        Raises:
            Exception: 获取上传凭证失败时
        """
        if not self.api_key:
            raise RuntimeError("DASHSCOPE_API_KEY 未设置,无法使用图片上传服务")
        
        headers = {
            "Authorization": f"Bearer {self.api_key}",
            "Content-Type": "application/json"
        }
        params = {
            "action": "getPolicy",
            "model": self.model_name
        }
        
        response = requests.get(
            self.UPLOAD_API_URL,
            headers=headers,
            params=params,
            proxies=self._proxies(),
        )
        if response.status_code != 200:
            raise Exception(f"Failed to get upload policy: {response.text}")

View on GitHub (pinned to 848b054e4f)

Solutions

  1. Export DASHSCOPE_API_KEY with a valid DashScope key before running.
  2. Pass api_key explicitly when constructing ImageProcessor.
  3. Check the key is loaded from the right .env file and the process actually sees it (print/os.environ check).
  4. In containers/CI, add the env var to the deployment secret configuration.

Example fix

// before
processor = ImageProcessor()  # no key

// after
import os
key = os.environ.get("DASHSCOPE_API_KEY")
assert key, "DASHSCOPE_API_KEY must be set"
processor = ImageProcessor(api_key=key)
Defensive patterns

Strategy: validation

Validate before calling

import os
api_key = os.environ.get("DASHSCOPE_API_KEY")
if not api_key:
    raise RuntimeError("Set DASHSCOPE_API_KEY before uploading images")

Type guard

def has_api_key(processor) -> bool:
    return bool(getattr(processor, "api_key", None))

Try / catch

try:
    url = processor.upload(file_path)
except RuntimeError as e:
    if "DASHSCOPE_API_KEY" in str(e):
        raise ConfigError("Export DASHSCOPE_API_KEY in this environment") from e
    raise

Prevention

When it happens

Trigger: Calling upload (which invokes get_upload_policy) on an ImageProcessor constructed without an API key, or with DASHSCOPE_API_KEY unset/empty in the environment.

Common situations: Environment variable not exported in the deployment environment (works locally, fails in CI/container), key passed under a different variable name, ImageProcessor instantiated without the api_key argument.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of ATH-MaaS/Pixelle-Video@848b054e4f (2026-08-30). Data as JSON: /api/errors/8042c26770435000. Report an issue: GitHub.