Luma Luis
Luma Luis

Reputation: 101

Is there a way to get all files from a blob of azure

I need to compare from a list that I have to the files in a blob storage of azure, the only part I need is a way to get a list of the files in that blob.

For example

blob azureImages
files:
name
something.jpg
asd.jpg
iwda.jpg
my list:
name
something.jpg
asd.jpg

When I compare the files from the blob with my list, I'd like to delete the files that have no match in my list.

Upvotes: 10

Views: 15991

Answers (1)

mfanto
mfanto

Reputation: 14418

You can get a list of blobs in a container with CloudBlobContainer.ListBlobs() or inside a directory with CloudBlobDirectory.ListBlobs()

CloudBlobClient blobClient = new CloudBlobClient(blobEndpoint, new StorageCredentialsAccountAndKey(accountName, accountKey));

//Get a reference to the container.
CloudBlobContainer container = blobClient.GetContainerReference("container");

//List blobs and directories in this container
var blobs = container.ListBlobs();

foreach (var blobItem in blobs)
{
    Console.WriteLine(blobItem.Uri);
}

You'll need to parse the file name from blobItem.Uri, but then you can use LINQ's Except() method to find the difference:

public string FindFilesToDelete(IEnumerable<string> fromAzure, IEnumerable<string> yourList)
{
     return fromAzure.Except(yourList);
}

which will return everything in the fromAzure list that isn't in yourList.

And lastly you can delete the blobs with this example

Upvotes: 22

Related Questions