以下触发器在将Blob(图像)上的exif数据上传到Azure存储后,将其删除。问题在于,每个Blob触发器都会触发至少5次。
在触发器中,通过向其中写入新的数据流来更新Blob。我以为blob receipts会阻止针对此Blob的Blob触发器的进一步触发。
[FunctionName("ExifDataPurge")]
public async System.Threading.Tasks.Task RunAsync(
[BlobTrigger("container/{name}.{extension}", Connection = "")]CloudBlockBlob image,
string name,
string extension,
string blobTrigger,
ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name}");
try
{
var memoryStream = new MemoryStream();
await image.DownloadToStreamAsync(memoryStream);
memoryStream.Position = 0;
using (Image largeImage = Image.Load(memoryStream))
{
if (largeImage.Metadata.ExifProfile != null)
{
//strip the exif data from the image.
for (int i = 0; i < largeImage.Metadata.ExifProfile.Values.Count; i++)
{
largeImage.Metadata.ExifProfile.RemoveValue(largeImage.Metadata.ExifProfile.Values[i].Tag);
}
var exifStrippedImage = new MemoryStream();
largeImage.Save(exifStrippedImage, new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder());
exifStrippedImage.Position = 0;
await image.UploadFromStreamAsync(exifStrippedImage);
}
}
}
catch (UnknownImageFormatException unknownImageFormatException)
{
log.LogInformation($"Blob is not a valid Image : {name}.{extension}");
}
}
触发器的处理方式是通过将收据存储在容器azure-webjobs-hosts
中来跟踪处理了哪些斑点。任何没有收据的Blob或旧的收据(基于Blob ETag)将被处理(或重新处理)。
因为您正在调用await image.UploadFromStreamAsync(exifStrippedImage);
,它会被触发(假设其未处理)
当您调用await image.UploadFromStreamAsync(exifStrippedImage);
时,它将更新blob,因此blob功能将再次触发。
您可以尝试检查Blob上现有的CacheControl
属性,如果已更新它以打破循环,则不对其进行更新。
CacheControl