开发者社区> 问答> 正文

急急急!!!oss put 请求SignatureDoesNotMatch

栗子雪忆 2016-05-02 15:27:30 3812

$user = $request->user()['name'];

$bucket = '';
$region ='';//new默认为oss-cn-hangzhou
$AcId ='';
$AcKey ='';
$up_url='http://oss-cn-shanghai.aliyuncs.com/';//上传路径
$down_url = "http://$bucket.$region.aliyuncs.com";//分享访问地址
$path =substr(md5($user), 0, 10);
if ($_FILES["image"]["error"] >0)
{
    echo "file error". $_FILES["image"]["error"]."<br />";
    die();
}
$filePath = $_FILES["image"]["tmp_name"];//存储在服务器的文件的临时副本的名称
$filePath1 = $_FILES["image"]["name"];
$fileSize = filesize($filePath);// 函数返回指定文件的大小。为curl服务
$serverPath = $path.md5(microtime()).".jpg";//md5(uniqid(microtime()))
$uri = "/".$bucket ."/$serverPath";

//格式化 GT/UTC 日期/时间。同 date() 函数 类似,不同的是返回的时间是格林威治标准时(GMT)。

$date = gmdate('D, d M Y H:i:s \G\M\T');
$picturedata=fread(fopen($filePath,"r"),$fileSize);
$content_md5=base64_encode(md5($picturedata));
$content_type= $_FILES["image"]["type"];
$caResource='/' . str_replace(array('%2F', '%25'), array('/', '%'), rawurlencode("/".$filePath1));
$string_to_sign="PUT\n".$content_md5."\n". $content_type."\n".$date."\n/".$bucket.$caResource;
$string_to_sign_new=utf8_encode($string_to_sign);
$sign = base64_encode(hash_hmac("sha1",$string_to_sign_new,$AcKey,true));
$ch = curl_init($up_url.$uri);
$headers = array(
    "PUT /".$filePath1." HTTP/1.1",
    "Content-Md5:".$content_md5,
    "Content-Type: ".$content_type,
    "Date: ".$date,
    "Host:oss-cn-shanghai.aliyuncs.com",
    "Authorization:OSS ".$AcId.":" .$sign);//HTTP 基本认证
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_PUT, true);
$fh = fopen($filePath, 'rb');
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, $fileSize);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);//设置cURL允许执行的最长秒数
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);

if(curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    return response()->json(['result'=>true,'url'=>$down_url .'/'.$serverPath]);
}
else {
    $errorMessage = sprintf("OSS API ERROR:%s", $result);
    return response()->json(['result'=>false, 'error'=>$errorMessage]);
    //echo $errorMessage;
}
curl_close($ch);

PUT
YmRmM2JmMWRhMzQwNTcyNWJlNzYzNTQwZDY2MDExNDQ=
image/jpeg
Mon, 02 May 2016 07:17:08 GMT
/pddtest//Hydrangeas.jpg

存储 JSON API 对象存储 数据格式
分享到
取消 提交回答
全部回答(1)
  • 栗子雪忆
    2019-07-17 18:51:01

    自己已经解决!!!!

    1 0

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题