How to check if Azure Blob file Exists or Not
Asked Answered
S

9

28

I want to check a particular file exist in Azure Blob Storage. Is it possible to check by specifying it's file name? Each time i got File Not Found Error.

Shirashirah answered 14/6, 2012 at 12:7 Comment(2)
I think this question is a duplicate of this one #2643419 Check it.Barto
Does this answer your question? Checking if a blob exists in Azure StorageJulesjuley
E
18

This extension method should help you:

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;
            }
        }
    }
}

Usage:

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

Estreat answered 14/6, 2012 at 12:13 Comment(2)
Yes, but I don't think there is a different way to check for the existence of the blob.Estreat
Note: this doesn't work anymore with the new SDK. The other answers are the solution.Jeff
F
40
var blob = client.GetContainerReference(containerName).GetBlockBlobReference(blobFileName);

if (blob.Exists())
 //do your stuff
Fractionate answered 20/9, 2013 at 20:55 Comment(1)
Try adding some context for your code to help the person asking the question.Antung
E
18

This extension method should help you:

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;
            }
        }
    }
}

Usage:

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

Estreat answered 14/6, 2012 at 12:13 Comment(2)
Yes, but I don't think there is a different way to check for the existence of the blob.Estreat
Note: this doesn't work anymore with the new SDK. The other answers are the solution.Jeff
F
14

With the updated SDK, once you have the CloudBlobReference you can call Exists() on your reference.

UPDATE

The relevant documentation has been moved to https://learn.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_

My implementation using 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();
    }
Filemon answered 5/8, 2013 at 22:15 Comment(11)
but it always return false. Anyone having the same problem?Covington
Verify the visibility of your bucket and the file itself. If it's publicly visible, try accessing it via a browser. Could be your URI is incorrect.Filemon
I am getting the name with .ListBlobs() on the same BlobService instance, so it should not be visibility/permission problem.Covington
@Covington - I'm have the same problem here. Verified file does exist, but exists() is always returning false. Haven't solved the problem yet...Jurist
@BabakNaffas: I'll just say that the page where your link reference to, is not longer available.Edessa
Was this ever solved? Exists always returns "false" for me as well.Wingding
@AdamLevitt I have updated the answer with my implementation using the Exists method. This has been working for me in production for a couple years.Filemon
@BabakNaffas, thanks for the update. My code is basically the same, but I added the line here per your recommendation to call the .SetPermissions() method as you did above, but that does not seem to be changing the behavior. I'm still getting a return value of "false" from the .Exists() method. Perhaps there's something else I'm missing?Wingding
@AdamLevitt make sure it's not a casing issue.Filemon
@BabakNaffas - thanks for your continued replies. It's not a casing issue. Just triple checked that. I'm definitely still getting a false for exists. It's very strange, and surprisingly that it's this difficult to get this to work.Wingding
NOTE: As of Microsoft.WindowsAzure.Storage version 8.1.4.0 (.Net Framework v4.6.2) The Exists() method doesn't exist in favour of ExistsAsync() Which is the version that will install for .NetCore projectsOxime
M
6

Using the new package Azure.Storage.Blobs

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

then check if exists

if (blobClient.Exists()){
 //your code
}
Medallion answered 19/2, 2020 at 22:5 Comment(0)
D
5

Use the ExistsAsync method of CloudBlockBlob.

bool blobExists = await cloudBlobContainer.GetBlockBlobReference("<name of blob>").ExistsAsync();
Disband answered 16/7, 2018 at 17:58 Comment(0)
C
3

Using Microsoft.WindowsAzure.Storage.Blob version 4.3.0.0, the following code should work (there are a lot of breaking changes with older versions of this assembly):

Using container/blob name, and the given API (seems now Microsoft have actualy implemented this):

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

Using blob URI (workaround):

  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;

(Fetch attributes will fail if the blob is not existing. Dirty, I know :)

Covington answered 15/5, 2015 at 21:44 Comment(2)
@Steve, does this help? what version of the Storage client are you using?Covington
Microsoft.WindowsAzure.Storage.StorageException has RequestInformation.HttpStatusCode. For non-existent blobs it will be HTTP/404.Obala
R
2

With the latest version of SDK, you need to use the ExistsAsync Method,

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

Here is the code sample.

Retired answered 2/6, 2020 at 2:56 Comment(0)
B
2

This complete example is here to help.

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();

    }

}

then you can test in 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();
    }

Important I found the file names are case sensitive

Biennial answered 2/12, 2021 at 22:6 Comment(0)
M
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)
Marshall answered 27/7, 2020 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.