1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Error 5455 when uploading to Cloudflare images using python library

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 17:12.

  1. Stack

    Stack Membro Participativo

    I use the latest version of the python library from Cloudflare to try to upload an image programmatically to my Cloudflare Images account. The code is quite simple:

    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:
    res = cf.images.v1.create(account_id = secrets["accid"], file = imgfile)


    Since the documentation is very generic about the "file" parameter:

    def create(self,
    *,
    account_id: str,
    file: object | NotGiven = NOT_GIVEN,
    metadata: object | NotGiven = NOT_GIVEN,
    require_signed_urls: bool | NotGiven = NOT_GIVEN,
    url: str | NotGiven = NOT_GIVEN,
    extra_headers: Mapping[str, str | Omit] | None = None,
    extra_query: Mapping[str, object] | None = None,
    extra_body: object | None = None,
    timeout: float | Timeout | None | NotGiven = NOT_GIVEN) -> Image

    Upload an image with up to 10 Megabytes using a single HTTP POST (multipart/ form-data) request. An image can be uploaded by sending an image file or passing an accessible to an API url.

    Args:
    account_id: Account identifier tag.
    file: An image binary data. Only needed when type is uploading a file.
    ...


    I tried various options:

    res = cf.images.v1.create(account_id = secrets["accid"], file = image)
    res = cf.images.v1.create(account_id = secrets["accid"], file = imgfile.read())
    res = cf.images.v1.create(account_id = secrets["accid"], file = io.open(image, "rb)


    but always throws the same error:


    cloudflare.APIStatusError: Error code: 415 - {'result': None, 'success': False, 'errors': [{'code': 5455, 'message': 'Uploaded image must have image/jpeg, image/png, image/webp, image/gif or image/svg+xml content-type'}], 'messages': []}

    The image is a 2.5MB Jpeg (I checked the header with Hex Editor, just in case). Any educated guess on what else I should try that may fix that will be most appreciated.

    Continue reading...

Compartilhe esta Página