1

谁能提供.net(c#或vb.net)中机架空间云文件tempurl函数的示例?

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

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

  • 设置账户 Temp URL Metadata Key
    • 创建 HMAC-SHA1 (RFC 2104)
    • 创建临时网址
4

2 回答 2

2

Athlectual 的回复似乎不适用于当前的 openstack.net NuGet 包,但我已经通过反复试验设法让它与最新版本 (1.1.2.1) 一起使用。希望这对其他人有帮助

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

注意我已经使用用户名和 APIKey 进行身份验证我想您可以改用密码。APIKey 可以在 Rackspace 云控制面板网站的帐户详细信息下找到。

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);
}

我不确定您打算如何获取存储 URL,但通过更多的试验和错误,我设法做到了这一点:

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 }
        });
}
于 2013-07-01T15:28:25.937 回答
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);
于 2013-03-26T11:48:07.643 回答