Skip to content Skip to sidebar Skip to footer

Azure Function Saving Temp Pdf File From Inputstream Is Corrupted

I have uploaded a pdf to a blob storage which when downloaded through MS Azure Explorer is absolutely fine. I have an Azure function that get's triggered by a queue and also has a

Solution 1:

Yes, this looks broken, the first few bytes are altered, maybe more (marvin3.jpg is the source image in blob storage).

hex dump

imgcat on bad image

As a workaround, just add this to your function.json blob input binding:

"dataType":"binary"

as in:

{"name":"inputBlob","type":"blob","dataType":"binary","direction":"in","path":"images/input_image.jpg","connection":"AzureWebJobsStorage"}

You shouldn't need to put that in (it's only needed for the JavaScript worker) but i guess there's a bug somewhere in the SDK that prevents the right type being inferred.

Full working example:

defmain(req: func.HttpRequest, inputBlob: func.InputStream) -> func.HttpResponse:
    blob = inputBlob.read()

    withopen("out.jpg", "wb") as outfile:
        outfile.write(blob)

    return func.HttpResponse(
            "Done. Binary data written to out.jpg",
            status_code=200
        )

imgcat on good image

This end to end test they have in the Python worker repo also seems to suggest that "dataType": "binary" should be there when using blob input bindings (no matter the file type you should get bytes).

If you're trying to cast the input blob as inputBlob: bytes instead of inputBlob: func.InputStream, the problem becomes more apparent if you don't have dataType specified:

Exception: TypeError: a bytes-likeobjectis required, not'str'

The Python worker gives you back a string instead of bytes.

I have opened an issue here for the docs to be updated.

Post a Comment for "Azure Function Saving Temp Pdf File From Inputstream Is Corrupted"