Keith
Keith

Reputation: 1038

Access Blob Properties in Azure

I need to get the blob properties, in there is a last modified date. I need the date for comparison. Have read a number of articles, and thought it was because I was not using a CloudBlockBlob, but I cannot extract the properties from it.

My code so far returns the names of the blobs:

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();
    }

This is the structure of the blobs:

<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>

It is the last modified within the properties that I need to get hold of and cannot.

Upvotes: 2

Views: 3966

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136356

Try this code: (note: not supported/working on 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();

Basically you would need to cast IListBlobItem to CloudBlob and there you will have access to blob's properties.

Upvotes: 5

Related Questions