适用于PHP的Microsoft Azure和SAS [英] Microsoft Azure and SAS for PHP

查看:75
本文介绍了适用于PHP的Microsoft Azure和SAS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用PHP创建到blob资源的SAS链接.不幸的是,当前在azure SDK中没有创建SAS签名的方法. 我写了一个生成SAS的代码,但是当我试图通过此方法生成的链接获取资源时,我收到以下消息:签名字段格式不正确.

    public function getSharedAccessSignatureURL($container, $blob)
{
    $signedStart = date('c', strtotime('-1 day'));
    $signedExpiry = date('c', strtotime('+1 day'));
    $signedResource = 'b';
    $signedPermission = 'r';
    $signedIdentifier = '';
    $responseContent = "file; attachment";
    $responseType = "binary";

    $canonicalizedResource = '/'.$this->account['accountName'].'/'.$container.'/'.$blob;
    $signedVersion = '2014-02-14';

    $stringToSign =
        $signedPermission."\n".
        $signedStart."\n".
        $signedExpiry."\n".
        $canonicalizedResource."\n".
        $signedIdentifier."\n".
        $signedVersion;

    $signature = base64_encode(
        hash_hmac(
            'sha256',
            urldecode(utf8_encode($stringToSign)),
            $this->account['primaryKey'],
            true
        )
    );

    $arrayToUrl = [
        'sv='.urlencode($signedVersion),
        'st='.urlencode($signedStart),
        'se='.urlencode($signedExpiry),
        'sr='.urlencode($signedResource),
        'sp='.urlencode($signedPermission),
        'rscd='.urlencode($responseContent),
        'rsct='.urlencode($responseType),
        'sig='.urlencode($signature)
    ];

    $url =  'https://'.$this->account['accountName'].'.blob.core.windows.net'.'/'
        .$container.'/'
        .$blob.'?'.implode('&', $arrayToUrl);

    return $url;
}

有人建议我做错了吗?我是Microsoft Azure的新手

解决方案

我相信您的$stringToSign变量存在问题.基于此处的文档: http://msdn.microsoft.com/en -US/library/azure/dn140255.aspx ,您要签名的字符串应如下所示:

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

考虑到要在SAS查询字符串中包含rscdrsct.请尝试以下操作,看看是否有区别:

    $stringToSign =
            $signedPermission."\n".
            $signedStart."\n".
            $signedExpiry."\n".
            $canonicalizedResource."\n".
            $signedIdentifier."\n".
            $signedVersion."\n".
            "\n".
            $responseContent."\n".
            "\n".
            "\n".
            $responseType;

更新

请尝试以下代码.将帐户名称/密钥,容器名称和Blob名称替换为适当的值:

<?php
$signedStart = gmdate('Y-m-d\TH:i:s\Z', strtotime('-1 day'));
echo $signedStart."\n";
$signedExpiry = gmdate('Y-m-d\TH:i:s\Z', strtotime('+1 day'));
echo $signedExpiry."\n";
$signedResource = 'b';
$signedPermission = 'r';
$signedIdentifier = '';
$accountName = "[account name]";
$accountKey = "[account key]";
$container = "[container name]";
$blob = "[blob name]";
$canonicalizedResource = '/'.$accountName.'/'.$container.'/'.$blob;
$signedVersion = '2014-02-14';  
echo $canonicalizedResource."\n";
$rscc = '';
$rscd = 'file; attachment';//Content disposition
$rsce = '';
$rscl = '';
$rsct = 'binary';//Content type
$stringToSign = 
                $signedPermission."\n".
                $signedStart."\n".
                $signedExpiry."\n".
                $canonicalizedResource."\n".
                $signedIdentifier."\n".
                $signedVersion."\n".
                $rscc."\n".
                $rscd."\n".
                $rsce."\n".
                $rscl."\n".
                $rsct;


echo $stringToSign."\n";

$signature = base64_encode(
        hash_hmac(
            'sha256',
            $stringToSign,
            base64_decode($accountKey),
            true
        )
    );

echo $signature."\n";


$arrayToUrl = [
        'sv='.urlencode($signedVersion),
        'st='.urlencode($signedStart),
        'se='.urlencode($signedExpiry),
        'sr='.urlencode($signedResource),
        'sp='.urlencode($signedPermission),
        'rscd='.urlencode($rscd),
        'rsct='.urlencode($rsct),
        'sig='.urlencode($signature)
    ];

    $url =  'https://'.$accountName.'.blob.core.windows.net'.'/'
        .$container.'/'
        .$blob.'?'.implode('&', $arrayToUrl);

echo $url."\n";
?>

基本上有两个问题(除了错误的$stringToSign变量之外):

  1. 开始/结束日期时间格式不正确.
  2. 我们需要base64_decode用于计算签名的帐户密钥.

