如何使用Azure Functions删除blob?

我正在创建一个Azure函数,该函数在图像上载或添加到特定Azure存储时触发,并执行以下操作:1。)调整图像大小2.)将图像放入正确的目录(使用输出绑定)3。)删除处理后添加到Azure存储的原始Blob映像。

我完成了流程中的步骤1和步骤2,但是我发现很少有关于删除blob或API的文档,这些文档将公开Azure存储的方法。 (使用C#)

这是示例代码:

#r "System.Drawing" using System; using ImageResizer; using System.Drawing; using System.Drawing.Imaging; public static void Run(Stream inputImage, string imageName, Stream resizedImage, TraceWriter log) { // Log the file name and size log.Info($"C# Blob trigger function Processed blob\n Name:{imageName} \n Size: {inputImage.Length} Bytes"); // Manipulate the image var settings = new ImageResizer.ResizeSettings { MaxWidth = 400, Format = "png" }; ImageResizer.ImageBuilder.Current.Build(inputImage, resizedImage, settings); // Delete the Raw Original Image Step } 

要删除blob,您需要

 var container = blobClient.GetContainerReference(containerName); var blockBlob = container.GetBlockBlobReference(fileName); return blockBlob.DeleteIfExists(); 

在尝试此操作之前,请确保关闭所有流,以便不再使用该图像。

确保导入正确的引用:

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

然后,您可以使用CloudBlockBlob作为参数类型并将其删除:

 public static void Run(CloudBlockBlob myBlob, string name, TraceWriter log) { myBlob.DeleteIfExists(); } 

当您使用C#时,您可以为您的函数使用多种输入类型,这里是webjobs sdk 备忘单,详细介绍了大多数可用的。

在您的情况下,您可以将输入图像请求为CloudBlockBlob ,它具有删除方法。 您可以在resize函数内或单独触发的函数中调用它来删除已完成的blob。 您可能需要将绑定direction更改为inout ,请参阅此处 。

目前没有自动清理的约束力。