Search code examples
c#asp.netazureblobstorage

get last blobitem in azure container


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.


Solution

  • 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;