插件图片上传后使用File类, 调用blob报错 Request URL is missing an 'http://' or 'https://' protocol. #139

Closed
opened 2026-02-16 11:19:51 -05:00 by yindo · 6 comments
Owner

Originally created by @jiadong696 on GitHub (Apr 8, 2025).

Image

Image

接受到的img1的数据是
[File(dify_model_identity='__dify__file__', url='/files/0c0c6064-1c0d-4097-a52a-ca30a849612b/file-preview?timestamp=1744164267&nonce=88faa1b997aacabec4b609a75b7f137c&sign=ZIp5FSF_Pq4buu_4OybcHpfzkiDnqTwGCpzgduoAlzE=', mime_type='image/jpeg', filename='510824197305072053.jpg', extension='.jpg', size=29529, type=<FileType.IMAGE: 'image'>), File(dify_model_identity='__dify__file__', url='/files/c81ba2c3-327a-427e-837e-2c8af75f2d6d/file-preview?timestamp=1744164267&nonce=7544f17ab3780280d2a057626875825f&sign=zhh5ile55W_xqZv6nHwqpM5ldWEujrYIUrioZ4D5qDU=', mime_type='image/jpeg', filename='510824199402152556.jpg', extension='.jpg', size=62689, type=<FileType.IMAGE: 'image'>), File(dify_model_identity='__dify__file__', url='/files/a5af6b6c-fcb7-445a-aa76-473da68ac601/file-preview?timestamp=1744164267&nonce=6d518da07d0b04cb40d8a55b7413e8cc&sign=Wgz1tLlMezfZqouZYqztKXTtqPSGtPuOxtM8h40B9Vs=', mime_type='image/jpeg', filename='ceshi.jpg', extension='.jpg', size=73207, type=<FileType.IMAGE: 'image'>)]

我调用 img.blob 提示httpx.UnsupportedProtocol: Request URL is missing an 'http://' or 'https://' protocol错误
请问是插件版本低了还是什么原因导致的? 我上传的本地文件, 这个如何修改, 谢谢指导

Originally created by @jiadong696 on GitHub (Apr 8, 2025). ![Image](https://github.com/user-attachments/assets/99654e1e-2992-45ce-80b8-36842bf6c495) ![Image](https://github.com/user-attachments/assets/4daa1326-b869-4c90-ae8f-f50ae4815b43) 接受到的img1的数据是 ` [File(dify_model_identity='__dify__file__', url='/files/0c0c6064-1c0d-4097-a52a-ca30a849612b/file-preview?timestamp=1744164267&nonce=88faa1b997aacabec4b609a75b7f137c&sign=ZIp5FSF_Pq4buu_4OybcHpfzkiDnqTwGCpzgduoAlzE=', mime_type='image/jpeg', filename='510824197305072053.jpg', extension='.jpg', size=29529, type=<FileType.IMAGE: 'image'>), File(dify_model_identity='__dify__file__', url='/files/c81ba2c3-327a-427e-837e-2c8af75f2d6d/file-preview?timestamp=1744164267&nonce=7544f17ab3780280d2a057626875825f&sign=zhh5ile55W_xqZv6nHwqpM5ldWEujrYIUrioZ4D5qDU=', mime_type='image/jpeg', filename='510824199402152556.jpg', extension='.jpg', size=62689, type=<FileType.IMAGE: 'image'>), File(dify_model_identity='__dify__file__', url='/files/a5af6b6c-fcb7-445a-aa76-473da68ac601/file-preview?timestamp=1744164267&nonce=6d518da07d0b04cb40d8a55b7413e8cc&sign=Wgz1tLlMezfZqouZYqztKXTtqPSGtPuOxtM8h40B9Vs=', mime_type='image/jpeg', filename='ceshi.jpg', extension='.jpg', size=73207, type=<FileType.IMAGE: 'image'>)] ` 我调用 img.blob 提示httpx.UnsupportedProtocol: Request URL is missing an 'http://' or 'https://' protocol错误 请问是插件版本低了还是什么原因导致的? 我上传的本地文件, 这个如何修改, 谢谢指导
yindo added the bug label 2026-02-16 11:19:51 -05:00
yindo closed this issue 2026-02-16 11:19:51 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Apr 8, 2025):

