使用Azure功能检查Blob存储上是否存在文件

时间:2017-06-01 10:13:57

标签: azure azure-storage-blobs 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);
}

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

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

4 个答案:

答案 0 :(得分:3)

由于您使用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功能并不允许我们在输出blob绑定中使用CloudBlockBlob。解决方法是将方向更改为" inout"在function.json中。之后,我们可以在输出blob绑定中使用CloudBlockBlob。

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

答案 1 :(得分:1)

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

CloudBlockBlob existingBlob = container.GetBlockBlobReference(blobName);

使用

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

答案 2 :(得分:0)

借助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

答案 3 :(得分:0)

相同的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 
}