C# 获取azure blob存储中的最后一个blob

C# 获取azure blob存储中的最后一个blob,c#,winforms,azure,C#,Winforms,Azure,我需要知道如何在blob存储中检索上次创建的blob。现在我使用for循环来实现它,但是目录中大约有50000个blockblob 如何高效地获取最后创建的块blob 我的代码如下: string url = null; foreach (var blobItem in subdir.ListBlobs()) { url = blobItem.Uri.ToString(); } url += signature; if (url != pictureBoxLiveViewer.Image

我需要知道如何在blob存储中检索上次创建的blob。现在我使用for循环来实现它,但是目录中大约有50000个blockblob

如何高效地获取最后创建的块blob

我的代码如下:

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

这是计时器中的一个函数,它每分钟都在滴答作响。因此这是低效的。

Blob存储不支持查询功能,因此您需要自己实现这一功能。一个明显的选择是您当前的操作方式,即列出blob并查找该列表中的最后一个blob

[大声思考]

您可以做的一件事是按时间倒序命名blob(方法类似于表中的PartitionKey)。所以你可以用这个逻辑来命名你的blob

var blobName = DateTime.MaxValue - DateTime.UtcNow
这样,当您列出blob时,您总是首先得到最新的blob


另一种选择是将blob名称、创建日期和blob URI保存在表存储中。同样,您可能希望对PartitionKey使用与上面相同的逻辑。然后您将查询表存储,从表中获取第一条记录,这将是您最近创建的blob。

我在上传应用程序中创建了一个函数,每次将最新图像的完整URI写入文本文件(txt)

这是我的另一个项目的计时器,它正在加载服务器的最后一个映像:

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