The error you're encountering is due to the URLs in the File objects missing the protocol (http:// or https://). The blob property method in the File class attempts to fetch the file content via an HTTP GET request, which requires a complete URL with a protocol.

To resolve this issue, you need to ensure that the URLs include the protocol. You can prepend the base URL to the url attribute of each File object. The dify-docs repository provides a configuration setting for specifying the base URL through the secret DIFY_BASE_URL, which defaults to https://api.dify.ai/v1 [1].

Here's a possible solution:

  1. Retrieve the base URL from your configuration or use the default https://api.dify.ai/v1.
  2. Prepend this base URL to the url attribute of each File object before accessing the blob property.

For example:

base_url = "https://api.dify.ai/v1"  # or retrieve from your configuration
for file in img1_data:
    if not file.url.startswith(('http://', 'https://')):
        file.url = base_url + file.url
    blob_data = file.blob  # Now this should work without errors

This should resolve the error by ensuring that all URLs are complete and valid for HTTP requests.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@dosubot[bot] commented on GitHub (Apr 8, 2025): <!-- Answer --> The error you're encountering is due to the URLs in the `File` objects missing the protocol (`http://` or `https://`). The `blob` property method in the `File` class attempts to fetch the file content via an HTTP GET request, which requires a complete URL with a protocol. To resolve this issue, you need to ensure that the URLs include the protocol. You can prepend the base URL to the `url` attribute of each `File` object. The dify-docs repository provides a configuration setting for specifying the base URL through the secret `DIFY_BASE_URL`, which defaults to `https://api.dify.ai/v1` <sup>[[1]](https://github.com/langgenius/dify-docs/blob/main/en/learn-more/use-cases/dify-schedule.md)</sup>. Here's a possible solution: 1. Retrieve the base URL from your configuration or use the default `https://api.dify.ai/v1`. 2. Prepend this base URL to the `url` attribute of each `File` object before accessing the `blob` property. For example: ```python base_url = "https://api.dify.ai/v1" # or retrieve from your configuration for file in img1_data: if not file.url.startswith(('http://', 'https://')): file.url = base_url + file.url blob_data = file.blob # Now this should work without errors ``` This should resolve the error by ensuring that all URLs are complete and valid for HTTP requests. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/61a4dfcb-7003-4cf3-9bf6-7239c6198a95?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/61a4dfcb-7003-4cf3-9bf6-7239c6198a95?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/61a4dfcb-7003-4cf3-9bf6-7239c6198a95?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/61a4dfcb-7003-4cf3-9bf6-7239c6198a95?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/61a4dfcb-7003-4cf3-9bf6-7239c6198a95?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/61a4dfcb-7003-4cf3-9bf6-7239c6198a95?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/61a4dfcb-7003-4cf3-9bf6-7239c6198a95?feedback_type=other)</sup>
Author
Owner

@jiadong696 commented on GitHub (Apr 8, 2025):

我获取dify_plugin模块中的config目录下的config文件里面DifyPluginEnv类下REMOTE_INSTALL_HOST, 对这个进行调用,提示我找不到, 报错 raise AttributeError(item)
AttributeError: REMOTE_INSTALL_HOST
但是我在.env配置中设置了对应的REMOTE_INSTALL_HOST, 但是他无法获取是怎么回事呢?

@jiadong696 commented on GitHub (Apr 8, 2025): 我获取dify_plugin模块中的config目录下的config文件里面DifyPluginEnv类下REMOTE_INSTALL_HOST, 对这个进行调用,提示我找不到, 报错 raise AttributeError(item) AttributeError: REMOTE_INSTALL_HOST 但是我在.env配置中设置了对应的REMOTE_INSTALL_HOST, 但是他无法获取是怎么回事呢?
Author
Owner

