4

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

这段代码将在一秒钟内运行一次,因此它以有效的方式运行很重要。

4

3 回答 3

0

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

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

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

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

于 2013-02-19T21:35:26.453 回答
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;
于 2013-02-20T14:27:29.217 回答
0

我发现接受的答案令人困惑且解释不清。我的偏好如下:

要创建一个临时文件(即:它的名称给出了上下文),我使用 Azure Blob 存储虚拟目录并使用Seconds since Epoch专门命名该 blob 。

一个例子,假设你有一个路径为“SomeContainer/SomeFolder/”的目录。该目录中有一个或多个文件,使用上面提到的自纪元以来的秒数命名。要获取“最后一个”或最新文件,请使用以下命令:

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 参考(下载等)

快乐滴!

于 2016-02-17T17:10:03.503 回答