← 返回
未分类 中文

Tmpfiles Upload (Stdlib)

Upload a local file to tmpfiles.org using Python standard library only, then return a direct download link in strict JSON.
仅使用 Python 标准库将本地文件上传至 tmpfiles.org,随后返回严格 JSON 格式的直接下载链接。
donigwapo donigwapo 来源
未分类 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 399
下载
💾 0
安装
1
版本
#files#latest#python#tmpfiles#upload

概述

Tmpfiles Upload (Standard Library)

Use this skill when the user wants to upload a local file that already exists in the OpenClaw workspace or container and receive a temporary public download link.

This skill is for:

  • uploading a file from a known local path
  • returning a temporary download link
  • producing machine-friendly output for workflows such as n8n

This skill is not for:

  • downloading files from remote URLs first
  • handling private or sensitive documents
  • long-term or secure file storage
  • inventing file paths that do not exist

Safety and scope

Before uploading, always check the following:

  1. The file path is explicitly provided by the user or clearly available in context.
  2. The file exists locally.
  3. The file is not obviously sensitive, unless the user clearly asked to upload it anyway.
  4. The user understands the link is temporary and public.

Never upload:

  • secrets, credentials, tokens, key files
  • private IDs, tax records, contracts, or financial files unless the user explicitly insists
  • arbitrary system files outside the task scope

If the file appears sensitive, warn the user briefly before proceeding.

Required input

You need:

  • a local file path, such as /root/.openclaw/workspace-default/report.pdf

If no local file path is available, ask for it or explain that the file must already exist locally in the workspace/container.

Output format

Always return strict JSON only with no markdown and no extra commentary.

Success format:

{
  "success": true,
  "file_path": "/root/.openclaw/workspace-default/report.pdf",
  "file_name": "report.pdf",
  "download_url": "https://tmpfiles.org/xxxxxxxx/report.pdf",
  "note": "Temporary public link generated."
}

Failure format:

{
  "success": false,
  "file_path": "/root/.openclaw/workspace-default/report.pdf",
  "error": "File not found"
}

Procedure

Follow these steps exactly:

  1. Confirm the local file path.
  2. Check whether the file exists.
  3. Upload the file to tmpfiles.org using Python standard library only.
  4. Extract the returned URL.
  5. Return strict JSON only.

Python upload method

Use python3 and standard library modules only.

Preferred approach:

  • os for file checks
  • mimetypes for content type guess if needed
  • urllib.request and uuid for multipart upload
  • json for parsing response

Use a one-shot Python command or heredoc script.

Reference implementation

python3 <<'PY'
import os
import json
import uuid
import mimetypes
import urllib.request

file_path = "/absolute/path/to/file.ext"

if not os.path.isfile(file_path):
    print(json.dumps({
        "success": False,
        "file_path": file_path,
        "error": "File not found"
    }))
    raise SystemExit(0)

file_name = os.path.basename(file_path)
mime_type = mimetypes.guess_type(file_name)[0] or "application/octet-stream"
boundary = "----WebKitFormBoundary" + uuid.uuid4().hex

with open(file_path, "rb") as f:
    file_bytes = f.read()

body = []
body.append(f"--{boundary}\r\n".encode())
body.append(
    f'Content-Disposition: form-data; name="file"; filename="{file_name}"\r\n'.encode()
)
body.append(f"Content-Type: {mime_type}\r\n\r\n".encode())
body.append(file_bytes)
body.append(f"\r\n--{boundary}--\r\n".encode())

data = b"".join(body)

req = urllib.request.Request(
    "https://tmpfiles.org/api/v1/upload",
    data=data,
    headers={
        "Content-Type": f"multipart/form-data; boundary={boundary}",
        "Accept": "application/json"
    },
    method="POST"
)

try:
    with urllib.request.urlopen(req, timeout=60) as resp:
        raw = resp.read().decode("utf-8", errors="replace")
    parsed = json.loads(raw)

    download_url = None
    if isinstance(parsed, dict):
        data_obj = parsed.get("data", {})
        if isinstance(data_obj, dict):
            download_url = data_obj.get("url")

    if download_url:
        print(json.dumps({
            "success": True,
            "file_path": file_path,
            "file_name": file_name,
            "download_url": download_url,
            "note": "Temporary public link generated."
        }))
    else:
        print(json.dumps({
            "success": False,
            "file_path": file_path,
            "error": "Upload response did not include a download URL",
            "raw_response": parsed
        }))
except Exception as e:
    print(json.dumps({
        "success": False,
        "file_path": file_path,
        "error": str(e)
    }))
PY

Execution rules

  • Replace /absolute/path/to/file.ext with the real local file path.
  • Do not use requests.
  • Do not return prose before or after the JSON.
  • Prefer a single final JSON object.
  • If upload fails, return the failure JSON.
  • If the file is missing, do not attempt upload.

When to decline

Decline or warn when:

  • the file path is missing
  • the file does not exist
  • the file is likely sensitive and public upload would be risky
  • the user asks for secure/private hosting instead of a public temporary link

In those cases, suggest using private storage such as S3 or Supabase instead.

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-31 18:23 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

dev-programming

Mcporter

steipete
使用 mcporter CLI 直接列出、配置、认证及调用 MCP 服务器/工具(支持 HTTP 或 stdio),涵盖临时服务器、配置编辑及 CLI/类型生成功能。
★ 196 📥 67,898
dev-programming

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 75 📥 182,331
ai-agent

Delivery Preference Resolver

donigwapo
在结构化JSON输出中确定用户意图、目的地、已知/缺失字段及后续需求,以解析配送偏好。
★ 0 📥 434