使用Dify工作流做了给总结视频内容的工具。
因为阿里的接口返回的json文件,Dify的HTTP请求之后自动将文件存储到了Dify。HTTP请求文件节点返回Body为空,文件信息保存在files里
{
"status_code": 200,
"body": "",
"headers": {
"server": "AliyunOSS",
"date": "Sun, 08 Jun 2025 03:45:09 GMT",
"content-type": "application/json",
"transfer-encoding": "chunked",
"connection": "keep-alive",
"vary": "Accept-Encoding",
"x-oss-request-id": "68450745F4EF8133368598B3",
"last-modified": "Sun, 08 Jun 2025 03:44:58 GMT",
"x-oss-object-type": "Normal",
"x-oss-hash-crc64ecma": "10081479749614708832",
"x-oss-storage-class": "Standard",
"x-oss-expiration": "expiry-date=\"Mon, 07 Jul 2025 00:00:00 GMT\", rule-id=\"baxxxx3-bd97-45bf-9a98-548f4xxx845\"",
"x-oss-ec": "0048-00000113",
"content-disposition": "attachment",
"x-oss-force-download": "true",
"content-md5": "wNudyHxxxxx+nZBh3A==",
"x-oss-server-time": "13",
"content-encoding": "gzip"
},
"files": [
{
"dify_model_identity": "__dify__file__",
"id": null,
"tenant_id": "xxxx-8517-4a26-b1b4-f75250811cfa",
"type": "custom",
"transfer_method": "tool_file",
"remote_url": null,
"related_id": "c81bf4b1-a3de-40f7-9fe8-xxxxx",
"filename": "187a22c0806b4a6faxxxxbb908.json",
"extension": ".json",
"mime_type": "application/json",
"size": 41161,
"url": "https://upload.dify.ai/files/tools/xxxxx.json?timestamp=1749354309&nonce=xxxx&sign=xxxx="
}
]
}
代码执行板块代码如下
import json
import requests
def main(arg1) -> dict:
if not arg1:
raise ValueError("arg1 数组为空")
first_item = arg1[0]
if "filename" in first_item and "url" in first_item:
filename = first_item["filename"]
file_url = first_item["url"]
file_content = read_remote_json(file_url)
return {
"filename": filename,
"content": file_content["transcripts"][0]["text"],
}
else:
raise KeyError("字典中缺少 'filename' 或 'url' 键")
def read_remote_json(url):
"""
从远程 URL 读取 JSON 文件并返回其内容。
"""
response = requests.get(url)
# 检查请求是否成功
if response.status_code == 200:
try:
return response.json() # 返回解析后的 JSON 数据
except json.JSONDecodeError:
raise ValueError("远程文件不是有效的 JSON 格式")
else:
raise Exception(f"无法读取远程文件,状态码:{response.status_code}")