我编写了以下函数来编码我的S3链接,以便他们处理亚马逊的S3编码系统以保护链接。麻烦的是它只有在文件在桶中时才有效。如果我将一个子文件夹放到桶中并将文件粘贴在那里,它就无法正常工作。我做错了什么?
function encodeS3($sURL,$sAccessKey,$sSecretKey,$nExpireMinutes = 5) {
$sFile = basename($sURL);
$sBucket = basename(str_replace('/' . $sFile,'',$sURL));
$asQuery = array(
'AWSAccessKeyId' => $sAccessKey,
);
$nExpireSecs = absint( $nExpireMinutes ) * 60;
$nExpireSecs = time() + absint( $nExpireSecs );
$asQuery[ 'Expires' ] = $nExpireSecs;
$sAmazonText = "GET\n\n\n{$nExpireSecs}\n/{$sBucket}/{$sFile}";
$asQuery[ 'Signature' ] = urlencode( base64_encode( ( hash_hmac( 'sha1', utf8_encode( $sAmazonText ), $sSecretKey, TRUE ) ) ) );
$s = add_query_arg( $asQuery, "https://s3.amazonaws.com/{$sBucket}/{$sFile}" );
return esc_url($s);
}
答案 0 :(得分:0)
我只需要改变解析URL的方式。
function encodeS3($sURL,$sAccessKey,$sSecretKey,$nExpireMinutes = 5) {
/*
$sFile = basename($sURL);
$sBucket = basename(str_replace('/' . $sFile,'',$sURL));
*/
$sURL = str_replace('https://','',$sURL);
$sURL = str_replace('http://','',$sURL);
$sURL = str_replace(urlencode('https://'),'',$sURL);
$sURL = str_replace(urlencode('http://'),'',$sURL);
$asParts = explode('/',$sURL);
array_shift($asParts);
$sBucket = array_shift($asParts);
$sFile = implode('/',$asParts);
$asQuery = array(
'AWSAccessKeyId' => $sAccessKey,
);
$nExpireSecs = absint( $nExpireMinutes ) * 60;
$nExpireSecs = time() + absint( $nExpireSecs );
$asQuery[ 'Expires' ] = $nExpireSecs;
$sAmazonText = "GET\n\n\n{$nExpireSecs}\n/{$sBucket}/{$sFile}";
$asQuery[ 'Signature' ] = urlencode( base64_encode( ( hash_hmac( 'sha1', utf8_encode( $sAmazonText ), $sSecretKey, TRUE ) ) ) );
$s = add_query_arg( $asQuery, "https://s3.amazonaws.com/{$sBucket}/{$sFile}" );
return esc_url($s);
}
顺便说一下,上面使用了一些仅在WordPress中可用的功能。如果你需要直接的PHP,你将不得不调整它。