Create a TypeScript function that fetches data from Azure Storage Blob service, processes it, and uploads the processed data back to another container in the same storage account.
PLAINFORMAT
import { BlobServiceClient, StorageSharedKeyCredential } from "@azure/storage-blob";

// Replace these values with your own storage account name and key
const STORAGE_ACCOUNT_NAME = "your_storage_account_name";
const STORAGE_ACCOUNT_KEY = "your_storage_account_key";

// Initialize the shared key credential and blob service client
const sharedKeyCredential = new StorageSharedKeyCredential(STORAGE_ACCOUNT_NAME, STORAGE_ACCOUNT_KEY);
const blobServiceClient = new BlobServiceClient(
  `https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`,
  sharedKeyCredential
);

async function processBlobData(sourceContainer: string, destinationContainer: string, blobName: string): Promise<void> {
  try {
    // Get source and destination containers
    const sourceContainerClient = blobServiceClient.getContainerClient(sourceContainer);
    const destinationContainerClient = blobServiceClient.getContainerClient(destinationContainer);

    // Fetch the blob data from the source container
    const sourceBlobClient = sourceContainerClient.getBlobClient(blobName);
    const response = await sourceBlobClient.download();
    const rawData = (await streamToString(response.readableStreamBody!)).trim();

    // Process the data - for example, convert the text to uppercase
    const processedData = rawData.toUpperCase();

    // Upload the processed data to the destination container
    const destinationBlobClient = destinationContainerClient.getBlockBlobClient(blobName);
    await destinationBlobClient.upload(processedData, Buffer.byteLength(processedData));

    console.log(`Processed data uploaded to ${destinationContainer}/${blobName}`);
  } catch (error) {
    console.error("Error processing blob data:", error);
  }
}

// Helper function to read a readable stream into a string
async function streamToString(readableStream: NodeJS.ReadableStream): Promise<string> {
  return new Promise((resolve, reject) => {
    const chunks: any[] = [];
    readableStream.on("data", (data) => {
      chunks.push(data.toString());
    });
    readableStream.on("end", () => {
      resolve(chunks.join(""));
    });
    readableStream.on("error", reject);
  });
}

// Example usage:
processBlobData("source-container", "destination-container", "example.txt");