使用Azure函数检查Blob存储上是否存在文件 [英] Check if file exists on blob storage with Azure functions

查看:113
本文介绍了使用Azure函数检查Blob存储上是否存在文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于 https://ppolyzos.com/2016/12/30/resize-images-using-azure-functions/我有以下C#代码使用Azure Functions调整图像大小.

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);
}

结果将是名为520x245-{blobname}.{blobextension}的图像.

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

我只希望在blob容器中不存在生成的图像的情况下运行代码.
如何获取容器上的现有文件?

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?

推荐答案

由于您正在使用CloudBlockBlob类型绑定outputBlob.您可以使用以下代码检查此blob是否存在.

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  
}

当前,Azure Function不允许我们在输出Blob绑定中使用CloudBlockBlob.解决方法是在function.json中将方向更改为"inout".之后,我们可以在输出Blob绑定中使用CloudBlockBlob.

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"
}

这篇关于使用Azure函数检查Blob存储上是否存在文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