我的应用程序使用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)
这段代码将在一秒钟内运行一次,因此重要的是它将以有效的方式运行。
答案 0 :(得分:0)
如果在hashSet中转换列表怎么办?改变你的foreach,看看发生了什么
var hashSet = new HashSet<IListBlobItem>(subdir.ListBlobs(options).ToList());
string url = uri = hashSet.Last().Uri.ToString() + signature.Text;
hashSet更快搜索和查找。
答案 1 :(得分:0)
我在我的上传应用程序中创建了一个函数,每次在文本文件(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());
这是我的另一个项目的计时器正在加载服务器的最后一个图像:
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;
答案 2 :(得分:0)
我发现接受的答案令人困惑且解释不清楚。我的偏好如下:
要创建临时文件(即:它的名称为其提供上下文),我将使用Azure Blob Storage虚拟目录,并使用 Seconds Since Epoch 专门命名blob。
一个例子,假设你有一个带路径的目录&#34; SomeContainer / SomeFolder /&#34;。此目录中包含一个或多个文件,使用上面提到的 Seconds Since Epoch 命名。要获取&#34;最后&#34;或最新文件,请使用以下内容:
StorageCredentials cred = new StorageCredentials("{{YOUR ACCOUNT NAME}}", ""{{YOUR ACCOUNT KEY}}");
CloudStorageAccount storageAccount = new CloudStorageAccount(cred, true);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); //invoke the blob client
CloudBlobContainer container = blobClient.GetContainerReference("SomeContainer"); //get your container reference
CloudBlobDirectory directory = container.GetDirectoryReference("SomeFolder"); //resolve the directory
IListBlobItem latestBlobItem = directory.ListBlobs(true, BlobListingDetails.All).LastOrDefault(); //get the latest blobItem
CloudBlockBlob blockBlob = null;
if(latestBlobItem != null && !String.IsNullOrWhiteSpace(latestBlobItem.Uri.LocalPath))
{
//get the blob's local path
string fullLocalPath = latestBlobItem.Uri.LocalPath;
//remove container and leading slash
string localPath = fullLocalPath.Substring(fullLocalPath.IndexOf('/', 1) + 1);
//get your blob reference
blockBlob = container.GetBlockBlobReference(localPath);
}
从那里按照你的意愿使用块blob引用(下载等)
快乐的蠢事!