c# - 获取 azure 容器中的最后一个blobitem

标签 c# asp.net azure blobstorage

我的应用程序使用 Microsoft Azure 云 blob 存储,我正在寻找一种替代方法来获取容器中文件夹中的最后一项。

现在的情况是这样的:

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

因此,我循环遍历这些项目,并每次都使用相同的字符串来分配 blob 的 URI。 当循环完成时,我的字符串具有目录中最后一项的 URI。

我认为我可以以更有效的方式做到这一点。该目录中有很多 blob。 (+- 30000)

这段代码每秒运行一次,因此以有效的方式运行很重要。

最佳答案

如果将列表转换为 hashSet 会怎样?为此更改你的 foreach 并看看发生了什么

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

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

hashSet 的搜索和查找速度更快。

关于c# - 获取 azure 容器中的最后一个blobitem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14961793/

相关文章:

c# - 使用 FormCollection 中的数据发出异步 HttpClient 发布请求

c# - 有人能告诉我为什么我的标题没有出现在 asp.net c# 中吗?

c# - 用户名认证网络服务

azure - 使用 Azure AD B2C 指定 returnUrl

javascript - 获取 Azure 服务总线的详细信息

c# - BackgroundMediaPlayer.MessageReceivedFromBackground 不工作

c# - ASP.NET 核心 : Accessing appsettings from another project in solution

c# - 如何对一系列数字使用 FindByValue

c# - 不支持流读取器异常 URI 格式

c# - 如何将变量定义为方法?