Search code examples
c#cloudfiles

How to get date modified of a file in cloud files?


How to get date modified of a file in cloud files?

I am using the .net SDK from cloud files (not the rack space nu get package).

I can get a list of my files and call GetStorageItemInformation size but I want to know when the file was put onto cloud files. If I use the Cloudberry explorer app I see it has the information.

Is it in the .net SDK and where?


Solution

  • When iterating over the files in your container, you can use OpenStack.NET's ContainerObject.LastModified. Here is a console application which lists all containers in a region and their files with the last modified timestamp.

    using System;
    using net.openstack.Core.Domain;
    using net.openstack.Providers.Rackspace;
    
    namespace CloudFilesDateModified
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string region = "DFW";
                var identity = new CloudIdentity { Username = "username", APIKey = "apikey" };
    
                var cloudfiles = new CloudFilesProvider(identity);
                foreach (Container container in cloudfiles.ListContainers(region:region))
                {
                    Console.WriteLine($"Container: {container.Name}");
    
                    foreach (ContainerObject file in cloudfiles.ListObjects(container.Name, region: region))
                    {
                        Console.WriteLine($"\t{file.Name} - {file.LastModified}");
                    }
                }
    
                Console.ReadLine();
    
            }
        }
    }
    

    Below is some sample output

    Container: test
        foobar - 10/12/2015 2:00:26 PM -06:00
        foobar/file.png - 11/6/2015 7:34:42 PM -06:00
        foobar/index.html - 11/6/2015 7:34:31 PM -06:00
    

    If your container has more than 10,000 files, you will need to use the paging parameters to loop through all the files. In the example below, I am paging through the results 100 at a time.

    foreach (Container container in cloudfiles.ListContainers(region: region))
    {
        Console.WriteLine($"Container: {container.Name}");
    
        int limit = 100;
        string lastFileName = null;
        IEnumerable<ContainerObject> results;
        do
        {
            results = cloudfiles.ListObjects(container.Name, region: region, limit: limit, marker: lastFileName);
            foreach (ContainerObject file in results)
            {
                Console.WriteLine($"\t{file.Name} - {file.LastModified}");
                lastFileName = file.Name;
            }
        } while (results.Any());
    }