只是需要一个简单的上传图片功能,
官方的那个php SDK有点多啊,
自己随便写了一个,用的put_object接口
不说了,上代码
<?php
class OSS
{
const AK = 'xxx';
const SK = 'xxxxxxxxxxxxx';
private $_date = '';
private $_ext = '';
private $_mime = '';
private $_file = '';
private $_savaName = null;
private $_bucket = null;
private $url = '';
private $_data = '';
public function __construct($url, $bucket, $path, $name = null)
{
$this->url = $url;
$this->_bucket = $bucket;
$this->_date = gmdate('D, d M Y H:i:s T');
$this->_file = $path;
$this->_savaName = $name;
$this->getExt();
if (is_null($name)) {
$this->_savaName = uniqid();
} else {
$this->_savaName = $name;
}
$this->_data = file_get_contents($path);
}
private function getExt()
{
$imageTypeArray = array
(
0 => 'UNKNOWN',
1 => 'GIF',
2 => 'JPEG',
3 => 'PNG',
4 => 'SWF',
5 => 'PSD',
6 => 'BMP',
7 => 'TIFF_II',
8 => 'TIFF_MM',
9 => 'JPC',
10 => 'JP2',
11 => 'JPX',
12 => 'JB2',
13 => 'SWC',
14 => 'IFF',
15 => 'WBMP',
16 => 'XBM',
17 => 'ICO',
18 => 'COUNT'
);
$info = getimagesize($this->_file);
if(!isset($info[2])) {
throw new Exception('getimagesize error');
}
$mineNO =$info[2];
if (!isset($imageTypeArray[$mineNO]) || $imageTypeArray[$mineNO] === 0) {
throw new Exception('unknown type');
}
$this->_mime = $info['mime'];
$this->_ext = strtolower($imageTypeArray[$mineNO]);
}
private function hmacSignature($encodedPutPolicy)
{
return base64_encode(hash_hmac("sha1", $encodedPutPolicy, self::SK, true));
}
private function getHeaders()
{
$send = array();
$send[] = sprintf('PUT /%s.%s HTTP/1.1', $this->_savaName, $this->_ext);
$send[] = sprintf('HOST: %s.%s', $this->_bucket, $this->url);
$send[] = "Cache-control: no-cache";
$send[] = sprintf('Date: %s', $this->_date);
$send[] = "Content-Encoding: utf-8";
$send[] = sprintf('Content-Type: %s', $this->_mime);
$send[] = sprintf('Content-Length: %s', strlen($this->_data));
$send[] = "Connection: Close";
return $send;
}
public function putObject()
{
$_arr = array(
'PUT',
'',
$this->_mime,
$this->_date,
sprintf('/%s/%s.%s', $this->_bucket, $this->_savaName, $this->_ext)
);
$signature = $this->hmacSignature(implode("\n", $_arr));
$authorization = "OSS " . self::AK . ":" . $signature;
$headers = $this->getHeaders();
$headers[] = 'Authorization: ' . $authorization . "\r\n\r\n";
$data = implode("\r\n", $headers) . $this->_data;
// echo sprintf('%s.%s', $this->_bucket, $this->url);
// echo $data;
$fp = fsockopen(sprintf('%s.%s', $this->_bucket, $this->url), 80, $errno, $errstr, 3);
if (!$fp) {
// echo "$errstr ($errno) \n";
throw new \Exception($errstr,$errno);
}
fwrite($fp, $data);
$buffer = array();
while (!feof($fp)) {
$buffer[] = fgets($fp, 128);
}
fclose($fp);
if (count($buffer) > 0 && $buffer[0] === "HTTP/1.1 200 OK\r\n") {
return true;
} else {
return false;
}
}
}
$oss = new OSS('oss-cn-beijing.aliyuncs.com', '你上传的bucket', '本地地址','保存名(不包含后缀)');
$result = $oss->putObject();
var_dump($result);
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。