Search code examples
azure-blob-storageazure-webjobsazure-webjobssdkazure-queues

WebJob to read from and write to the same Azure blob


I am trying to process images uploaded to azure using webjob. I have 2 containers image and thumbs.

Currently, I am reading from image container, creating a thumbnail and writing it to thumbs container using the following code, which works great.

public static void GenerateThumbnail([QueueTrigger("addthumb")] ImageDTO blobInfo,
                [Blob("images/{Name}", FileAccess.Read)] Stream input, [Blob("thumbs/{Name}")] CloudBlockBlob outputBlob)
    {
        using (Stream output = outputBlob.OpenWrite())
        {
            ConvertImageToThumbnail(input, output, blobInfo.Name);
            outputBlob.Properties.ContentType = GetMimeType(blobInfo.Name);
        }
    }

Now, I would also like to resize the main image from image container (if it's too big), compress it and replace the original with it.

Is there a way to read from and write to the same blob?


Solution

  • Yes, you can read/write to the same blob. For example you could change your input binding to bind to CloudBlockBlob using FileAccess.ReadWrite:

    public static void GenerateThumbnail(
        [QueueTrigger("addthumb")] ImageDTO blobInfo,
        [Blob("images/{Name}", FileAccess.ReadWrite)] CloudBlockBlob input,
        [Blob("thumbs/{Name}")] CloudBlockBlob output)
    {
        // Process the image  
    }
    

    You can then access the OpenRead/OpenWrite stream methods on that blob to read the image blob and process/modify it as needed.