简体   繁体   English

获取Azure Blob存储中的最后一个Blob

[英]Get last blob in azure blob storage

I need to know how I can retrieve the last created blob in my blob storage. 我需要知道如何检索我的Blob存储中最后创建的Blob。 Now I'm doing it with a for loop, but there are approximately 50000 blockblobs in the directory. 现在,我使用for循环来执行此操作,但是目录中大约有50000个

How can I get the last created block blob on a efficient way? 如何有效地获取最后创建的块Blob?

My code is as follows: 我的代码如下:

string url = null;
foreach (var blobItem in subdir.ListBlobs())
{
    url = blobItem.Uri.ToString();
}
url += signature;
if (url != pictureBoxLiveViewer.ImageLocation)
{
    pictureBoxLiveViewer.ImageLocation = url;
}

This is a function in a timer who's ticking every minute. 这是计时器中每分钟滴答滴答的功能。 So this is inefficient. 因此,这是低效的。

Blob storage does not support querying capability so you would need to implement this on your own. Blob存储不支持查询功能,因此您需要自己实现。 One obvious option is the way you're currently doing it where you list the blobs and find the last blob in that list. 一个明显的选择是您当前的操作方式,在该操作中您列出了blob,并在该列表中找到了最后一个blob。

[Thinking out loud] [把想法大声说出来]

One thing you could do is name the blobs in reverse chronological order (approach similar to PartitionKey in tables). 您可以做的一件事是按相反的时间顺序命名blob (方法类似于表中的PartitionKey)。 So what you could do is name your blob using this logic 因此,您可以使用此逻辑命名Blob

var blobName = DateTime.MaxValue - DateTime.UtcNow

That way when you list the blob, you'll always get the latest blob first. 这样,当您列出Blob时,您将始终始终首先获得最新的Blob。

Another alternative could be to save the blob name, the creation date, and the blob URI in Table storage. 另一种选择是将Blob名称,创建日期和Blob URI保存在表存储中。 Again, you may want to use the same logic as above for PartitionKey. 同样,您可能希望对PartitionKey使用与上述相同的逻辑。 Then you would query the table storage, get the 1st record from the table and that would be your most recently created blob. 然后,您将查询表存储,从表中获取第一条记录,这将是您最近创建的Blob。

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) 我在上载应用程序中创建了一个函数,该函数将每次在文本文件(txt)中写入最新图像的完整URI。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM