| 1 |
<?php |
| 2 |
namespace WPOSS; |
| 3 |
|
| 4 |
if (is_file(__DIR__ . '/sdk/aliyun-oss-php-sdk/autoload.php')) { |
| 5 |
require_once __DIR__ . '/sdk/aliyun-oss-php-sdk/autoload.php'; |
| 6 |
} |
| 7 |
|
| 8 |
use OSS\OssClient; |
| 9 |
use OSS\Core\OssException; |
| 10 |
|
| 11 |
|
| 12 |
class Api { |
| 13 |
|
| 14 |
private $options = array(); |
| 15 |
private $client; |
| 16 |
private $errors = array(); |
| 17 |
|
| 18 |
public function __construct($options = array()) { |
| 19 |
$this->options = $options; |
| 20 |
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。 |
| 21 |
// 强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 |
| 22 |
// Endpoint以杭州为例,� |
| 23 |
�它Region请按实� |
| 24 |
� |
| 25 |
况填写。 |
| 26 |
// 说明 使用自定义域名时,无法使用listBuckets方法。 |
| 27 |
|
| 28 |
try { |
| 29 |
$this->client = new OssClient($this->options['accessKeyId'], $this->options['accessKeySecret'], $this->options['endpoint'], $this->options['cname']); |
| 30 |
if (!$this->client->doesBucketExist($this->options['bucket'])) { |
| 31 |
$this->client = Null; |
| 32 |
$this->errors[] = "Bucket 不存在!"; |
| 33 |
}; |
| 34 |
} catch (OssException $e) { |
| 35 |
$this->errors[] = $e->getMessage(); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
public function is_client() { |
| 40 |
return $this->client instanceof OSSClient; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* 判断bucket是否存在 |
| 45 |
* @param $accessKeyId : $accessKeyId |
| 46 |
* @param $accessKeySecret : |
| 47 |
* @param $endpoint : |
| 48 |
* @param $bucket : bucket name |
| 49 |
* @return array, 1 (存在), 0 (不存在), -1 (异常) |
| 50 |
*/ |
| 51 |
static public function does_bucket_exist($accessKeyId, $accessKeySecret, $endpoint, $bucket) { |
| 52 |
try { |
| 53 |
$client = new OssClient($accessKeyId, $accessKeySecret, $endpoint, False); |
| 54 |
if ($client->doesBucketExist($bucket)) { |
| 55 |
return array( |
| 56 |
"status" => 1, |
| 57 |
"msg" => "Bucket 存在!", |
| 58 |
); |
| 59 |
} else { |
| 60 |
return array( |
| 61 |
"status" => 0, |
| 62 |
"msg" => "Bucket 不存在!", |
| 63 |
); |
| 64 |
} |
| 65 |
} catch (OssException $e) { |
| 66 |
return array("status" => -1, "msg" => $e->getMessage(),); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* 上传文件到OSS |
| 72 |
* @param $object : 文件名称 |
| 73 |
* @param $filePath : 需要上传的文件路径,例如/users/local/my_file.txt |
| 74 |
*/ |
| 75 |
public function Upload($object, $filePath) { |
| 76 |
try{ |
| 77 |
// 判读对象是否存在! |
| 78 |
// $exist = $this->client->doesObjectExist($this->options['bucket'], $object); |
| 79 |
// if (!$exist) { |
| 80 |
$this->client->uploadFile($this->options['bucket'], $object, $filePath); |
| 81 |
// } else { |
| 82 |
// $this->errors[] = "该object或object name已存在!"; |
| 83 |
// } |
| 84 |
} catch(OssException $e) { |
| 85 |
$this->errors[] = $e->getMessage(); |
| 86 |
// return; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* 删除远程OSS对象 |
| 92 |
* @param $objects |
| 93 |
*/ |
| 94 |
public function Delete($objects) { |
| 95 |
// object不存在时,也返回正常的响应 |
| 96 |
$this->client->deleteObjects($this->options['bucket'], $objects); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* 远程OSS对象是否存在 |
| 102 |
* @param $object |
| 103 |
* @return bool |
| 104 |
*/ |
| 105 |
public function hasExist($object) { |
| 106 |
try{ |
| 107 |
return $this->client->doesObjectExist($this->options['bucket'], $object); |
| 108 |
} catch(OssException $e) { |
| 109 |
return False; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|