Upload a local file to tmpfiles.org using Python standard library only, then return a direct download link in strict JSON.
当用户想要上传一个已存在于 OpenClaw 工作区或容器中的本地文件并获取临时公共下载链接时,使用此技能。
此技能适用于:
此技能不适用于:
在上传之前,请始终检查以下内容:
切勿上传:
如果文件看起来敏感,请在继续之前简要警告用户。
你需要:
如果没有可用的本地文件路径,请询问用户或说明文件必须已存在于工作区/容器中。
始终返回严格的 JSON 格式,不包含 markdown 和任何额外注释。
成功格式:
json
{
success: true,
file_path: /root/.openclaw/workspace-default/report.pdf,
file_name: report.pdf,
download_url: https://tmpfiles.org/xxxxxxxx/report.pdf,
note: 已生成临时公共链接。
}
失败格式:
json
{
success: false,
file_path: /root/.openclaw/workspace-default/report.pdf,
error: 文件未找到
}
请严格按照以下步骤执行:
仅使用 python3 和标准库模块。
推荐方法:
使用一次性 Python 命令或 heredoc 脚本。
bash
python3 <
import json
import uuid
import mimetypes
import urllib.request
file_path = /绝对/路径/到/文件.ext
if not os.path.isfile(file_path):
print(json.dumps({
success: False,
filepath: filepath,
error: 文件未找到
}))
raise SystemExit(0)
filename = os.path.basename(filepath)
mimetype = mimetypes.guesstype(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(
fContent-Disposition: form-data; name=file; filename={file_name}\r\n.encode()
)
body.append(fContent-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: fmultipart/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):
downloadurl = dataobj.get(url)
if download_url:
print(json.dumps({
success: True,
filepath: filepath,
filename: filename,
downloadurl: downloadurl,
note: 已生成临时公共链接。
}))
else:
print(json.dumps({
success: False,
filepath: filepath,
error: 上传响应未包含下载 URL,
raw_response: parsed
}))
except Exception as e:
print(json.dumps({
success: False,
filepath: filepath,
error: str(e)
}))
PY
在以下情况下拒绝或警告:
在这些情况下,建议使用私有存储,例如 S3 或 Supabase。
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 tmpfiles-upload-stdlib-1776015181 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 tmpfiles-upload-stdlib-1776015181 技能
skillhub install tmpfiles-upload-stdlib-1776015181
文件大小: 2.91 KB | 发布时间: 2026-4-13 12:22