Search code examples
c#.netapirackspacecloudfiles

Rackspace Cloud Files Get Objects In Container C#


I have been looking at the documentation, testing examples and getting familiar with the Rackspace Cloud Files API. I got the basic code down, got the API key, got a username, the basic stuff. Now one thing confuses me, this problem is something that appears to be not well documented.

So what I am trying to do is to get all the objects, including folders and files in a container. I have tried this example:

IEnumerable<ContainerObject> containerObjects = cloudFilesProvider.ListObjects("storage",null,null,null,null,null,false,ci);

foreach(ContainerObject containerObject in containerObjects)
{
    MessageBox.Show(containerObject.ToString());
}

This doesn't return the result I am looking for, it seems to not return anything.

I am using OpenStack provided by running this in the NuGet console: Install-Package Rackspace.

I am trying to create a file backup program for me and my family.


Solution

  • A common problem is not specifying the region. When your Rackspace cloud account's default region is not the same region as where your container lives, then you must specify region, otherwise you won't see any results listed. Which is quite misleading...

    Here is a sample console application which prints the contents of all your containers (and their objects) in a particular region. Change "DFW" to the name of the region where your container lives. If you aren't sure which region has your container, log into the Rackspace cloud control panel and go to "Storage > Files" and note the region drop down at the top right.

    using System;
    using net.openstack.Core.Domain;
    using net.openstack.Providers.Rackspace;
    
    namespace ListCloudFiles
    {
        class Program
        {
            static void Main()
            {
                var region = "DFW";
                var identity = new CloudIdentity
                {
                    Username = "username",
                    APIKey = "apikey"
                };
    
                var cloudFilesProvider = new CloudFilesProvider(identity);
    
                foreach (Container container in cloudFilesProvider.ListContainers(region: region))
                {
                    Console.WriteLine(container.Name);
    
                    foreach (ContainerObject obj in cloudFilesProvider.ListObjects(container.Name, region: region))
                    {
                        Console.WriteLine(" * " + obj.Name);
                    }
                }
                Console.ReadLine();
            }
        }
    }