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.
-
1I think this question is a duplicate of this one http://stackoverflow.com/questions/2642919/checking-if-a-blob-exists-in-azure-storage Check it. – Marco Staffoli Aug 30 '12 at 14:59
-
Does this answer your question? [Checking if a blob exists in Azure Storage](https://stackoverflow.com/questions/2642919/checking-if-a-blob-exists-in-azure-storage) – Michael Freidgeim Nov 05 '21 at 11:06
9 Answers
var blob = client.GetContainerReference(containerName).GetBlockBlobReference(blobFileName);
if (blob.Exists())
//do your stuff
-
13Try adding some context for your code to help the person asking the question. – Bjorn Sep 20 '13 at 21: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

- 45,581
- 7
- 87
- 126
-
Yes, but I don't think there is a different way to check for the existence of the blob. – Jakub Konecki Jun 14 '12 at 12:33
-
3Note: this doesn't work anymore with the new SDK. The other answers are the solution. – Tincan Aug 04 '14 at 09:37
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();
}

- 12,395
- 3
- 34
- 49
-
3
-
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. – Babak Naffas Jun 09 '14 at 17:04
-
I am getting the name with .ListBlobs() on the same BlobService instance, so it should not be visibility/permission problem. – d.popov Jun 10 '14 at 06:42
-
1@d.popov - I'm have the same problem here. Verified file does exist, but exists() is always returning false. Haven't solved the problem yet... – Steve May 15 '15 at 20:57
-
@BabakNaffas: I'll just say that the page where your link reference to, is not longer available. – H. Pauwelyn Mar 20 '16 at 10:49
-
Was this ever solved? Exists always returns "false" for me as well. – Adam Levitt Jul 12 '17 at 17:41
-
1@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. – Babak Naffas Jul 12 '17 at 18:51
-
@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? – Adam Levitt Jul 12 '17 at 23:18
-
1
-
@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. – Adam Levitt Jul 13 '17 at 15:31
-
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 projects – Adam Hardy Nov 22 '17 at 23:21
Use the ExistsAsync
method of CloudBlockBlob.
bool blobExists = await cloudBlobContainer.GetBlockBlobReference("<name of blob>").ExistsAsync();

- 1,219
- 15
- 16
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
}

- 697
- 8
- 9
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 :)

- 4,175
- 1
- 36
- 47
-
@Steve, does this help? what version of the Storage client are you using? – d.popov May 15 '15 at 21:48
-
`Microsoft.WindowsAzure.Storage.StorageException` has `RequestInformation.HttpStatusCode`. For non-existent blobs it will be HTTP/404. – Jari Turkia Feb 19 '20 at 14:40
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.

- 216,225
- 63
- 350
- 396
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

- 970
- 2
- 12
- 20
## 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)

- 468
- 4
- 10