c# - Azure - 为列表 blob 调用存储休息 api

标签 c# rest azure

我想要做的是连接到 Azure 存储 Rest API 列表 Blob。引用:http://msdn.microsoft.com/en-us/library/windowsazure/dd135734.aspx

我尝试关注 http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx为了指定授权 header ,但是我收到 403 错误 - 禁止。

代码:

Uri address = new Uri("https://account.blob.core.windows.net/$logs?restype=container&comp=list");
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(address);
req.Headers["x-ms-date"] = "2013-09-04";
req.Headers["x-ms-version"] = "2012-02-12";
req.Method = "GET";

string StringToSign =  "GET\n"
    + "\n" // content encoding
    + "\n" // content language
    + "\n" // content length
    + "\n" // content md5
    + "\n" // content type
    + "\n" // date
    + "\n" // if modified since
    + "\n" // if match
    + "\n" // if none match
    + "\n" // if unmodified since
    + "\n" // range
    + "x-ms-date: 2013-09-04\nx-ms-version:2012-02-12\n" // headers
    + "/account/blob\ncomp:list\nrestype:container"; // resources

string accountName = "account";
string key = Convert.ToBase64String(Encoding.Default.GetBytes(StringToSign));
req.Headers["Authorization"] = string.Format("SharedKey {0}:{1}", accountName, key);

HttpWebResponse resp = req.GetResponse() as HttpWebResponse;

任何人都可以看到任何错误吗?有没有可以生成 key 的工具?我不确定的一件事是我是否正确编码/散列字符串。

谢谢, 安德鲁

更新为最新代码。这段代码给了我一个禁止错误。

DateTime dt = DateTime.UtcNow;
string StringToSign = "GET\n"
    + "\n" // content encoding
    + "\n" // content language
    + "\n" // content length
    + "\n" // content md5
    + "\n" // content type
    + "\n" // date
    + "\n" // if modified since
    + "\n" // if match
    + "\n" // if none match
    + "\n" // if unmodified since
    + "\n" // range
    + "x-ms-date: " + dt.ToString("R") + "\nx-ms-version:2012-02-12\n" // headers
    + "/account/$logs\ncomp:list\nrestype:container";

string auth = SignThis(StringToSign, "accountkey", "account");
string method = "GET";
string urlPath = "https://account.blob.core.windows.net/$logs?restype=container&comp=list";
Uri uri = new Uri(urlPath);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = method;
request.Headers.Add("x-ms-date", dt.ToString("R"));
request.Headers.Add("x-ms-version", "2012-02-12");
request.Headers.Add("Authorization", auth);

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
}

最佳答案

上面的代码存在一些问题。但在此之前,您首先需要的是存储帐户的 key 。您可以从 Windows Azure 门户获取它。单击门户中的存储帐户名称,然后单击“管理访问 key ”,如下面的屏幕截图所示:

enter image description here

现在讨论问题:

您创建授权 header 的方式不正确。为了创建授权 header ,您需要上面代码中的帐户名、帐户 key 和 StringToSign。试试这个代码:

private static String SignThis(String StringToSign, string Key, string Account)
        {
            String signature = string.Empty;
            byte[] unicodeKey = Convert.FromBase64String(Key);
            using (HMACSHA256 hmacSha256 = new HMACSHA256(unicodeKey))
            {
                Byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(canonicalizedString);
                signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
            }

            String authorizationHeader = String.Format(
                  CultureInfo.InvariantCulture,
                  "{0} {1}:{2}",
                  "SharedKey",
                  Account,
                  signature);

            return authorizationHeader;
        }

上面的函数将提供授权 header ,您需要将其作为授权传递。

我注意到的第二件事是,在 StringToSign 的代码中,您没有传递容器名称。所以你的 StringToSign 应该是:

string StringToSign =  "GET\n"
    + "\n" // content encoding
    + "\n" // content language
    + "\n" // content length
    + "\n" // content md5
    + "\n" // content type
    + "\n" // date
    + "\n" // if modified since
    + "\n" // if match
    + "\n" // if none match
    + "\n" // if unmodified since
    + "\n" // range
    + "x-ms-date: 2013-09-04\nx-ms-version:2012-02-12\n" // headers
    + "/account/$logs\ncomp:list\nrestype:container"; // resources 

您提到您对 Windows Azure 还很陌生。如果我可以建议的话 - 早期也有很多人完成了 REST API 的实现。请看看他们做了什么,而不是试图再次做同样的事情。您可能会发现这些链接很有用:

http://convective.wordpress.com/2010/08/18/examples-of-the-windows-azure-storage-services-rest-api/

http://azurestoragesamples.codeplex.com/ - 查看此项目中的 REST API 实现。

更新

这是工作代码(只需更改帐户名、 key 和容器名称)

static void ListContainers()
{
    string Account = "account";
    string Key = "key";
    string Container = "$logs";
    DateTime dt = DateTime.UtcNow;
    string StringToSign = String.Format("GET\n"
        + "\n" // content encoding
        + "\n" // content language
        + "\n" // content length
        + "\n" // content md5
        + "\n" // content type
        + "\n" // date
        + "\n" // if modified since
        + "\n" // if match
        + "\n" // if none match
        + "\n" // if unmodified since
        + "\n" // range
        + "x-ms-date:" + dt.ToString("R") + "\nx-ms-version:2012-02-12\n" // headers
        + "/{0}/{1}\ncomp:list\nrestype:container", Account, Container);

    string auth = SignThis(StringToSign, Key, Account);
    string method = "GET";
    string urlPath = string.Format("https://{0}.blob.core.windows.net/{1}?restype=container&comp=list", Account, Container);
    Uri uri = new Uri(urlPath);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = method;
    request.Headers.Add("x-ms-date", dt.ToString("R"));
    request.Headers.Add("x-ms-version", "2012-02-12");
    request.Headers.Add("Authorization", auth);

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
    }
}

希望这有帮助。

关于c# - Azure - 为列表 blob 调用存储休息 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18605316/

相关文章:

rest - Grails POST请求字符集编码

azure - 将具有专用终结点的 Azure 数据工厂连接到具有同一 VNet 中的另一个专用终结点的存储帐户

c# - 从具有相同列表子项的列表中查找最大 ID

c# - 正则表达式包括结果中应该是非捕获组的内容

c# - 获取调用线程

azure - Windows 服务不会在 azure 虚拟机规模集实例中自动启动

c# - 从远程路径获取FileInfo

c# - 我如何获得屏幕方向?

rest - Vuejs 中的异步和等待

java - Restful Jersey Web 服务可以接受所有类型的资源吗?