Upload media (OSS)

Vmake Labs tools take a publicly readable media URL as input. The recommended approach is to exchange temporary Aliyun OSS credentials from the token policy API, upload with the official OSS SDK, and use the resulting URL as the tool input.

Step 1: Get temporary credentials

FieldValue
MethodGET
HostRegion endpoint from algorithm.regions[<region>] in /skill/config.json
Path/ai/token_policy
Querytype=<token_policy_type>, default mtai. Overridden by token_policy_types.upload when present
AuthSDK-HMAC-SHA256. See Signing

Example response (key parts only):

{
  "data": {
    "mtai": {
      "upload": {
        "order": ["oss"],
        "oss": {
          "url": "https://<bucket>.oss-<region>.aliyuncs.com",
          "bucket": "<bucket>",
          "key": "path/to/<uuid>",
          "region": "<region>",
          "data": "https://<bucket>.oss-<region>.aliyuncs.com/path/to/<uuid>",
          "credentials": {
            "access_key": "STS.xxx",
            "secret_key": "xxx",
            "session_token": "xxx"
          },
          "ttl": 1800
        }
      },
      "api": {
        "order": ["mtai"],
        "mtai": {
          "url": "https://<strategy-host>",
          "push_path": "ai/task_push",
          "status_query": { "path": "ai/task_query", "durations": "500,1000,2000,2000,2000" },
          "sync_timeout": 10
        }
      }
    }
  }
}

Read it in this order:

  1. data.mtai.upload.order[0] gives the cloud name (currently oss).
  2. data.mtai.upload[cloud] is the storage policy used for the OSS upload.
  3. If the config sets both token_policy_types.upload and token_policy_types.ai, request the upload and AI policies with their respective types.
  4. data.mtai.api.order[0] gives the tool cloud, and data.mtai.api[cloud] is the AI policy used to submit and poll tasks.

You can reuse the AI policy within its ttl. Fetch a new storage policy before every upload.

cURL example

Continuing from the config and region_name variables in Get config, get the AI policy and save it to ai.

region=$(jq -er --arg name "$region_name" '.response.algorithm.regions[$name]' <<< "$config")
[[ "$region" == https://* ]] || region="https://$region"
policy_type=$(jq -r '(.response.algorithm | .token_policy_types.ai // .token_policy_type // "mtai") | @uri' <<< "$config")
policy=$(api GET "${region%/}/ai/token_policy?type=$policy_type")
ai=$(jq -ec '.data.mtai.api | .[.order[0]]' <<< "$policy")
echo "$ai" | jq '{url, push_path, status_query, sync_timeout}'

The example skips the OSS upload and uses an image you are authorized to use that is readable at a public URL. To use a local image, upload it as in step 2 and use policy.data instead.

media_url='Replace with your image HTTPS URL'

Step 2: Upload with temporary OSS credentials

Key points:

  • Normalize endpoint to https://<host> and drop the path.
  • For OSS signing, parse region from the endpoint host first. Use policy.region when the host does not contain one.
  • Use the STS triple in credentials as static credentials.
  • Take bucket and key for PutObjectRequest directly from policy.
  • After a successful upload, the tool input URL is policy.data.

Python example (Aliyun OSS SDK v2):

import alibabacloud_oss_v2 as oss
from urllib.parse import urlparse

def upload_to_oss(policy, local_path):
    creds = policy["credentials"]
    cfg = oss.config.load_default()
    cfg.credentials_provider = oss.credentials.StaticCredentialsProvider(
        creds["access_key"], creds["secret_key"], creds.get("session_token"),
    )
    parsed = urlparse(policy["url"] if "://" in policy["url"] else "https://" + policy["url"])
    cfg.endpoint = f"https://{parsed.netloc}"

    host = "." + parsed.hostname.lower()
    if ".oss-" in host and "aliyuncs.com" in host:
        tail = host.split(".oss-", 1)[1]
        cfg.region = tail.split(".aliyuncs.com", 1)[0].removesuffix("-internal")
    else:
        cfg.region = policy["region"]

    client = oss.Client(cfg)
    client.put_object_from_file(
        oss.PutObjectRequest(bucket=policy["bucket"], key=policy["key"]),
        local_path,
    )
    data = policy["data"]
    return data["url"] if isinstance(data, dict) else data

Remote media

If your media already has a public URL:

  • If the URL is stable and has no strict access control, use it directly as the tool input.
  • If the URL requires authentication, expires quickly, or is on a private network, download it to disk or memory first and upload it through the OSS flow above so the tool can read it.

Errors

SymptomAction
Invalid signing regionThe OSS region does not match the bucket's actual region. Parse it again with the rules above
403 SignatureDoesNotMatchLocal clock skew or an expired STS token. Call the token policy API again
Upload succeeds but the tool cannot read the fileCheck that policy.data is the tool input. Some buckets restrict referer or ACL

Next steps