@jiadong696 commented on GitHub (Apr 8, 2025):

我需要写死当前http的dify路由吗 我能从那个配置获取图片的前缀URL么?

@jiadong696 commented on GitHub (Apr 8, 2025): 我需要写死当前http的dify路由吗 我能从那个配置获取图片的前缀URL么?
Author
Owner

@dosubot[bot] commented on GitHub (Apr 8, 2025):

对于第一个问题,AttributeError: REMOTE_INSTALL_HOST 表示在 DifyPluginEnv 类中无法找到 REMOTE_INSTALL_HOST。确保在代码中正确地访问环境变量。通常,环境变量可以通过 os.environ 来访问。例如:

import os

remote_install_host = os.environ.get('REMOTE_INSTALL_HOST')
if remote_install_host is None:
    raise AttributeError("REMOTE_INSTALL_HOST not found in environment variables")

确保 .env 文件中的变量已被正确加载到环境中。可以使用 python-dotenv 库来加载 .env 文件:

from dotenv import load_dotenv
import os

load_dotenv()  # 加载 .env 文件

remote_install_host = os.environ.get('REMOTE_INSTALL_HOST')
if remote_install_host is None:
    raise AttributeError("REMOTE_INSTALL_HOST not found in environment variables")

对于第二个问题,关于获取图片的前缀 URL,dify-docs 仓库中没有直接提供构建或检索文件资源基础 URL 的方法或工具。你可以在配置中定义一个基础 URL,然后在代码中使用它来构建完整的 URL。例如:

base_url = "https://api.dify.ai/v1"  # 从配置中获取或定义
file_url = base_url + "/path/to/your/file"

确保在代码中使用正确的基础 URL 来构建完整的文件访问路径。

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@dosubot[bot] commented on GitHub (Apr 8, 2025): <!-- Answer --> 对于第一个问题,`AttributeError: REMOTE_INSTALL_HOST` 表示在 `DifyPluginEnv` 类中无法找到 `REMOTE_INSTALL_HOST`。确保在代码中正确地访问环境变量。通常,环境变量可以通过 `os.environ` 来访问。例如: ```python import os remote_install_host = os.environ.get('REMOTE_INSTALL_HOST') if remote_install_host is None: raise AttributeError("REMOTE_INSTALL_HOST not found in environment variables") ``` 确保 `.env` 文件中的变量已被正确加载到环境中。可以使用 `python-dotenv` 库来加载 `.env` 文件: ```python from dotenv import load_dotenv import os load_dotenv() # 加载 .env 文件 remote_install_host = os.environ.get('REMOTE_INSTALL_HOST') if remote_install_host is None: raise AttributeError("REMOTE_INSTALL_HOST not found in environment variables") ``` 对于第二个问题,关于获取图片的前缀 URL,`dify-docs` 仓库中没有直接提供构建或检索文件资源基础 URL 的方法或工具。你可以在配置中定义一个基础 URL,然后在代码中使用它来构建完整的 URL。例如: ```python base_url = "https://api.dify.ai/v1" # 从配置中获取或定义 file_url = base_url + "/path/to/your/file" ``` 确保在代码中使用正确的基础 URL 来构建完整的文件访问路径。 <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/6a45f983-e270-4f61-85d9-1c191deb7f8c?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/6a45f983-e270-4f61-85d9-1c191deb7f8c?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/6a45f983-e270-4f61-85d9-1c191deb7f8c?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/6a45f983-e270-4f61-85d9-1c191deb7f8c?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/6a45f983-e270-4f61-85d9-1c191deb7f8c?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/6a45f983-e270-4f61-85d9-1c191deb7f8c?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/6a45f983-e270-4f61-85d9-1c191deb7f8c?feedback_type=other)</sup>
Author
Owner

