简体   繁体   English

使用 Azure 函数检查 Blob 存储上是否存在文件

[英]Check if file exists on blob storage with 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.基于https://ppolyzos.com/2016/12/30/resize-images-using-azure-functions/我有以下 C# 代码来使用 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} .结果将是一个名为520x245-{blobname}.{blobextension}

I would like the code to run only if the resulting image does not already exist in the blob container.我希望代码仅在 blob 容器中不存在生成的图像时运行。
How can I get the existing files on the container?如何获取容器上的现有文件?

Since you are using CloudBlockBlob type to bind outputBlob.由于您使用 CloudBlockBlob 类型来绑定 outputBlob。 You could check whether this blob exist or not using following code.您可以使用以下代码检查此 blob 是否存在。

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.目前,Azure Function 不允许我们在输出 Blob 绑定中使用 CloudBlockBlob。 A workaround is change the direction to "inout" in function.json.解决方法是在 function.json 中将方向更改为“inout”。 After that, we can use CloudBlockBlob in output blob binding.之后,我们可以在输出 blob 绑定中使用 CloudBlockBlob。

{
  "type": "blob",
  "name": "outputBlob",
  "path": "mycontainer/520x245-{blobname}.{blobextension}",
  "connection": "connectionname",
  "direction": "inout"
}

Check if your Blob exists in the container, but then you will need to add the CloudBlobContainer as input parameter as well.检查您的 Blob 是否存在于容器中,但随后您还需要添加 CloudBlobContainer 作为输入参数。

CloudBlockBlob existingBlob = container.GetBlockBlobReference(blobName);

And check if it exists using并检查它是否存在使用

await existingBlob.ExistsAsync()

With Azure Blob storage library v12, you can use BlobBaseClient.Exists()/BlobBaseClient.ExistsAsync()借助 Azure Blob 存储库 v12,可以使用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.Exists(CancellationToken) 方法

BlobBaseClient.ExistsAsync(CancellationToken) Method BlobBaseClient.ExistsAsync(CancellationToken) 方法

Java version for the same ( using the new v12 SDK ) Java 版本相同(使用新的 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 
}         

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM