Overview
The OSS SDK provides SDKs for integrating signatures and the upload and download processes. However, you may need to use API operations to upload and download objects with signatures in practice. This topic uses PutObject as an example to describe how to call the PutObject operation in PHP.
Note
Note: We recommend that you use the OSS SDK. This topic provides an example of how to use a signature to upload objects. You can modify the business code in actual use.
Detail
The sample code for implementing PutObject in PHP 5.6.30 is as follows:
<? php
function curlput($url,$data,$method='PUT',$bucket,$object,$accesskey ,$accesskeySecret){
$time = gmdate ("D, d M Y H:i:s T");
$str = "PUT\n\n"."application/json\n".$time."\n/".$bucket." /".$object;
//echo($str);
$signature = base64_encode(hash_hmac("sha1", $str, $accesskeySecret, true));
//echo($time);
//echo($signature);
$ch = curl_init(); // initialize the CURL handle.
curl_setopt($ch, CURLOPT_URL, $url); // Set the request URL.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); // Set the request method.
$headers = array(
"Date:".$time,
'Content-Type:application/json',
"Authorization:OSS ".$accesskey.":".$signature
);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);// Set the HTTP header.
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);// Set the string to be submitted.
$document = curl_exec($ch);// run the predefined CURL
if(! curl_errno($ch)){
$info = curl_getinfo($ch);
echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
} else {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
return $document;
}
$accesskey ='xxx';
$accesskeySecret ='xxx';
$bucket = 'bucketname';
$object = 'mytest/2.txt';
$url = 'http://bucketname.oss-cn-hangzhou.aliyuncs.com/mytest/2.txt';
$data = "{wewwe:wewee}";
$return = curlput($url, $data, 'PUT',$bucket,$object,$accesskey,$accesskeySecret);
var_dump($return);
exit;
? >
Documentation
For more information about how to use the OSS SDK, see add signatures to OSS headers.
Scope
- OSS