Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

blob

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.

like image 309
Luma Luis Avatar asked Oct 15 '12 18:10

Luma Luis


1 Answers

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

like image 118
mfanto Avatar answered Oct 14 '22 18:10

mfanto