【问题标题】:Check if file exists on blob storage with Azure functions使用 Azure 函数检查 Blob 存储中是否存在文件
【发布时间】:2017-11-02 10:30:12
【问题描述】:

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

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

我希望代码仅在 blob 容器中不存在生成的图像时运行。
如何获取容器上的现有文件?

【问题讨论】:

    标签: azure azure-blob-storage azure-functions


    【解决方案1】:

    检查您的 Blob 是否存在于容器中,但您还需要添加 CloudBlobContainer 作为输入参数。

    CloudBlockBlob existingBlob = container.GetBlockBlobReference(blobName);
    

    并使用

    检查它是否存在
    await existingBlob.ExistsAsync()
    

    【讨论】:

    • 为了添加 CloudBlobContainer container 我相信我需要将它绑定到 Azure Functions。我试了一下:{"type": "container", "name": "folder", "path": "media", "connection": AzureWebJobsDashboard", "direction": "in" } 但这没有用。知道怎么做吗?我用谷歌搜索了有关它的任何文档都无济于事。
    • 你说得对,我应该先检查文档。好像你不能绑定 BlobContainer。
    【解决方案2】:

    由于您使用 CloudBlockBlob 类型来绑定 outputBlob。您可以使用以下代码检查此 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  
    }
    

    目前,Azure Function 不允许我们在输出 blob 绑定中使用 CloudBlockBlob。一种解决方法是在 function.json 中将方向更改为“inout”。之后,我们可以在输出 blob 绑定中使用 CloudBlockBlob。

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

    【讨论】:

    • 有什么更新吗?您的问题解决了吗,如果是,请将有用的回复标记为答案。如果您还有其他问题,请随时告诉我。
    【解决方案3】:

    借助 Azure Blob 存储库 v12,您可以使用 BlobBaseClient.Exists()/BlobBaseClient.ExistsAsync()

    用法如下,

    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.ExistsAsync(CancellationToken) Method

    【讨论】:

      【解决方案4】:

      Java 版本相同(使用新的 v12 SDK)

      这使用共享密钥凭据授权,即帐户访问密钥。

      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 
      }         
      

      【讨论】:

        猜你喜欢
        • 2013-07-05
        • 2011-02-08
        • 2011-10-14
        • 1970-01-01
        • 2012-06-17
        • 1970-01-01
        • 1970-01-01
        • 2017-03-06
        • 2020-06-04
        相关资源
        最近更新 更多