[英]How to create/upload to sub containers with Rackspace?
How do you create sub containers (directory) and upload them using Rackspace OpenNetStack SDK ? 如何创建子容器(目录)并使用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. 我尝试在创建子容器时添加
"\\"
,但是实际上创建了一个名为folder\\subfolder
的容器,因为在OpenNetStack SDK中找不到如何添加子容器的任何地方。 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? 有谁知道另一个允许创建/上传到子容器的Rackspace库?
You are very close! 你很亲密! The trick is to put a URL path separator,
/
, in the object name, rather than in the container name. 技巧是在对象名称而不是容器名称中放置URL路径分隔符
/
。 This is how the OpenStack ObjectStorage API works, and is not specific to the .NET SDK or Rackspace. 这就是OpenStack ObjectStorage API的工作方式,并不特定于.NET SDK或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
. 在下面的示例控制台应用程序中,我创建一个
images
容器,然后通过将文件命名为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
. 打印出的文件的公共URL就是容器的公共URL +文件名或
http://abc123.r27.cf1.rackcdn.com/thumbnails/logo.png
。 The container URL will be unique to each container and user. 容器URL对于每个容器和用户都是唯一的。
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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.