问题描述
基于 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
Image name:{blobname}
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 存储中是否存在文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!