【问题标题】:How to create azure file storage SAS url(without azure libraries)?如何创建 azure 文件存储 SAS url(没有 azure 库)?
【发布时间】:2017-08-22 08:37:08
【问题描述】:

我的主要目标是为文件创建一个 SAS url(没有 azure 库)。 我尝试使用 blob 创建 azure storage SAS,一切正常。当我尝试在 File 中执行相同操作时,出现错误。这是我的代码:

string azAccName = "AccountName";
string resource = "/upgfile/prt.png";
string endPoint = "https://" + azAccName + ".file.core.windows.net";
string uri = endPoint + resource;
string _now = DateTime.UtcNow.ToString("s") + "Z";    
string _noww = DateTime.UtcNow.AddHours(3).AddMinutes(5).ToString("s") + "Z";
string StorageKey = "xxx";

string signedpermissions = "r";
string signedstart = _now;//"2017-02-14"; //yyyy-mm--dd
string signedexpiry = _noww;// "2017-02-14";
string canonicalizedresource = "/file/" + azAccName + resource; //"/blob/myaccount/music/intro.mp3"
string signedidentifier = ""; //YWJjZGVmZw==
string signedIP = "";
string signedProtocol = "https";
string signedversion = "2015-02-21";
string rscc = "";  //Cache-Control
string rscd = "file; attachment";  //Content-Disposition               
string rsce = "";  //Content-Encoding
string rscl = "";  //Content-Language
string rsct = "binary";  //Content-Type      binary


string StringToSign = signedpermissions + "\n" +
               signedstart + "\n" +
               signedexpiry + "\n" +
               canonicalizedresource + "\n" +
               signedidentifier + "\n" +
               signedversion + "\n" +
               rscc + "\n" +
               rscd + "\n" +
               rsce + "\n" +
               rscl + "\n" +
               rsct;

HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(StorageKey));
string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(StringToSign)));

string link = String.Format("{0}?sv={1}&st={2}&se={3}&sr={4}&sp={5}&rscd={8}&rsct={9}&spr={6}&sig={7}",
                                        uri,
                                        signedversion,
                                        signedstart,
                                        signedexpiry,
                                        "c",   //b for blob
                                        signedpermissions,
                                        "https",
                                        signature.Replace("/", "%2"),
                                        rscd,///////////////
                                        rsct); 

我收到此错误。

<Error>
<Code>AuthenticationFailed</Code>
<Message>
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:92eda75a-001a-0072-501d-1bb6fd000000 Time:2017-08-22T08:03:58.6115733Z
</Message>
<AuthenticationErrorDetail>Signature fields not well formed.</AuthenticationErrorDetail>
</Error>

我有几乎相同的 blob 代码(细微差别)并且它可以正常工作。 有什么建议吗?

【问题讨论】:

    标签: file azure geturl


    【解决方案1】:

    我认为问题在于您的stringToSign 中缺少参数。您必须包含所有指定的参数here:

    StringToSign = signedpermissions + "\n" +  
                   signedstart + "\n" +  
                   signedexpiry + "\n" +  
                   canonicalizedresource + "\n" +  
                   signedidentifier + "\n" +  
                   signedIP + "\n" +  
                   signedProtocol + "\n" +  
                   signedversion + "\n" +  
                   rscc + "\n" +  
                   rscd + "\n" +  
                   rsce + "\n" +  
                   rscl + "\n" +  
                   rsct  
    

    如果您不使用参数(例如 signedIP 在您的情况下),则必须指定一个空行。

    基于此,您的StringToSign 应该是:

    string StringToSign = signedpermissions + "\n" +
                   signedstart + "\n" +
                   signedexpiry + "\n" +
                   canonicalizedresource + "\n" +
                   signedidentifier + "\n" +
                   "\n" + //For signed IP
                   "\n" + //For signed Protocol
                   signedversion + "\n" +
                   rscc + "\n" +
                   rscd + "\n" +
                   rsce + "\n" +
                   rscl + "\n" +
                   rsct;
    

    此外,link 中的 sr(签名资源类型)应该是 f(用于文件)而不是您正在使用的 c

    【讨论】:

      猜你喜欢
      • 2015-01-29
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 2022-07-19
      • 1970-01-01
      • 2017-03-07
      • 2015-07-11
      • 1970-01-01
      相关资源
      最近更新 更多