Introduction

In this article, we'll walk through creating an Azure Blob Trigger Function to compress images uploaded to Azure Storage using the sharp image processing library. The function automatically compresses the images and saves them to a specified directory.

Prerequisites

Before we begin, ensure you have the following.

Step 1. Create an Azure Blob Storage.

Begin by creating an Azure Storage Account if you haven't already. Inside the Storage Account, create a container named blob-trigger. This container will be used to trigger the Azure Function whenever a new blob (file) is added.

Step 2. Set up your Azure Function App.

Create an Azure Function App in the Azure portal. Make sure to select the Node.js runtime. Once the Function App is created, navigate to the Functions section and add a new function. Choose the Blob trigger template and set the container name to blob-trigger (or whatever container you created in Step 1).

Step 3. Configure local.settings.json.

In your function app folder, create a file named local.settings.json to store local development settings. Update the file with your Azure Storage Account connection string, account name, and key. Also, specify the blob container name and Node.js as the runtime.

Here’s how the local.settings.json file should look.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "Your_Connection_String_Here",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "gunalearnings_STORAGE": "Your_Connection_String_Here"
  }
}

Step 4. Configure host.json.

In your function app folder, create a file named host.json to define global configuration settings for your function app. This includes settings for monitoring, such as enabling Application Insights and configuring function extensions.

Here's an example of what the host.json file might look like.

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.*, 5.0.0)"
  },
  "concurrency": {
    "dynamicConcurrencyEnabled": true,
    "snapshotPersistenceEnabled": true
  }
}

Step 5. Write the Azure Blob Trigger Function Code.

Now, create a file named blob-trigger-poc.ts and implement the Azure Blob Trigger Function for image compression using the Sharp library.

Here's the code that triggers a new blob being uploaded, compresses the image, and saves it to a new location.

import { app, InvocationContext } from "@azure/functions";
import sharp from "sharp";
import * as fs from "fs";
import * as path from "path";

export async function blobtriggerpoc(
  blob: Buffer,
  context: InvocationContext
): Promise<void> {
  const blobName = context.triggerMetadata.name;
  context.log(`Processing blob: ${blobName}`);

  try {
    // Compress the image
    const compressedImage = await sharp(blob)
      .resize({ width: 800 })
      .jpeg({ quality: 80 })
      .toBuffer();

    // Define the output path correctly
    const outputDir = path.join("src/", "compressed-images");
    if (!fs.existsSync(outputDir)) {
      fs.mkdirSync(outputDir, { recursive: true });
    }

    const outputPath = path.join(outputDir, `${blobName}-compressed.jpg`);

    // Ensure the output directory exists
    if (!fs.existsSync(outputDir)) {
      fs.mkdirSync(outputDir, { recursive: true });
    }

    // Write the compressed image to the specified path
    fs.writeFileSync(outputPath, compressedImage);

    context.log(`Compressed image saved to: ${outputPath}`);
  } catch (error) {
    context.log(`Error processing blob: ${error.message}`);
  }
}

app.storageBlob("blobtriggerpoc", {
  path: "blob-trigger/{name}",
  connection: "Your_Connection_String_Here",
  handler: blobtriggerpoc,
});

Explanation of the Code

The code has been finalized. Please execute it by pressing F5.

Azure function

Step 6. Open Azure Storage Explorer and Upload an Image.

Conclusion

by using Azure Blob Storage and an Azure Function with a blob trigger, you can efficiently automate image processing tasks such as compression. With the steps provided, you can upload images to your blob container, trigger the function, and easily store the processed results. This workflow enables seamless automation of image handling, ensuring scalability and efficient resource management in the cloud.