Rackspace Cloud Files(OpenStack Swift)在vb.net中的TempUrl示例

时间:2013-01-22 16:21:58

标签: rackspace openstack cloudfiles openstack-swift

任何人都可以在.net(c#或vb.net)中为rackspace云文件tempurl函数提供示例吗?

RackSpace网站上有文档: http://docs.rackspacecloud.com/files/api/v1/cf-devguide/cf-devguide-20121130.pdf 从第52页开始。

Ruby和Python中有一些例子,但我在移植它们时遇到了麻烦。我需要:

  • 设置帐户临时URL元数据密钥
    • 创建HMAC-SHA1(RFC 2104)
    • 创建临时网址

2 个答案:

答案 0 :(得分:2)

以下内容将使用C#生成临时URL:

        var account = new CF_Account(conn, client);

        string tempUrlKey = account.Metadata["temp-url-key"];

        //Set the link to expire after 60 seconds (in epoch time)
        int epochExpire = ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds) + 60;

        //The path to the cloud file
        string path = string.Format("{0}/{1}/{2}", account.StorageUrl.AbsolutePath, containerName, fileName);

        string hmacBody = string.Format("GET\n{0}\n{1}", epochExpire.ToString(), path);

        byte[] hashSaltBytes = Encoding.ASCII.GetBytes(tempUrlKey);

        string sig = null;

        using (HMACSHA1 myhmacMd5 = new HMACSHA1(hashSaltBytes))
        {
            byte[] ticketBytes = Encoding.ASCII.GetBytes(hmacBody);
            byte[] checksum = myhmacMd5.ComputeHash(ticketBytes);

            StringBuilder hexaHash = new StringBuilder();

            foreach (byte b in checksum)
            {
                hexaHash.Append(String.Format("{0:x2}", b));
            }

            sig = hexaHash.ToString();
        }

        string cloudFileUrl = string.Format("https://{0}{1}", account.StorageUrl.Host, path);

        //Compile the temporary URL
        string tempUrl = string.Format("{0}?temp_url_sig={1}&temp_url_expires={2}", cloudFileUrl, sig, epochExpire);

答案 1 :(得分:2)

知识分子的答复似乎不适用于当前的openstack.net NuGet软件包,但我已经设法通过反复试验使用最新版本(1.1.2.1)。希望这会有助于其他人

在我的情况下,似乎我没有设置临时URL元数据密钥,因为已经设置了一个,必须在创建帐户时随机生成。因此,获取有效的临时URL的代码如下所示。

N.b。我使用了Username和APIKey进行身份验证。我猜你可以用密码代替。可以在Rackspace云控制面板网站的帐户详细信息下找到APIKey。

private static string GetCloudFilesTempUrl(Uri storageUrl, string username, string apiKey, string containerName, string filename)
{
    var cloudIdentity = new CloudIdentity()
        {
            Username = username,
            APIKey = apiKey
        };

    var provider = new CloudFilesProvider(cloudIdentity);

    var accountMetaData = provider.GetAccountMetaData();
    var tempUrlKey = accountMetaData["Temp-Url-Key"];

    //Set the link to expire after 60 seconds (in epoch time)
    var epochExpire = ((int) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds) + 60;

    //The path to the cloud file
    var path = string.Format("{0}/{1}/{2}", storageUrl.AbsolutePath, containerName, filename);

    var hmacBody = string.Format("GET\n{0}\n{1}", epochExpire, path);

    var hashSaltBytes = Encoding.ASCII.GetBytes(tempUrlKey);

    string sig;

    using (var myhmacMd5 = new HMACSHA1(hashSaltBytes))
    {
        var ticketBytes = Encoding.ASCII.GetBytes(hmacBody);
        var checksum = myhmacMd5.ComputeHash(ticketBytes);

        var hexaHash = new StringBuilder();

        foreach (var b in checksum)
        {
            hexaHash.Append(String.Format("{0:x2}", b));
        }

        sig = hexaHash.ToString();
    }

    var cloudFileUrl = string.Format("https://{0}{1}", storageUrl.Host, path);

    //Compile the temporary URL
    return string.Format("{0}?temp_url_sig={1}&temp_url_expires={2}", cloudFileUrl, sig, epochExpire);
}

我不确定你是如何获取存储网址的,但是通过更多的试验和错误我设法了:

private static string GetCloudFilesStorageUrl(string username, string apiKey)
{
    var cloudIdentity = new CloudIdentity()
    {
        Username = username,
        APIKey = apiKey
    };

    var identityProvider = new CloudIdentityProvider(cloudIdentity);

    return identityProvider.GetUserAccess(cloudIdentity)
                            .ServiceCatalog
                            .Single(x => x.Name == "cloudFiles")
                            .Endpoints[0].PublicURL;
}

如上所述,我没有设置临时URL密钥,我可能不会尝试以防万一我打破了正常工作的东西!如果你需要,我猜这应该做的诀窍:

private static void SetCloudFilesTempUrlKey(string username, string apiKey, string tempUrlKey)
{
    var cloudIdentity = new CloudIdentity()
        {
            Username = username,
            APIKey = apiKey
        };

    var provider = new CloudFilesProvider(cloudIdentity);

    provider.UpdateAccountMetadata(new Metadata
        {
            { "Temp-Url-Key", tempUrlKey }
        });
}