[Python] Header Info is saved to image from FormData request

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am building an API endpoint that should upload an Image I send over FormData to my Supabase storage folder. It works fine so far, but the images I find in my Supabase Storage Dashboard are broken. When I download one I can see that there is header info saved to the image file. How do I avoid this? If i remove the header info from the downloaded file manually, the file is fixed.

    The Api endpoint:

    def upload(request: Request) -\> Response:
    try:
    image = request.files\['image'\]
    filename = image.filename
    mimetype = image.mimetype or "image/jpeg"

    # Create a temporary file
    with tempfile.NamedTemporaryFile(delete=False) as tmp:
    tmp.write(image.read())
    tmp.flush()

    # Upload the temp file using its path
    upload_response = supabase.storage.from_("products").upload(
    f"products/{filename}",
    tmp.name,
    {"Content-Type": mimetype}
    )

    return jsonify({"message": "Image uploaded successfully"}), 201

    except Exception as e:
    return jsonify({"error": str(e)}), 500


    The downloaded image file (beginning):

    --b6d61cb67cff2bea49da34524d19850e
    Content-Disposition: form-data; name="file"; filename="postman.png"
    Content-Type: text/plain;charset=UTF-8

    ‰PNG
    IHDR..


    I want to pass the image I get from the FormData of the API request to the supabase storage. How can I avoid the header info in the file?

    Continue reading...

Compartilhe esta Página