2

Based on https://ppolyzos.com/2016/12/30/resize-images-using-azure-functions/ I have the following C# code to resize an image using Azure Functions.

#r "Microsoft.WindowsAzure.Storage"
using ImageResizer;
using ImageResizer.ExtensionMethods;
using Microsoft.WindowsAzure.Storage.Blob;

public static void Run(Stream inputBlob, string blobname, string blobextension, CloudBlockBlob outputBlob, TraceWriter log)
{
    log.Info($"Resize function triggered\n Image name:{blobname} \n Size: {inputBlob.Length} Bytes");
    log.Info("Processing 520x245");

    /// Defining parameters for the Resizer plugin
    var instructions = new Instructions
    {
        Width = 520,
        Height = 245,
        Mode = FitMode.Carve,
        Scale = ScaleMode.Both
    };

    /// Resizing IMG
    Stream stream = new MemoryStream();
    ImageBuilder.Current.Build(new ImageJob(inputBlob, stream, instructions));
    stream.Seek(0, SeekOrigin.Begin);

    /// Changing the ContentType (MIME) for the resulting images
    string contentType = $"image/{blobextension}";
    outputBlob.Properties.ContentType = contentType;
    outputBlob.UploadFromStream(stream);
}

The result will be an image named 520x245-{blobname}.{blobextension}.

I would like the code to run only if the resulting image does not already exist in the blob container.
How can I get the existing files on the container?

maudv
  • 71
  • 1
  • 2
  • 4

4 Answers4

4

Since you are using CloudBlockBlob type to bind outputBlob. You could check whether this blob exist or not using following code.

if (outputBlob.Exists())
{
    log.Info($"520x245-{blobname}.{blobextension} is already exist");  
}
else
{
    log.Info($"520x245-{blobname}.{blobextension} is not exist");  
    //do the resize and upload the resized image to blob  
}

Currently, Azure Function doesn't allow us to use CloudBlockBlob in output blob binding. A workaround is change the direction to "inout" in function.json. After that, we can use CloudBlockBlob in output blob binding.

{
  "type": "blob",
  "name": "outputBlob",
  "path": "mycontainer/520x245-{blobname}.{blobextension}",
  "connection": "connectionname",
  "direction": "inout"
}
Amor
  • 8,325
  • 2
  • 19
  • 21
  • Any updates? Is your issue solved, If yes, please mark helpful replies as answer. If you have further questions, please feel free to let me know. – Amor Jun 22 '17 at 08:15
1

Check if your Blob exists in the container, but then you will need to add the CloudBlobContainer as input parameter as well.

CloudBlockBlob existingBlob = container.GetBlockBlobReference(blobName);

And check if it exists using

await existingBlob.ExistsAsync()
  • In order to add the `CloudBlobContainer container` I believe I would need to bind it to Azure Functions. I gave it a go with: `{"type": "container", "name": "folder", "path": "media", "connection": AzureWebJobsDashboard", "direction": "in" }` but that didn't work. Any idea how to do it? I google'd for any documentation about it to no avail. – maudv Jun 01 '17 at 15:57
  • Would this work for you? https://stackoverflow.com/questions/36953126/azure-function-resize-image-stored-in-a-blob-container – Ling Toh Jun 01 '17 at 16:43
  • You are correct, I should have checked the documentation first. Seems like you can't bind a BlobContainer. – Steven Van Eycken Jun 02 '17 at 09:35
1

With Azure Blob storage library v12, you can use BlobBaseClient.Exists()/BlobBaseClient.ExistsAsync()

Usage is something like below,

var blobServiceClient = new BlobServiceClient(_storageConnection);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(_containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);

bool isExists = await blobClient.ExistsAsync(cancellationToken);

BlobBaseClient.Exists(CancellationToken) Method

BlobBaseClient.ExistsAsync(CancellationToken) Method

Jaliya Udagedara
  • 1,087
  • 10
  • 16
0

Java version for the same ( using the new v12 SDK )

This uses the Shared Key Credential authorization, which is the account access key.

StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);
String endpoint = String.format(Locale.ROOT, "https://%s.blob.core.windows.net", accountName);
BlobServiceClient storageClient = new BlobServiceClientBuilder().credential(credential)
                                      .endpoint(endpoint).buildClient();

BlobContainerClient container = storageClient.getBlobContainerClient(containerName)
if ( container.exists() ) {
   // perform operation when container exists 
}         
Somansh Reddy
  • 125
  • 2
  • 13