简体   繁体   English

Rackspace Cloud Files(OpenStack Swift)在vb.net中的TempUrl示例

[英]Rackspace Cloud Files (OpenStack Swift) TempUrl Example in vb.net

Can anyone provide an example for rackspace cloud files tempurl function in .net (c# or vb.net)? 任何人都可以在.net(c#或vb.net)中提供架空云文件tempurl功能的示例吗?

There is documentation at the RackSpace site at: http://docs.rackspacecloud.com/files/api/v1/cf-devguide/cf-devguide-20121130.pdf starting on page 52. RackSpace站点上有文档: http ://docs.rackspacecloud.com/files/api/v1/cf-devguide/cf-devguide-20121130.pdf,从第52页开始。

There are examples in Ruby and Python but I'm having trouble porting them. Ruby和Python中有一些例子,但我在移植它们时遇到了麻烦。 I need to: 我需要:

  • Set the account Temp URL Metadata Key 设置帐户Temp URL元数据密钥
    • Create the HMAC-SHA1 (RFC 2104) 创建HMAC-SHA1(RFC 2104)
    • Create the temp url 创建临时网址

The following will generate a temporary URL with C#: 以下将使用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);

Athlectual's reply doesn't seem to apply to the current openstack.net NuGet package, but I've managed to get it working with the latest version (1.1.2.1) through trial and error. 知识分子的答复似乎不适用于当前的openstack.net NuGet软件包,但我已经设法通过反复试验使用最新版本(1.1.2.1)。 Hopefully this will help others 希望这会有助于其他人

In my case it seems I didn't have to set the Temp URL Metadata Key as one was already set, must be randomly generated when the account was created. 在我的情况下,似乎我没有设置临时URL元数据密钥,因为已经设置了一个,必须在创建帐户时随机生成。 So the code to get the temp URL that works is as follows. 因此,获取有效URL的代码如下所示。

Nb I've used Username and APIKey to authenticate. Nb我使用了Username和APIKey进行身份验证。 I guess you can use Password instead. 我猜你可以用密码代替。 APIKey can be found under your account details in the Rackspace Cloud Control Panel website. 可以在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);
}

I'm not sure how you're meant to get the storage URL, but through more trial and error I managed this: 我不确定你是如何获取存储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;
}

As mentioned I didn't have to set the Temp URL Key, and I'm probably not going to try in case I break something that's working! 如上所述,我没有设置临时URL密钥,我可能不会尝试以防万一我打破了有效的东西! If you need to though I'd guess that the following should do the trick: 如果你需要,虽然我猜这应该做的伎俩:

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.

相关问题 在使用openstack.net在机架空间云中上传云文件时,我得到了net.openstack.Core.Exceptions.Response.BadServiceRequestException - while uploading a cloudfile in rackspace cloud using openstack.net i get net.openstack.Core.Exceptions.Response.BadServiceRequestException 如何使用.net将文件上传到RackSpace Cloud文件? - How to upload files to RackSpace Cloud Files using .net? 将文件从Rackspace CloudSite传输到云文件 - Transfer Files from Rackspace CloudSite to Cloud Files Rackspace云文件:使用PHP将图像上传到Rackspace云文件 - Rackspace cloud files: Image Upload to rackspace cloud files using PHP 图像使用PHP上传到rackspace云文件 - Image Upload to rackspace cloud files using PHP Rackspace Cloud Files API:404错误 - Rackspace Cloud Files API: 404 Error 用于检索Rackspace云文件容器(大)内容的脚本? - Script to retrieve the (large) contents of a Rackspace cloud files container? 如何使用Windows服务将文件上传到机架云 - how to upload files to rackspace cloud using windows services 使用ASP.net连接到Rackspace Cloud - Connecting to Rackspace Cloud using ASP.net Rackspace云文件REST API莫名其妙地收到错误的请求响应 - Rackspace cloud files REST api inexplicably getting bad request response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM