2013-01-22 22 views

回答

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

Athlectual的回覆似乎並不適用於當前的openstack.net NuGet包,但我試圖通過反覆試驗來使其與最新版本(1.1.2.1)一起工作。希望這會幫助其他人

就我而言,似乎我沒有設置臨時URL元數據密鑰,因爲已經設置了Temp URL元數據密鑰,必須在創建帳戶時隨機生成。因此,獲取工作的臨時URL的代碼如下所示。

N.b.我已經使用用戶名和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); 
} 

我不知道你是如何打算讓存儲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 } 
     }); 
} 
相關問題