SAS Azure Signature did not match

前端 未结 1 1117
执笔经年
执笔经年 2021-01-15 17:01

I try to create a SAS to a blob on azure storage in php. I write the following code:

$key =\"...\";

$end = date(\'Y-m-d\\TH\\:i\\:s\\Z\', strtotime(\'+1 day         


        
相关标签:
1条回答
  • 2021-01-15 17:31

    It seems that you are trying to generate an account SAS token, as the second example described at https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1#examples-of-sas-uris. Per my understanding, you can only generate a common blob SAS token as the first example mentioned at above article.

    Meanwhile, according to the description of Constructing the Signature String, you missed several parts when generating the signature.

    So, please try the following code snippet:

    function getSASForBlob($accountName, $container, $blob, $permissions ,$expiry, $key){
     /* Create the signature */
     $_arraysign = array();
     $_arraysign[] = $permissions;
     $_arraysign[] = '';
     $_arraysign[] = $expiry;
     $_arraysign[] = '/blob' .'/'.$accountName . '/' . $container . '/' . $blob;
     $_arraysign[] = '';
     $_arraysign[] = '';
     $_arraysign[] = '';
     $_arraysign[] = "2015-12-11"; //the API version is now required 
     $_arraysign[] = '';
     $_arraysign[] = '';
     $_arraysign[] = '';
     $_arraysign[] = '';
     $_arraysign[] = '';
    
     $_str2sign = implode("\n", $_arraysign);
    
     return base64_encode(hash_hmac('sha256', urldecode(utf8_encode($_str2sign)), base64_decode($key), true));
    }
    
    function getBlobUrl($accountName, $container, $blob, $resourceType, $permissions, $expiry, $_signature){
     /* Create the signed query part */
    
     $_parts = array();
        $_parts[] = (!empty($expiry)) ? 'se=' . urlencode($expiry) : '';
        $_parts[] = 'sr=' . $resourceType;
        $_parts[] = (!empty($permissions)) ? 'sp=' . $permissions : '';
        $_parts[] = 'sig=' . urlencode($_signature);
        $_parts[] = 'sv=2015-12-11';
        $_parts[] = 'rscd=';
    
    
     /* Create the signed blob URL */
     $_url = 'https://'
     .$accountName.'.blob.core.windows.net/'
     . $container . '/'
     . $blob . '?'
     . implode('&', $_parts);
    
     return $_url;
     }
    
    $sig = getSASForBlob(AZURE_ACC_NAME,AZURE_CONTAINER, BLOB, "r", $endDate, AZURE_PRIMARY_KEY);
    $url = getBlobUrl(AZURE_ACC_NAME,AZURE_CONTAINER,BLOB,"b","r", $endDate, $sig);
    
    0 讨论(0)
提交回复
热议问题