0

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?

mathewc
  • 13,312
  • 2
  • 45
  • 53
CoOl
  • 2,637
  • 21
  • 24

1 Answers1

1

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.

mathewc
  • 13,312
  • 2
  • 45
  • 53
  • Great, I'll try it soon! – CoOl Jan 11 '16 at 16:44
  • Looking at your example, I presume I don't need to use queue at all? Can I trigger a webjob function when a new blob is added to a container? – CoOl Jan 11 '16 at 16:54
  • 1
    I just updated the example :) Technically you don't need the queue, but use of a queue is the recommended pattern. `BlobTrigger` can trigger automatically, but there can be latencies as described [here](https://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-webjobs-sdk-storage-blobs-how-to/). – mathewc Jan 11 '16 at 16:57
  • Can I do multiple binding to the same blob as well? for example, `[Blob("images/{Name}", FileAccess.Read)] Stream input, [Blob("images/{Name}")] CloudBlockBlob output` ? – CoOl Jan 12 '16 at 07:47