OSS SignatureDoesNotMatch-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

OSS SignatureDoesNotMatch

栗子雪忆 2016-05-02 13:50:30 4165

StringToSign:
PUT
YmRmM2JmMWRhMzQwNTcyNWJlNzYzNTQwZDY2MDExNDQ=
image/jpg
Mon, 02 May 2016 05:33:01 GMT
/pddtest/Hydrangeas.jpg

{"result":false,"error":"OSS API ERROR:
<?xml version="1.0" encoding="UTF-8"?>n
n

<Code>SignatureDoesNotMatch<\/Code>\n  
    <Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.<\/Message>\n  
        <RequestId>5726E67F58DB3AB17E9902A5<\/RequestId>\n  
            <HostId>oss-cn-shanghai.aliyuncs.com<\/HostId>\n  
                <OSSAccessKeyId>8gfnvylFKLbgBQVL<\/OSSAccessKeyId>\n  
                    <SignatureProvided>P5jGd01TmaEawuEtzx\/x6iVYRts=<\/SignatureProvided>\n  
                        <StringToSign>PUT\nYmRmM2JmMWRhMzQwNTcyNWJlNzYzNTQwZDY2MDExNDQ=\nimage\/jpg\nMon, 02 May 2016 05:33:01 GMT\n\/pddtest\/d41d8cd98f6714f5ba4f331db3d856465cd0a2c0a0.jpg<\/StringToSign>\n  

我的代码:

$fileSize = filesize($filePath);// 函数返回指定文件的大小。为curl服务

$serverPath = $path.md5(microtime()).".jpg";//md5(uniqid(microtime()))
$uri = "/".$bucket ."/$serverPath";
//$date = gmdate('D, d M Y H:i:s GMT',strtotime('+8 hours'));//格式化 GMT/UTC 日期/时间。同 date() 函数 类似,不同的是返回的时间是格林威治标准时(GMT)。
$date = gmdate('D, d M Y H:i:s GMT');
$picturedata=fread(fopen($filePath,"r"),$fileSize);
//echo $picturedata;md5_file
$content_md5=base64_encode(md5($picturedata));
// echo $content_md5;
// $content_md5="";
//$content_type= $_FILES"image";
//echo $content_type;
$content_type="image/jpg";
$caOssHeaders="";
//$caResource='/' . str_replace(array('%2F', '%25'), array('/', '%'), rawurlencode("/".$filePath1));
$string_to_sign="PUT\n".$content_md5."n". $content_type."\n".$date."n/".$bucket."/".$filePath1;
$string_to_sign_new=utf8_encode($string_to_sign);
//$string_to_sign_new=str_replace(array("\r", "\n"), '', $string_to_sign);
//$string_to_sign_new.=rawurldecode($caResource);
echo $string_to_sign_new."n";

$sign = base64_encode(hash_hmac("sha1",$string_to_sign_new,$AcKey,true));
// echo $sign;
$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 基本认证
XML 移动开发 API 对象存储 数据格式
分享到
取消 提交回答
全部回答(2)
  • 栗子雪忆
    2019-07-17 18:51:01

    非常感谢楼上的关注!我自己已经解决了!感觉SDK类容太多,我只要实现一个简单的图片上传功能,在项目中给大家提供一个API!

    1 0
  • 鸣镝
    2019-07-17 18:51:01

    你好,请问为什么不使用sdk?

    0 0
添加回答

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

推荐文章
相似问题
推荐课程