访问Azure中的Blob属性

时间:2017-03-30 13:45:52

标签: c# azure azure-storage-blobs

我需要获取blob属性,因为有最后修改日期。我需要比较日期。已阅读了大量文章,并认为这是因为我没有使用CloudBlockBlob,但我无法从中提取属性。

到目前为止,我的代码返回了blob的名称:

public static void ListBlobsAnonymously()
    {
        //Get the blob from the URL - URL is in the app.config file so it can be changed easily should it need it. 
        CloudBlobContainer container = new CloudBlobContainer(new Uri(ConfigurationManager.AppSettings["VOURL"]));

        //For each of the blobs, write the file name out. This will be needed for a comparison. 
        foreach (IListBlobItem blobItem in container.ListBlobs())
        {
            string name = blobItem.Uri.Segments.Last();
            Console.WriteLine(name);
        }

        Console.ReadKey();
    }

这是blob的结构:

<EnumerationResults ServiceEndpoint="https://test.blob.core.windows.net/" ContainerName="downloads">
<Blobs>
    <Blob>
        <Name>
        This_is_a_test
        </Name>
        <Properties>
            <Last-Modified>Tue, 01 Oct 2010 14:33:48 GMT</Last-Modified>
            <Content-Length>452</Content-Length>
            <Content-Type>application/zip</Content-Type>
            <BlobType>BlockBlob</BlobType>
        </Properties>
    </Blob>
</Blobs>
</EnumerationResults>

这是我需要掌握的属性中的最后一次修改而不能。

1 个答案:

答案 0 :(得分:3)

试试这段代码:(注意:不支持/在.NET Core上工作)

        CloudBlobContainer container = new CloudBlobContainer(new Uri(ConfigurationManager.AppSettings["VOURL"]));

        //For each of the blobs, write the file name out. This will be needed for a comparison. 
        foreach (IListBlobItem blobItem in container.ListBlobs())
        {
            var blob = (CloudBlob)blobItem;
            if (blob != null)
            {
                string name = blobItem.Uri.Segments.Last();
                Console.WriteLine(name);
                Console.WriteLine(blob.Properties.LastModified);
            }
        }

        Console.ReadKey();

基本上你需要将IListBlobItem投射到CloudBlob,然后你就可以访问blob的属性了。