![](/img/trans.png)
[英]while uploading a cloudfile in rackspace cloud using openstack.net i get net.openstack.Core.Exceptions.Response.BadServiceRequestException
[英]Rackspace Cloud Files (OpenStack Swift) TempUrl Example in vb.net
任何人都可以在.net(c#或vb.net)中提供架空雲文件tempurl功能的示例嗎?
RackSpace站點上有文檔: http ://docs.rackspacecloud.com/files/api/v1/cf-devguide/cf-devguide-20121130.pdf,從第52頁開始。
Ruby和Python中有一些例子,但我在移植它們時遇到了麻煩。 我需要:
以下將使用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);
知識分子的答復似乎不適用於當前的openstack.net NuGet軟件包,但我已經設法通過反復試驗使用最新版本(1.1.2.1)。 希望這會有助於其他人
在我的情況下,似乎我沒有設置臨時URL元數據密鑰,因為已經設置了一個,必須在創建帳戶時隨機生成。 因此,獲取有效URL的代碼如下所示。
Nb我使用了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);
}
我不確定你是如何獲取存儲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 }
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.