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