上传文件到阿里云OSS

/**
 *  阿里云 OSS 密钥
 */
define('AccessKeyId', 'AccessKeyId');
define('AccessKeySecret', 'AccessKeySecret');

/**
 *  上传文件到 OSS
 */
function ossUpload($bucket, $file, $upload){

	$date = gmdate('D, d M Y H:i:s \G\M\T');
	$sign = 'OSS ' . AccessKeyId . ':' . base64_encode(hash_hmac('sha1', "PUT\n\napplication/octet-stream\n" . $date . "\n/" . $bucket . $upload, AccessKeySecret, true));

	$header = array(
        'Date: ' . $date,
        'Authorization: ' . $sign,
		'Content-Type: application/octet-stream'
    );

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, 'https://moguapp.oss-cn-beijing.aliyuncs.com' . $upload);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
	curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
	curl_setopt($curl, CURLOPT_POSTFIELDS, file_get_contents($file));
    $respone = curl_exec($curl);
    $error = curl_error($curl);
    curl_close($curl);
	
    if ($error) {
        return $error;
    } else {
        return $respone;
    }

}

function main() {
	ossUpload('mogublog', '01.jpg', '/01.jpg');
}

main();