i trying to create SAS link to blob resource using PHP. Unfortunately currently in azure SDK there is no method for creating SAS signature. I wrote a code for generating SAS but when i'm trying to get a resource by the link generated by this method i'm getting this message: Signature fields not well formed.

    public function getSharedAccessSignatureURL($container, $blob)
{
    $signedStart = date('c', strtotime('-1 day'));
    $signedExpiry = date('c', strtotime('+1 day'));
    $signedResource = 'b';
    $signedPermission = 'r';
    $signedIdentifier = '';
    $responseContent = "file; attachment";
    $responseType = "binary";

    $canonicalizedResource = '/'.$this->account['accountName'].'/'.$container.'/'.$blob;
    $signedVersion = '2014-02-14';

    $stringToSign =
        $signedPermission."\n".
        $signedStart."\n".
        $signedExpiry."\n".
        $canonicalizedResource."\n".
        $signedIdentifier."\n".
        $signedVersion;

    $signature = base64_encode(
        hash_hmac(
            'sha256',
            urldecode(utf8_encode($stringToSign)),
            $this->account['primaryKey'],
            true
        )
    );

    $arrayToUrl = [
        'sv='.urlencode($signedVersion),
        'st='.urlencode($signedStart),
        'se='.urlencode($signedExpiry),
        'sr='.urlencode($signedResource),
        'sp='.urlencode($signedPermission),
        'rscd='.urlencode($responseContent),
        'rsct='.urlencode($responseType),
        'sig='.urlencode($signature)
    ];

    $url =  'https://'.$this->account['accountName'].'.blob.core.windows.net'.'/'
        .$container.'/'
        .$blob.'?'.implode('&', $arrayToUrl);

    return $url;
}

Any suggest what i am doing wrong? I am commpletle newbie at Microsoft Azure

解决方案

I believe there's an issue with your $stringToSign variable. Based on the documentation here: http://msdn.microsoft.com/en-US/library/azure/dn140255.aspx, your string to sign should be constructed like the following:

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

considering you're including rscd and rsct in your SAS querystring. Please try the following and see if that makes the difference:

    $stringToSign =
            $signedPermission."\n".
            $signedStart."\n".
            $signedExpiry."\n".
            $canonicalizedResource."\n".
            $signedIdentifier."\n".
            $signedVersion."\n".
            "\n".
            $responseContent."\n".
            "\n".
            "\n".
            $responseType;

UPDATE

Please try the code below. Replace the account name/key, container name and blob name with appropriate values:

<?php
$signedStart = gmdate('Y-m-d\TH:i:s\Z', strtotime('-1 day'));
echo $signedStart."\n";
$signedExpiry = gmdate('Y-m-d\TH:i:s\Z', strtotime('+1 day'));
echo $signedExpiry."\n";
$signedResource = 'b';
$signedPermission = 'r';
$signedIdentifier = '';
$accountName = "[account name]";
$accountKey = "[account key]";
$container = "[container name]";
$blob = "[blob name]";
$canonicalizedResource = '/'.$accountName.'/'.$container.'/'.$blob;
$signedVersion = '2014-02-14';  
echo $canonicalizedResource."\n";
$rscc = '';
$rscd = 'file; attachment';//Content disposition
$rsce = '';
$rscl = '';
$rsct = 'binary';//Content type
$stringToSign = 
                $signedPermission."\n".
                $signedStart."\n".
                $signedExpiry."\n".
                $canonicalizedResource."\n".
                $signedIdentifier."\n".
                $signedVersion."\n".
                $rscc."\n".
                $rscd."\n".
                $rsce."\n".
                $rscl."\n".
                $rsct;


echo $stringToSign."\n";

$signature = base64_encode(
        hash_hmac(
            'sha256',
            $stringToSign,
            base64_decode($accountKey),
            true
        )
    );

echo $signature."\n";


$arrayToUrl = [
        'sv='.urlencode($signedVersion),
        'st='.urlencode($signedStart),
        'se='.urlencode($signedExpiry),
        'sr='.urlencode($signedResource),
        'sp='.urlencode($signedPermission),
        'rscd='.urlencode($rscd),
        'rsct='.urlencode($rsct),
        'sig='.urlencode($signature)
    ];

    $url =  'https://'.$accountName.'.blob.core.windows.net'.'/'
        .$container.'/'
        .$blob.'?'.implode('&', $arrayToUrl);

echo $url."\n";
?>

Essentially there were two issues (apart from incorrect $stringToSign variable):

  1. Start/End date time were not properly formatted.
  2. We would need to base64_decode the account key for calculating signature.

这篇关于适用于PHP的Microsoft Azure和SAS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