gpt4 book ai didi

php - 适用于 blob 的 Azure PHP SAS

转载 作者:行者123 更新时间:2023-12-03 00:13:44 25 4
gpt4 key购买 nike

我正在尝试为我的 azure blob 存储创建一个 SAS,但我收到一条消息,表明我的签名字段格式不正确。

首先是创建签名的函数:

function getSASForBlob($accountName,$container, $blob, $resourceType, $permissions, $expiry,$key){

/* Create the signature */
$_arraysign = array();
$_arraysign[] = $permissions;
$_arraysign[] = '';
$_arraysign[] = $expiry;
$_arraysign[] = '/' . $accountName . '/' . $container . '/' . $blob;
$_arraysign[] = '';
$_arraysign[] = "2014-02-14"; //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)
);
}

然后是一个为我提供 URL 的函数:

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=2014-02-14';

/* Create the signed blob URL */
$_url = 'https://'
.$accountName.'.blob.core.windows.net/'
. $container . '/'
. $blob . '?'
. implode('&', $_parts);

return $_url;
}

然后我调用函数并指定参数。

$container = '87d57f73-a327-4344-91a4-27848d319a66';
$blob = '8C6CBCEB-F962-4939-AD9B-818C124AD3D9.mp4';
$endDate = date('YYYY-MM-DDThh:mmTZD', strtotime('+1 day'));

$_signature = getSASForBlob($accountName,$container,$blob,'b','r',$endDate,$accountKey);
$_blobUrl = getBlobUrl($accountName,$container,$blob,'b','r',$endDate,$_signature);

https://ttresources.blob.core.windows.net/87d57f73-a327-4344-91a4-27848d319a66/8C6CBCEB-F962-4939-AD9B-818C124AD3D9.mp4?se=2016201620162016-FebFeb-ThuThuCET1111%3A0202CET3600Thu&sr=b&sp=r&sig=1VdOjLX179wLQv5o265JBR%2B6CaLTr1nwCs6GHSVqfbA%3D&sv=2014-02-14

这是我得到的结果网址,当我关注它时,我收到下一条错误消息:

AuthenticationFailed Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:41f089a3-0001-0027-376f-6966d1000000 Time:2016-02-17T10:38:00.2724783Z Signature fields not well formed.

最佳答案

因为问题是您设置了错误的日期时间格式。根据guide :

The signedstart and signedexpiry fields must be expressed as UTC times and must adhere to a valid ISO 8601 format. Supported ISO 8601 formats include the following:

  • YYYY-MM-DD
  • YYYY-MM-DDThh:mmTZD
  • YYYY-MM-DDThh:mm:ssTZD

在 PHP 中,您应该按以下格式创建日期时间:

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

然后这将是正确的格式。

关于php - 适用于 blob 的 Azure PHP SAS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35454468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com