Question

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

This is 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'm looping through the items and use every time the same string to assign the URI of the blob. When the loop is finished, my string has the URI of the last item in the directory.

I think i can do this on a more efficiency way. The directory's have lots of blobs in it. (+- 30000)

This piece of code will be run once in a second, so its important it will be run on a efficient way.

Was it helpful?

Solution 2

I have created a function in my upload 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 who is loading the last image of the server:

            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;

OTHER TIPS

What if you convert your list in a hashSet? change your foreach for this and see what happend

 var hashSet = new HashSet<IListBlobItem>(subdir.ListBlobs(options).ToList());

 string url = uri = hashSet.Last().Uri.ToString() + signature.Text;

hashSet are faster for search and find.

I find the accepted answer confusing and poorly explained. My preference is as follows:

To create a transient file (ie: it's name gives it context) I make use of Azure Blob Storage Virtual Directories and name the blob specifically using Seconds Since Epoch.

An example, assuming you have a directory with path "SomeContainer/SomeFolder/". This directory has one or more files in it, named using the Seconds Since Epoch mentioned above. To fetch the "last" or latest 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's 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 wish with the block blob reference (download etc.)

Happy blobbing!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top