2013-01-22 21 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 } 
     }); 
} 
相关问题