@jiadong696 commented on GitHub (Apr 9, 2025):

生成.difypkg插件文件后 .env文件不会包含在插件中, 所以正式上线是无法获取通用地址的, 这个怎么解决呢 不可能我测试服生成一个difypkg, 生产环境生成一个, 正式服生成一个, 所以这个还有其他方式获取吗

@jiadong696 commented on GitHub (Apr 9, 2025): 生成.difypkg插件文件后 .env文件不会包含在插件中, 所以正式上线是无法获取通用地址的, 这个怎么解决呢 不可能我测试服生成一个difypkg, 生产环境生成一个, 正式服生成一个, 所以这个还有其他方式获取吗
Author
Owner

@dosubot[bot] commented on GitHub (Apr 9, 2025):

在生成 .difypkg 插件文件后,.env 文件不会包含在插件中,因此在正式环境中无法直接获取通用地址。为了解决这个问题,可以考虑以下方法:

  1. 使用环境变量管理服务:在生产环境中,使用环境变量管理服务(如 AWS Secrets Manager、Azure Key Vault 或 HashiCorp Vault)来存储和管理敏感信息和配置。这些服务可以安全地存储环境变量,并在应用启动时动态加载。

  2. 配置文件:在生产环境中,使用配置文件来存储必要的配置信息。确保这些文件在部署时被正确加载,并且不包含在插件包中。可以使用 python-dotenv 或类似工具在应用启动时加载这些配置。

  3. 环境变量注入:在部署过程中,通过 CI/CD 管道或部署脚本将必要的环境变量注入到运行环境中。这种方法可以确保在不同环境中使用相同的插件包,而不需要在每个环境中生成不同的包。

  4. 动态配置服务:实现一个配置服务,应用在启动时从该服务获取必要的配置信息。这种方法可以使配置管理更加灵活,并且可以在不重新部署应用的情况下更新配置。

通过这些方法,可以在不同环境中使用相同的插件包,同时确保能够获取到正确的配置信息。

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@dosubot[bot] commented on GitHub (Apr 9, 2025): <!-- Answer --> 在生成 `.difypkg` 插件文件后,`.env` 文件不会包含在插件中,因此在正式环境中无法直接获取通用地址。为了解决这个问题,可以考虑以下方法: 1. **使用环境变量管理服务**:在生产环境中,使用环境变量管理服务(如 AWS Secrets Manager、Azure Key Vault 或 HashiCorp Vault)来存储和管理敏感信息和配置。这些服务可以安全地存储环境变量,并在应用启动时动态加载。 2. **配置文件**:在生产环境中,使用配置文件来存储必要的配置信息。确保这些文件在部署时被正确加载,并且不包含在插件包中。可以使用 `python-dotenv` 或类似工具在应用启动时加载这些配置。 3. **环境变量注入**:在部署过程中,通过 CI/CD 管道或部署脚本将必要的环境变量注入到运行环境中。这种方法可以确保在不同环境中使用相同的插件包,而不需要在每个环境中生成不同的包。 4. **动态配置服务**:实现一个配置服务,应用在启动时从该服务获取必要的配置信息。这种方法可以使配置管理更加灵活,并且可以在不重新部署应用的情况下更新配置。 通过这些方法,可以在不同环境中使用相同的插件包,同时确保能够获取到正确的配置信息。 <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/5209a618-fb89-4b46-ba99-db72b4757106?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/5209a618-fb89-4b46-ba99-db72b4757106?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/5209a618-fb89-4b46-ba99-db72b4757106?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/5209a618-fb89-4b46-ba99-db72b4757106?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/5209a618-fb89-4b46-ba99-db72b4757106?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/5209a618-fb89-4b46-ba99-db72b4757106?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/5209a618-fb89-4b46-ba99-db72b4757106?feedback_type=other)</sup>
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-docs-archived#139