Create Image Upload is broken for Sync and Async

未关闭
#2,569 3 条评论 6 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
api

调研方向

先从 Cloudflare 和 AsyncCloudflare 客户端的 images.v1.create 开始,然后检查 tests/api_resources/images/test_v1.py 中链接覆盖率附近的内容。使用为 WebP 文件展示的元组重现 multipart 上传,并在请求能够接受该上传且不再出现表单字段解析错误后,为同步和异步情况添加覆盖率。

由索引模型根据 Issue 内容生成。

描述

Confirm this is a Python library issue and not an underlying Cloudflare API issue.
  • This is an issue with the Python library
Describe the bug

The Cloudflare Image Create function is broken.

There is an example below, this has also been discussed on StackOverflow

https://stackoverflow.com/questions/79032782/error-5455-when-uploading-to-cloudflare-images-using-python-library

I cant see any test coverage for this use case.

https://github.com/cloudflare/cloudflare-python/blob/fc6ca90779cd230dc06445ba42e4c540da186ce0/tests/api_resources/images/test_v1.py#L28

To Reproduce

This is a working example using Python Requests

import requests
with open(
    "water bottle.webp", "rb"
) as f:
    data = f.read()

# try requests
files = {
    "file": (
        "water bottle.webp",
        data,
        "image/webp",
    )
}

response = requests.post(
    url=f"https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/images/v1",
    headers={
        "Authorization": f"Bearer ..."
    },
    files=files,
)
print(response.status_code)
print(response.json())

This is a broken example using the Cloudflare Sync function

with open(
    "water bottle.webp", "rb"
) as f:
    data = f.read()

from cloudflare import Cloudflare

client = Cloudflare(
    api_token='...'
)

# Create a tuple: (filename, binary data, content type)
file_tuple = ("water bottle.webp", data,   "image/webp",)

cloudflare_image = await client.images.v1.create(
    account_id=CLOUDFLARE_ACCOUNT_ID,
    file=file_tuple,
)
print(cloudflare_image)

The output is

cloudflare.BadRequestError: Error code: 400 - {'result': None, 'success': False, 'errors': [{'code': 5400, 'message': 'Bad request: Error parsing form fields'}], 'messages': []}

This is a broken example using the Async Cloudflare function

from cloudflare import AsyncCloudflare

client = AsyncCloudflare(
    api_token='...'
)

# Create a tuple: (filename, binary data, content type)
file_tuple = ("water bottle.webp", data,   "image/webp",)

cloudflare_image = await client.images.v1.create(
    account_id=CLOUDFLARE_ACCOUNT_ID,
    file=file_tuple,
)
print(cloudflare_image)

The output is

cloudflare.BadRequestError: Error code: 400 - {'result': None, 'success': False, 'errors': [{'code': 5400, 'message': 'Bad request: Error parsing form fields'}], 'messages': []}
Code snippets

OS

macOS

Python version

3.12.9

Library version

v4.1.0

主要语言
Python
星标
509
派生
150
平均合并
3 小时 15 分钟
30 天内合并 PR
1

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

cloudflare/cloudflare-python 的其他 Issue

查看 cloudflare/cloudflare-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。