I have this issue with net7.0, dotnet-isolated: the documentation said that I can work with stream but my code crashes.
I work with these packages:
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="6.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
My function is this:
[Function(nameof(CMImageResizer))]
public async Task Run(
[BlobTrigger("rawimages/{name}", Connection = "StorageAccountConnectionString")] Stream myBlob,
[BlobInput("resizedimages/{name}", Connection = "StorageAccountConnectionString")] Stream resizedImage, string name)
{
try
{
_log.LogInformation("Processing image:{name} size: {myBlobLength} Bytes", name, myBlob.Length);
And the error code is:
Executing 'Functions.CMImageResizer' (Reason='New blob detected(LogsAndContainerScan): rawimages/taladro1.jpg', Id=00c22256-e65c-478c-82c3-eaca92c310b7)
[2023-12-24T05:35:39.632Z] Trigger Details: MessageId: 2f450295-dae1-42f5-ac2e-089d5b0d2a55, DequeueCount: 1, InsertedOn: 2023-12-24T05:35:38.000+00:00, BlobCreated: 2023-12-24T04:42:41.000+00:00, BlobLastModified: 2023-12-24T05:35:28.000+00:00 [2023-12-24T05:35:40.601Z] Function 'CMImageResizer',
Invocation id '00c22256-e65c-478c-82c3-eaca92c310b7': An exception was thrown by the invocation. [2023-12-24T05:35:40.604Z] Result: Function 'CMImageResizer', Invocation id '00c22256-e65c-478c-82c3-eaca92c310b7': An exception was thrown by the invocation. [2023-12-24T05:35:40.604Z] Exception: Microsoft.Azure.Functions.Worker.FunctionInputConverterException:
Error converting 1 input parameters for Function 'CMImageResizer': Cannot convert input parameter 'myBlob' to type 'System.IO.Stream' from type 'System.ReadOnlyMemory1[[System.Byte, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'. Error:System.Text.Json.JsonException: 'R' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
[2023-12-24T05:35:40.604Z] ---> System.Text.Json.JsonReaderException: 'R' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0.
[2023-12-24T05:35:40.604Z] at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan1 bytes) [2023-12-24T05:35:40.604Z] at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte marker) [2023-12-24T05:35:40.604Z] at System.Text.Json.Utf8JsonReader.ReadFirstToken(Byte first) [2023-12-24T05:35:40.604Z] at System.Text.Json.Utf8JsonReader.ReadSingleSegment() [2023-12-24T05:35:40.604Z] at System.Text.Json.Utf8JsonReader.Read() [2023-12-24T05:35:40.604Z] at System.Text.Json.Serialization.JsonConverter1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
What's wrong, because the documentation said that now the worker allow working with stream.
This is still broken even after one year after being known and originally reported on GitHub as an issue: link
The workaround as mentioned in the linked issue comment is to use a side parameter referring to the name of the file if you're using a Stream and file name expressions (like "container/{name}"). So in your example, I would try the following:
[Function(nameof(CMImageResizer))]
public async Task Run(
[BlobTrigger("rawimages/{name}", Connection = "StorageAccountConnectionString")] Stream myBlob,
string blobName, // Add this parameter
[BlobInput("resizedimages/{name}", Connection = "StorageAccountConnectionString")] Stream resizedImage,
string name)
{
// Process your files
}
I'll also share my own experience with this problematic case: I have a function that I only want to operate on the trigger and output a file, without using a BlobInput parameter. However, I could not get the Stream binding to work without any BlobInput parameters, so I resorted to using byte[] for the sake of getting the job done. In my case I'm thankfully not concerned about allocating the entire array in the memory, as the images I'm processing are not greater than 2 MB each.
I would also try using the ReadOnlyMemory<byte> instance directly by binding to that, though the docs don't mention anything about supporting it, so it would be my last resort if I tried finding optimizations without using Stream.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With