AliyunOSS.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /**
  3. * 阿里云oss文件上传处理类
  4. */
  5. namespace app\common\library;
  6. vendor('aliyun-oss.autoload');
  7. use OSS\OssClient;
  8. use OSS\Core\OssException;
  9. use think\Env;
  10. /**
  11. * Class aliyunOSS
  12. *
  13. * 获取OssClient实例和其他公用方法
  14. * 阿里云文档 https://promotion.aliyun.com/ntms/act/ossdoclist.html
  15. */
  16. class AliyunOSS
  17. {
  18. private static $endpoint;
  19. private static $accessKeyId;
  20. private static $accessKeySecret;
  21. private static $bucket;
  22. private static $self = NULL;
  23. /**
  24. * 根据Config配置,得到一个OssClient实例
  25. *
  26. * @return OssClient 一个OssClient实例
  27. */
  28. static public function getOssClient()
  29. {
  30. if (is_null(self::$self)) {
  31. try {
  32. self::$self = new OssClient(Env::get('ali_oss.access_keysid'), Env::get('ali_oss.access_keysecrt'), Env::get('ali_oss.access_endpoint'),false);
  33. } catch (OssException $e) {
  34. printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
  35. printf($e->getMessage() . "\n");
  36. return null;
  37. }
  38. }
  39. return self::$self;
  40. }
  41. /*******************************************************************************/
  42. /********************** bucket操作 *******************************/
  43. /*******************************************************************************/
  44. /**
  45. * 工具方法,创建一个存储空间,如果发生异常直接exit
  46. */
  47. static public function createBucket($bucket)
  48. {
  49. $ossClient = self::getOssClient();
  50. if (is_null($ossClient)) exit(1);
  51. $acl = OssClient::OSS_ACL_TYPE_PUBLIC_READ;
  52. try {
  53. return $ossClient->createBucket($bucket, $acl);
  54. } catch (OssException $e) {
  55. $message = $e->getMessage();
  56. if (\OSS\Core\OssUtil::startsWith($message, 'http status: 403')) {
  57. echo "Please Check your AccessKeyId and AccessKeySecret" . "\n";
  58. exit(0);
  59. } elseif (strpos($message, "BucketAlreadyExists") !== false) {
  60. echo "Bucket already exists. Please check whether the bucket belongs to you, or it was visited with correct endpoint. " . "\n";
  61. exit(0);
  62. }
  63. printf(__FUNCTION__ . ": FAILED\n");
  64. printf($e->getMessage() . "\n");
  65. return '';
  66. }
  67. }
  68. /**
  69. * 列出用户所有的Bucket
  70. * @return null
  71. */
  72. public static function listBuckets()
  73. {
  74. $ossClient = self::getOssClient();
  75. $bucketList = null;
  76. try {
  77. $bucketListInfo = $ossClient->listBuckets();
  78. } catch (OssException $e) {
  79. printf(__FUNCTION__ . ": FAILED\n");
  80. printf($e->getMessage() . "\n");
  81. return '';
  82. }
  83. $bucketList = $bucketListInfo->getBucketList();
  84. /* foreach ($bucketList as $bucket) {
  85. print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
  86. }*/
  87. return $bucketList;
  88. }
  89. /**
  90. * 判断Bucket是否存在
  91. * @param string $bucket 存储空间名称
  92. */
  93. public static function doesBucketExist($bucket)
  94. {
  95. $ossClient = self::getOssClient();
  96. try {
  97. $res = $ossClient->doesBucketExist($bucket);
  98. } catch (OssException $e) {
  99. printf(__FUNCTION__ . ": FAILED\n");
  100. printf($e->getMessage() . "\n");
  101. return '';
  102. }
  103. return $res;
  104. }
  105. /**
  106. * 获取bucket的acl配置
  107. * @param string $bucket 存储空间名称
  108. * @return null
  109. */
  110. public static function getBucketAcl($bucket)
  111. {
  112. $ossClient = self::getOssClient();
  113. try {
  114. $res = $ossClient->getBucketAcl($bucket);
  115. } catch (OssException $e) {
  116. printf(__FUNCTION__ . ": FAILED\n");
  117. printf($e->getMessage() . "\n");
  118. return '';
  119. }
  120. return $res;
  121. }
  122. /**
  123. * 设置bucket的acl配置
  124. * @param string $bucket 存储空间名称
  125. * @return null
  126. */
  127. public static function putBucketAcl($bucket)
  128. {
  129. $ossClient = self::getOssClient();
  130. $acl = OssClient::OSS_ACL_TYPE_PRIVATE;
  131. try {
  132. $res = $ossClient->putBucketAcl($bucket, $acl);
  133. } catch (OssException $e) {
  134. printf(__FUNCTION__ . ": FAILED\n");
  135. printf($e->getMessage() . "\n");
  136. return '';
  137. }
  138. return $res;
  139. }
  140. /*******************************************************************************/
  141. /********************** 文件上传和下载 操作 *******************************/
  142. /*******************************************************************************/
  143. /**
  144. * 创建目录
  145. * $dir 目录名
  146. */
  147. public static function createDir($dir)
  148. {
  149. $time = date("Y-m-d H:i:s");
  150. $content = "这是一个{$dir}目录,创建时间{$time}";
  151. $fileName = $dir."/Readme.txt";
  152. $ossClient = self::getOssClient();
  153. $options = array();
  154. try {
  155. $res = $ossClient->putObject(Env::get('ali_oss.access_bucket'), $fileName, $content, $options);
  156. } catch (OssException $e) {
  157. printf(__FUNCTION__ . ": FAILED\n");
  158. printf($e->getMessage() . "\n");
  159. return '';
  160. }
  161. return $res;
  162. }
  163. public static function doesDirExist($dir){
  164. $fileName = $dir."/Readme.txt";
  165. $res = self::doesObjectExist($fileName);
  166. return $res;
  167. }
  168. /**
  169. * 把本地变量的内容存到文件 如果是远程图片,则把图片变成二进制流,在写入文件中
  170. *
  171. * 简单上传,上传指定变量的内存值作为object的内容
  172. * @param string $fileName 要存储文件名称 当上传远程图片时可以不给名字 会与远程名字同名
  173. * @param string $content 要写入的内容
  174. * @return null
  175. */
  176. public static function putObject($content,$fileName='',$dir='')
  177. {
  178. $extentionArr = array( 'gif', 'jpg', 'jpeg', 'png');
  179. $extention = pathinfo($content, PATHINFO_EXTENSION);
  180. if(in_array($extention,$extentionArr)){
  181. $picName = pathinfo($content,PATHINFO_BASENAME);
  182. $content = file_get_contents($content);
  183. if(empty($fileName)){
  184. $fileName = $picName;
  185. }
  186. if(empty($dir)){
  187. $dir = date("Ym");
  188. }
  189. $fileName = $dir.'/'.$fileName;
  190. }else{
  191. return '1';
  192. }
  193. $ossClient = self::getOssClient();
  194. $options = array();
  195. try {
  196. $res = $ossClient->putObject(Env::get('ali_oss.access_bucket'), $fileName, $content, $options);
  197. } catch (OssException $e) {
  198. printf(__FUNCTION__ . ": FAILED\n");
  199. printf($e->getMessage() . "\n");
  200. return '';
  201. }
  202. return $res;
  203. }
  204. /**
  205. * 上传指定的本地文件内容
  206. * @param string $new_fileName 给文件重命名
  207. * @param string $filePath 文件路劲
  208. * @param string $dir 存放目录 不给默认为最新的时间为目录
  209. * @return null
  210. */
  211. public static function uploadFile($filePath,$new_fileName,$dir='')
  212. {
  213. $ossClient = self::getOssClient();
  214. $options = [];
  215. $dir = empty($dir) ? date("Ym",time()) : $dir;
  216. $new_fileName = $dir.'/'.$new_fileName;
  217. try {
  218. $res = $ossClient->uploadFile(Env::get('ali_oss.access_bucket'), $new_fileName, $filePath, $options);
  219. } catch (OssException $e) {
  220. printf(__FUNCTION__ . ": FAILED\n - ");
  221. printf($e->getMessage() . "\n");
  222. return '';
  223. }
  224. return $res;
  225. }
  226. /**
  227. * 列出Bucket内所有文件, 注意如果符合条件的文件数目超过设置的max-keys, 用户需要使用返回的nextMarker作为入参,通过
  228. * @param string $prefix 模糊查询
  229. $prefix 限定返回的Object 必须以Prefix作为前缀。注意使用prefix查询时,返回的key中仍会包含Prefix。
  230. $delimiter 用于对Object名字进行分组的字符。所有名字包含指定的前缀且第一次出现Delimiter字符之间的Object作为一组元素
  231. $nextMarker 设定结果从Marker之后按字母排序的第一个开始返回。作为下一页的标记
  232. $maxkeys = 1000 限定此次返回Object的最大数,如果不设定,默认为100,MaxKeys取值不能大于1000。
  233. * @return null
  234. */
  235. public static function listObjects($prefix = '',$delimiter='',$nextMarker='',$maxkeys=50)
  236. {
  237. $ossClient = self::getOssClient();
  238. $options = array(
  239. 'delimiter' => $delimiter,
  240. 'prefix' => $prefix,
  241. 'max-keys' => $maxkeys,
  242. 'marker' => $nextMarker,
  243. );
  244. try {
  245. $listObjectInfo = $ossClient->listObjects(Env::get('ali_oss.access_bucket'), $options);
  246. } catch (OssException $e) {
  247. printf(__FUNCTION__ . ": FAILED\n");
  248. printf($e->getMessage() . "\n");
  249. return '';
  250. }
  251. $data = array();
  252. //下一页的标记
  253. $nextMarker = $listObjectInfo->getNextMarker();
  254. if($delimiter){
  255. $objectList = $listObjectInfo->getPrefixList();
  256. if (!empty($objectList)) {
  257. foreach ($objectList as $objectInfo) {
  258. $data[] = $objectInfo->getPrefix();
  259. }
  260. }
  261. }else{
  262. $objectList = $listObjectInfo->getObjectList(); // 文件列表
  263. if (!empty($objectList)) {
  264. foreach ($objectList as $objectInfo) {
  265. $data[] = ['key' => $objectInfo->getKey(), 'LastModified' => $objectInfo->getLastModified()];
  266. }
  267. }
  268. }
  269. return array("data"=>$data,'nextMarker'=>$nextMarker);
  270. }
  271. /**
  272. * 获取object的内容
  273. * @param string $fileName 获取文件的内容,如果是图片,则得到二进制流 不用绝对地址,按照bucket相对路劲
  274. * @return null
  275. */
  276. public static function getObject($fileName,$options = array())
  277. {
  278. $ossClient = self::getOssClient();
  279. try {
  280. $content = $ossClient->getObject(Env::get('ali_oss.access_bucket'), $fileName, $options);
  281. } catch (OssException $e) {
  282. printf(__FUNCTION__ . ": FAILED\n");
  283. printf($e->getMessage() . "\n");
  284. return '';
  285. }
  286. // $content = getimagesize($content); 得到图片具体信息
  287. return $content;
  288. }
  289. /**
  290. * get_object_to_local_file
  291. *
  292. * 获取object
  293. * 将object下载到指定的文件 少用,还不是很清楚,怎么用
  294. * @param string $fileName 获取文件的内容,如果是图片,则得到二进制流 不用绝对地址,按照bucket相对路劲
  295. * $localfile 本地文件名
  296. * @return null
  297. */
  298. public static function getObjectToLocalFile($fileName,$localfile)
  299. {
  300. $ossClient = self::getOssClient();
  301. $options = array(
  302. OssClient::OSS_FILE_DOWNLOAD => $localfile,
  303. );
  304. try {
  305. $res = $ossClient->getObject(Env::get('ali_oss.access_bucket'), $fileName, $options);
  306. } catch (OssException $e) {
  307. printf(__FUNCTION__ . ": FAILED\n");
  308. printf($e->getMessage() . "\n");
  309. return '';
  310. }
  311. return $res;
  312. }
  313. /**
  314. * 删除object
  315. * @param string $fileName 不用绝对地址,按照bucket相对路劲
  316. * $fileName也可以是数组形式 那么就是批量删除
  317. * @param string $bucket,为空时使用默认配置的bucket
  318. * @return null
  319. */
  320. public static function deleteObject($fileName,$bucket='')
  321. {
  322. $ossClient = self::getOssClient();
  323. try {
  324. $bucket = !empty($bucket) ? $bucket : Env::get('ali_oss.access_bucket');
  325. $res = $ossClient->deleteObject($bucket, $fileName);
  326. } catch (OssException $e) {
  327. printf(__FUNCTION__ . ": FAILED\n");
  328. printf($e->getMessage() . "\n");
  329. return '';
  330. }
  331. return $res;
  332. }
  333. /**
  334. * 判断object是否存在
  335. * @param string $fileName 不用绝对地址,按照bucket相对路劲
  336. * @return null
  337. */
  338. public static function doesObjectExist($fileName)
  339. {
  340. $ossClient = self::getOssClient();
  341. try {
  342. $exist = $ossClient->doesObjectExist(Env::get('ali_oss.access_bucket'), $fileName);
  343. } catch (OssException $e) {
  344. printf(__FUNCTION__ . ": FAILED\n");
  345. printf($e->getMessage() . "\n");
  346. return '';
  347. }
  348. return $exist;
  349. }
  350. }