1

How do you create sub containers (directory) and upload them using Rackspace OpenNetStack SDK? I've tried adding "\" when creating a sub container however it actually creates a container with the name folder\subfolder because I cannot find anywhere in the OpenNetStack SDK on how to add a sub container. Even so, creating the sub container(s) manually wouldn't be too difficult.. but what about uploading to them?

Does anyone know of another Rackspace Library that would allow creating/uploading to sub containers?

NuWin
  • 276
  • 5
  • 15
  • Have you tried [forward slashes instead of backslashes](https://developer.rackspace.com/docs/cloud-files/v1/developer-guide/#document-pseudo-hierarchical-folders-and-directories)? – stuartd Aug 08 '16 at 22:38
  • Yes, however it will throw an error. "Container contains "/"" – NuWin Aug 08 '16 at 22:40

1 Answers1

2

You are very close! The trick is to put a URL path separator, /, in the object name, rather than in the container name. This is how the OpenStack ObjectStorage API works, and is not specific to the .NET SDK or Rackspace.

In the example console application below, I create a container, images, and then add a file to a subdirectory in that container by naming it thumbnails/logo.png. The resulting public URL for the file is printed out, and is essentially the container's public URL + the file name or http://abc123.r27.cf1.rackcdn.com/thumbnails/logo.png. The container URL will be unique to each container and user.

using System;
using net.openstack.Core.Domain;
using net.openstack.Providers.Rackspace;

namespace CloudFileSubdirectories
{
    public class Program
    {
        public static void Main()
        {
            // Authenticate
            const string region = "DFW";
            var user = new CloudIdentity
            {
                Username = "username",
                APIKey = "apikey"
            };
            var cloudfiles = new CloudFilesProvider(user);

            // Create a container
            cloudfiles.CreateContainer("images", region: region);

            // Make the container publically accessible
            long ttl = (long)TimeSpan.FromMinutes(15).TotalSeconds;
            cloudfiles.EnableCDNOnContainer("images", ttl, region);
            var cdnInfo = cloudfiles.GetContainerCDNHeader("images", region);
            string containerPrefix = cdnInfo.CDNUri;

            // Upload a file to a "subdirectory" in the container
            cloudfiles.CreateObjectFromFile("images", @"C:\tiny-logo.png", "thumbnails/logo.png", region: region);

            // Print out the URL of the file
            Console.WriteLine($"Uploaded to {containerPrefix}/thumbnails/logo.png");
            // Uploaded to http://abc123.r27.cf1.rackcdn.com/thumbnails/logo.png
        }
    }
}
  • 1
    Thank you for pointing out that there isn't an example of this for OpenStack.NET. I have added it to the wiki: https://github.com/openstacknetsdk/openstack.net/wiki/Rackspace-Cloud-Files-Code-Samples#create-subdirectories-in-a-container – Carolyn Van Slyck Aug 09 '16 at 13:18
  • This is awesome, I really appreciate it! – NuWin Aug 09 '16 at 16:24