1

誰かが.net(c#またはvb.net)のラックスペースクラウドファイルのtempurl関数の例を提供できますか?

RackSpaceサイトのドキュメントは次の場所にあります: http ://docs.rackspacecloud.com/files/api/v1/cf-devguide/cf-devguide-20121130.pdf52 ページから。

RubyとPythonには例がありますが、移植に問題があります。する必要がある:

  • アカウントの一時URLメタデータキーを設定します
    • HMAC-SHA1(RFC 2104)を作成します
    • 一時URLを作成します
4

2 に答える 2

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 に答える
2

Athlectual の回答は現在の openstack.net NuGet パッケージには当てはまらないようですが、試行錯誤しながら最新バージョン (1.1.2.1) で動作させることができました。うまくいけば、これは他の人を助けるでしょう

私の場合、一時 URL メタデータ キーは既に設定されているため、設定する必要はなかったようです。アカウントの作成時にランダムに生成する必要があります。したがって、機能する一時 URL を取得するコードは次のとおりです。

Nb認証に Username と APIKey を使用しました。代わりに Password を使用できると思います。APIKey は、Rackspace Cloud Control Panel Web サイトのアカウントの詳細の下にあります。

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 に答える