Content type changes when using copying file from one blob to another using extra outputs (blob storage binding)

Alexandru BAU 20 Reputation points
2024-05-22T20:35:04.3466667+00:00

I'm a TypeScript developer trying to build a flow using Function App.

The flow is very simple: when a file gets uploaded to Storage Blob a message containing the filename is dispatched on Storage Queue which triggers another function (doc-analyse) that copies the original file to a different Storage Blob. I am using extra inputs and outputs to perform this action. This is actually the example that you can find in the official docs.

The type of files that I am copying are PDFs and I've noticed that the copied file is always empty. It has the exact number of pages as the original PDF but they are all empty. I've noticed that the copied file has a different content-type which is application/octet-stream.

Maybe this is the issue? Can someone explain to me why this is happening and how can I fix it?

I've found this very old issue on Github that may be related.

import { app, InvocationContext, input, output } from '@azure/functions';  
  
const blobInput = input.storageBlob({
  connection: 'DOCUMENTS_STORAGE',
  path: 'documents/uploads/{queueTrigger}',
});
const blobOutput = output.storageBlob({
  connection: 'DOCUMENTS_STORAGE',
  path: 'documents/analyzed/analyzed-{queueTrigger}',
});
async function docAnalyse(queueItem: unknown, context: InvocationContext): Promise<void> {
  const blobInputValue = <string>context.extraInputs.get(blobInput);
  context.extraOutputs.set(blobOutput, blobInputValue);
}
app.storageQueue('doc-analyse', {
  connection: 'DOCUMENTS_STORAGE',
  queueName: 'doc-analyze',
  extraInputs: [blobInput],
  extraOutputs: [blobOutput],
  handler: docAnalyse
});

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,551 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anand Prakash Yadav 7,540 Reputation points Microsoft Vendor
    2024-05-23T11:00:55.2966667+00:00

    Hello Alexandru BAU,

    Thank you for posting your query here!

    The reason why the content type changes when copying a file from one blob to another using extra outputs in Azure Functions is that the blob output binding does not automatically preserve the content type of the source file. Instead, it uses the default content type of application/octet-stream, unless you explicitly specify a different content type in the binding configuration. This is a known limitation of the blob output binding.

    The solution to preserve the content type of the source file when copying a file from one blob to another using extra outputs in Azure Functions is to use the contentType property in the blob output binding configuration. This property allows you to specify the content type of the output blob, either as a static value or as a dynamic expression. For example, if you know that the source file is always a PDF document, you can set the contentType property to application/pdf, as shown in the following code snippet:

    const blobOutput = output.storageBlob({
    connection: 'DOCUMENTS_STORAGE',
    path: 'documents/analyzed/analyzed-{queueTrigger}',
    contentType: 'application/pdf' // specify the content type of the output blob
    });
    

    Alternatively, if you want to dynamically set the content type of the output blob based on the content type of the input blob, you can use the context.bindings object to access the input blob metadata, and use the contentType property of the input blob as the value of the contentType property of the output blob, as shown in the following code snippet:

    const blobOutput = output.storageBlob({
    connection: 'DOCUMENTS_STORAGE',
    path: 'documents/analyzed/analyzed-{queueTrigger}',
    contentType: context.bindings.blobInput.contentType // use the content type of the input blob as the content type of the output blob
    });
    

    By using the contentType property in the blob output binding configuration, you can ensure that the content type of the copied file is the same as the content type of the original file and avoid any issues with opening or processing the file.

    Do let us know if you have any further queries. I’m happy to assist you further.

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful