我的应用程序使用 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/