You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Cloudflare Python库上传图片时遇Error 5455错误求助

解决Cloudflare Images上传时415内容类型错误

核心问题

你的代码未正确传递文件元信息(文件名/Content-Type),导致Cloudflare无法识别图片的MIME类型,即便文件本身是合法的JPEG格式。

可行解决方案

1. 传递包含文件名的文件元组

Cloudflare Python库底层基于requests,上传文件时需以(文件名, 文件对象, [Content-Type])的元组格式传递file参数,让服务端能正确识别文件类型:

import io
from pathlib import Path
from cloudflare import Cloudflare

if __name__ == '__main__':
    secrets = loadSecrets()
    image = Path("E:/").joinpath("test", "test_pict.jpg")
    cf = Cloudflare(api_token = secrets["image"]["apitoken"])
    with open(image, "rb") as imgfile:
        # 传递文件名+文件对象,让库自动推断Content-Type
        res = cf.images.v1.create(
            account_id=secrets["accid"],
            file=(image.name, imgfile)
        )

若自动推断仍有问题,可手动指定Content-Type:

res = cf.images.v1.create(
    account_id=secrets["accid"],
    file=(image.name, imgfile, "image/jpeg")
)

2. 验证文件有效性

即便已检查文件头,仍可通过Pillow确认文件是否为可正常解析的JPEG:

from PIL import Image

try:
    with Image.open(image) as img:
        assert img.format == "JPEG", "文件不是有效JPEG格式"
except Exception as e:
    print(f"文件验证失败: {e}")

3. 确认API Token权限

确保你的API Token拥有Cloudflare Images的编辑/上传权限(权限路径:Account > Cloudflare Images > Edit),虽然当前错误并非权限问题,但权限不足也可能引发异常。

内容的提问来源于stack exchange,提问作者juan noguera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 21:13:18