正在检索blob文件名

本文关键字:文件名 blob 检索 | 更新日期: 2023-09-27 17:59:23

所以我正在尝试检索blob存储中的文件的详细信息。其想法是,客户要求将专门与他们相关的文档放在他们的门户网站上。

这是一次迁移,目前文件以以下格式列在网格中:

文件名,文件大小,文件类型,下载链接。

我遇到的问题是检索blob属性。

以下是我目前拥有的代码片段。

public void BindGridDocuments()
{
    var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString);
    var blobStorage = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobStorage.GetContainerReference("documents");
    var documentCollection = container.ListBlobs();
    foreach (var document in documentCollection)
    {
        string filename = document.Uri.ToString();
    }
}

正在检索blob文件名

试试这个代码。代码假定blob容器中的所有blob都属于block-blobs类型。

存储客户端库2.0:

        CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
        CloudBlobContainer blobContainer = storageAccount.CreateCloudBlobClient().GetContainerReference("images");
        var blobs = blobContainer.ListBlobs(null, true, BlobListingDetails.All).Cast<CloudBlockBlob>();
        foreach (var blockBlob in blobs)
        {
            Console.WriteLine("Name: " + blockBlob.Name);
            Console.WriteLine("Size: " + blockBlob.Properties.Length);
            Console.WriteLine("Content type: " + blockBlob.Properties.ContentType);
            Console.WriteLine("Download location: " + blockBlob.Uri);
            Console.WriteLine("=======================================");
        }

存储客户端库1.7:

        CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
        CloudBlobContainer blobContainer = storageAccount.CreateCloudBlobClient().GetContainerReference("images");
        var blobs = blobContainer.ListBlobs(new BlobRequestOptions()
            {
                BlobListingDetails = BlobListingDetails.All,
                UseFlatBlobListing = true,
            }).Cast<CloudBlockBlob>();
        foreach (var blockBlob in blobs)
        {
            Console.WriteLine("Name: " + blockBlob.Name);
            Console.WriteLine("Size: " + blockBlob.Properties.Length);
            Console.WriteLine("Content type: " + blockBlob.Properties.ContentType);
            Console.WriteLine("Download location: " + blockBlob.Uri);
            Console.WriteLine("=======================================");
        }