Get the latest blobitem in an azure container

My application uses Microsoft Azure cloud block storage, and I'm looking for an alternative way to get the last item in a folder in a container.

Here's how it goes now:

CloudBlobClient blobClient = new CloudBlobClient("http://ferryjongmans.blob.core.windows.net/", new StorageCredentialsSharedAccessSignature(signature.Text)); CloudBlobContainer container = blobClient.GetContainerReference(cameraList.Items[Convert.ToInt32(Label1.Text)].ToString()); //Maak mooie datum notatie zoals : 01-01-2013 (standaard methode geeft in dit geval: 1-1-2013) string dag = DateTime.Now.Day.ToString(); if (dag.Length == 1) { string temp = dag; dag = "0" + temp; } string maand = DateTime.Now.Month.ToString(); if (maand.Length == 1) { string temp = maand; maand = "0" + temp; } //Complete datum (DD-MM-YYYY) string datum = dag + "-" + maand + "-" + DateTime.Now.Year.ToString(); CloudBlobDirectory direct = container.GetDirectoryReference(cameraList.Items[Convert.ToInt32(Label1.Text)].ToString()); CloudBlobDirectory subdir = direct.GetSubdirectory(datum); BlobRequestOptions options = new BlobRequestOptions(); options.UseFlatBlobListing = true; options.BlobListingDetails = BlobListingDetails.Snapshots; //maak string voor het tijdelijk oplaan van de uri string uri=""; //Ken steeds een waarde aan 'uri' toe om vervolgens wanneer de for loop klaar is //de laatste uri te krijgen. foreach (var blobItem in subdir.ListBlobs(options)) { uri = blobItem.Uri.ToString(); } string url = uri + signature.Text; if (url != pictureBox2.ImageUrl) { loadImage(url); } 

So, I iterate over the elements and every time I use the same string to assign the blob URI. When the loop is finished, my line has the URI of the last item in the directory.

I think I can do it more efficiently. There are a lot of drops in the catalog. (+ - 30000)

This piece of code will run once per second, so it is important that it works in an efficient manner.

+4
source share
3 answers

I created a function in the download application that will write every time the full URI of the newest image in a text file (txt)

  CloudBlobClient blobClient = new CloudBlobClient(sUrl, new StorageCredentialsSharedAccessSignature(signature)); CloudBlobContainer container = blobClient.GetContainerReference(container1); CloudBlobDirectory dir = container.GetDirectoryReference(cameranaam); CloudBlob cloudBlob = dir.GetBlobReference(cameranaam+".txt"); cloudBlob.UploadText(blobSAS.Uri.ToString()); 

And this is the timer of my other project, which loads the last server image:

  blobClient = new CloudBlobClient(blobstoreurl, new StorageCredentialsSharedAccessSignature(signature)); container = blobClient.GetContainerReference(containerName); CloudBlobDirectory dir = container.GetDirectoryReference(comboBoxCameras.SelectedItem.ToString()); CloudBlob cloudBlob = dir.GetBlobReference(comboBoxCameras.SelectedItem.ToString().Replace(" ","")+".txt"); pictureBoxLiveViewer.ImageLocation = cloudBlob.DownloadText()+signature; 
0
source

What if you convert your list to hashSet? change your foreach for this and see what happened

  var hashSet = new HashSet<IListBlobItem>(subdir.ListBlobs(options).ToList()); string url = uri = hashSet.Last().Uri.ToString() + signature.Text; 

hashSet is faster for searching and searching.

0
source

I believe that the accepted answer is confusing and poorly explained. My preferences are as follows:

To create a transition file (that is: this name gives it context), I use the Azure Blob virtual storage directories and name blob using Seconds from the era .

An example is if you have a directory with the path "SomeContainer / SomeFolder /". This directory contains one or more files named using the Seconds Since Epoch mentioned above. To get the "last" or last file, use the following:

 StorageCredentials cred = new StorageCredentials("{{YOUR ACCOUNT NAME}}", ""{{YOUR ACCOUNT KEY}}"); CloudStorageAccount storageAccount = new CloudStorageAccount(cred, true); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); //invoke the blob client CloudBlobContainer container = blobClient.GetContainerReference("SomeContainer"); //get your container reference CloudBlobDirectory directory = container.GetDirectoryReference("SomeFolder"); //resolve the directory IListBlobItem latestBlobItem = directory.ListBlobs(true, BlobListingDetails.All).LastOrDefault(); //get the latest blobItem CloudBlockBlob blockBlob = null; if(latestBlobItem != null && !String.IsNullOrWhiteSpace(latestBlobItem.Uri.LocalPath)) { //get the blob local path string fullLocalPath = latestBlobItem.Uri.LocalPath; //remove container and leading slash string localPath = fullLocalPath.Substring(fullLocalPath.IndexOf('/', 1) + 1); //get your blob reference blockBlob = container.GetBlockBlobReference(localPath); } 

From there, do as you want using the block block link (download, etc.).

Happy sob!

0
source

All Articles