24

我想检查 Azure Blob 存储中是否存在特定文件。是否可以通过指定文件名进行检查?每次我得到文件未找到错误。

4

9 回答 9

39
var blob = client.GetContainerReference(containerName).GetBlockBlobReference(blobFileName);

if (blob.Exists())
 //do your stuff
于 2013-09-20T20:55:00.733 回答
18

此扩展方法应该可以帮助您:

public static class BlobExtensions
{
    public static bool Exists(this CloudBlob blob)
    {
        try
        {
            blob.FetchAttributes();
            return true;
        }
        catch (StorageClientException e)
        {
            if (e.ErrorCode == StorageErrorCode.ResourceNotFound)
            {
                return false;
            }
            else
            {
                throw;
            }
        }
    }
}

用法:

static void Main(string[] args)
{
    var blob = CloudStorageAccount.DevelopmentStorageAccount
        .CreateCloudBlobClient().GetBlobReference(args[0]);
    // or CloudStorageAccount.Parse("<your connection string>")

    if (blob.Exists())
    {
        Console.WriteLine("The blob exists!");
    }
    else
    {
        Console.WriteLine("The blob doesn't exist.");
    }
}

http://blog.smarx.com/posts/testing-existence-of-a-windows-azure-blob

于 2012-06-14T12:13:14.493 回答
14

使用更新的 SDK,一旦您拥有 CloudBlobReference,您就可以在您的引用上调用 Exists()。

更新

相关文档已移至https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.blob.cloudblob.exists?view=azurestorage-8.1.3#Microsoft_WindowsAzure_Storage_Blob_CloudBlob_Exists_Microsoft_WindowsAzure_Storage_Blob_BlobRequestOptions_Microsoft_WindowsAzure_Storage_OperationContext_

我使用 WindowsAzure.Storage v2.0.6.1 的实现

    private CloudBlockBlob GetBlobReference(string filePath, bool createContainerIfMissing = true)
    {
        CloudBlobClient client = _account.CreateCloudBlobClient();
        CloudBlobContainer container = client.GetContainerReference("my-container");

        if ( createContainerIfMissing && container.CreateIfNotExists())
        {
            //Public blobs allow for public access to the image via the URI
            //But first, make sure the blob exists
            container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
        }

        CloudBlockBlob blob = container.GetBlockBlobReference(filePath);

        return blob;
    }

    public bool Exists(String filepath)
    {
        var blob = GetBlobReference(filepath, false);
        return blob.Exists();
    }
于 2013-08-05T22:15:49.663 回答
4

使用ExistsAsyncCloudBlockBlob 的方法。

bool blobExists = await cloudBlobContainer.GetBlockBlobReference("<name of blob>").ExistsAsync();
于 2018-07-16T17:58:10.980 回答
2

使用Microsoft.WindowsAzure.Storage.Blob 版本 4.3.0.0,以下代码应该可以工作(此程序集的旧版本有很多重大更改):

使用容器/blob 名称和给定的 API(现在微软似乎已经实现了这个):

return _blobClient.GetContainerReference(containerName).GetBlockBlobReference(blobName).Exists();

使用 blob URI(解决方法):

  try 
  {
      CloudBlockBlob cb = (CloudBlockBlob) _blobClient.GetBlobReferenceFromServer(new Uri(url));
      cb.FetchAttributes();
  }
  catch (StorageException se)
  {
      if (se.Message.Contains("404") || se.Message.Contains("Not Found"))
      {
          return false;
      }
   }
   return true;

(如果 blob 不存在,获取属性将失败。脏,我知道 :)

于 2015-05-15T21:44:29.460 回答
1

使用新包Azure.Storage.Blobs

BlobServiceClient blobServiceClient = new BlobServiceClient("YourStorageConnectionString");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("YourContainerName");
BlobClient blobClient = containerClient.GetBlobClient("YourFileName");

然后检查是否存在

if (blobClient.Exists()){
 //your code
}
于 2020-02-19T22:05:00.197 回答
1

使用最新版本的SDK,您需要使用ExistsAsync方法,

public async Task<bool> FileExists(string fileName)
{
    return  await directory.GetBlockBlobReference(fileName).ExistsAsync();
}

这是代码示例

于 2020-06-02T02:56:35.017 回答
0
## dbutils.widgets.get to call the key-value from data bricks job

storage_account_name= dbutils.widgets.get("storage_account_name")
container_name= dbutils.widgets.get("container_name")
transcripts_path_intent= dbutils.widgets.get("transcripts_path_intent")
# Read azure blob access key from dbutils 
storage_account_access_key = dbutils.secrets.get(scope = "inteliserve-blob-storage-secret-scope", key = "storage-account-key")

from azure.storage.blob import BlockBlobService
block_blob_service = BlockBlobService(account_name=storage_account_name, account_key=storage_account_access_key)

def blob_exists():
        container_name2 = container_name
        blob_name = transcripts_path_intent
        exists=(block_blob_service.exists(container_name2, blob_name))
        return exists
blobstat = blob_exists()
print(blobstat)
于 2020-07-27T07:21:26.060 回答
0

这个完整的例子可以提供帮助。

public class TestBlobStorage
{

    public bool BlobExists(string containerName, string blobName)
    {
        BlobServiceClient blobServiceClient = new BlobServiceClient(@"<connection string here>");

        var container = blobServiceClient.GetBlobContainerClient(containerName);
        
        var blob = container.GetBlobClient(blobName);

        return blob.Exists();

    }

}

然后你可以在 main 中测试

    static void Main(string[] args)
    {

        TestBlobStorage t = new TestBlobStorage();

        Console.WriteLine("blob exists: {0}", t.BlobExists("image-test", "AE665.jpg")); 

        Console.WriteLine("--done--");
        Console.ReadLine();
    }

重要我发现文件名区分大小写

于 2021-12-02T22:06:41.240 回答