无法使ADLS Gen2 REST延续令牌起作用

时间:2019-05-13 17:44:33

标签: rest azure-data-lake

我正在尝试从ADLS Gen2检索文件和文件夹列表。我可以得到前5000个项目,但是当我使用延续来获取剩余的项目(大约17,000个项目)时,我会收到错误403(禁止使用)。根据文档,我将延续令牌添加到签名字符串中的URI和Canonicalized Resource中。但是,我无法使其正常工作。

我已经阅读了有关ADLS Gen2 REST调用的文档,以及在此上我能找到的任何内容,我无法弄清问题所在。

var date = System.DateTime.UtcNow.ToString("R");
string toSign = DefaultSignatureString(date);
toSign +=
    $"/{storageaccountname}/{filesystemname}" + "\n" +
    $"directory:{dir}" +"\n" +
    "recursive:true" + "\n" +
    "resource:filesystem";

var signedSignature = SignData(accessKey, toSign);
var uri = $"https://{storageaccountname}.dfs.core.windows.net/{filesystemname}?directory={dir}&recursive=true&resource=filesystem";

HttpWebResponse response = GetWebResponse(storageaccountname, date, signedSignature, uri);
var token_continuation = response.Headers["x-ms-continuation"];
//I get the token_continuation and repeat the previous steps, adding the continuation part:
while (token_continuation != null)
{
    date = System.DateTime.UtcNow.ToString("R");
    toSign = DefaultSignatureString(date);
    toSign +=
        $"/{storageaccountname}/{filesystemname}" + "\n" +
        $"continuation:{token_continuation}" + "\n" +
        $"directory:{dir}" + "\n" +
        "recursive:true" + "\n" +
        "resource:filesystem";

    signedSignature = SignData(accessKey, toSign);
    uri = $"https://{storageaccountname}.dfs.core.windows.net/{filesystemname}?directory={dir}&recursive=true&resource=filesystem&continuation={token_continuation}";
    response = GetWebResponse(storageaccountname, date, signedSignature, uri);
    token_continuation = response.Headers["x-ms-continuation"];
}


//this is my GetWebResponse method
private static HttpWebResponse GetWebResponse(string storageaccountname, string date, string signedSignature, string uri, string continuation = null)
    {
        WebRequest request = WebRequest.Create(uri);
        if (continuation != null)
        {
            request.Headers.Add($"x-ms-continuation:{continuation}");
        }
        request.Headers.Add($"x-ms-date:{date}");
        request.Headers.Add($"x-ms-version:2018-11-09");
        request.Headers.Add($"Authorization:SharedKey {storageaccountname}:{signedSignature}");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        return response;
    }

正如我所说,我得到了第一条答复;当我进入while循环时,我得到了错误。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

最后弄清楚了这一点:大多数情况下(如果不是全部的话)延续令牌都以“ ==”结尾返回,并且使uri混乱。对于uri,我逃避了;对于规范化的资源,我将字符串保留原样。