Object.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace app\common\service\Oss;
  3. use Exception;
  4. use OSS\Core\OssException;
  5. use OSS\OssClient;
  6. use RuntimeException;
  7. class Object extends Common {
  8. /**
  9. * 创建虚拟目录
  10. *
  11. * @param $dir 虚拟目录名
  12. * @return Boolean
  13. */
  14. public function createObjectDir($dir) {
  15. try {
  16. $this->_ossClient->createObjectDir($this->getBucketName(), $dir);
  17. } catch (OssException $e) {
  18. throw_exception($e->getMessage());
  19. return false;
  20. }
  21. return true;
  22. }
  23. /**
  24. * 把本地变量的内容到文件
  25. * 简单上传,上传指定变量的内存值作为object的内容
  26. * @param string $fileName 存储的文件名:photo1.png | 201602/photo1.png
  27. * @param string $content 文件流
  28. * @return Boolean
  29. */
  30. public function putObject(string $fileName, string &$content) {
  31. try {
  32. $this->_ossClient->putObject($this->getBucketName(), $fileName, $content, []);
  33. } catch (Exception $e) {
  34. throw new RuntimeException($e);
  35. }
  36. return true;
  37. }
  38. /**
  39. * 上传指定的本地文件内容
  40. *
  41. * @param string $fileName 存储的文件名:photo1.png | 201602/photo1.png
  42. * @param string $filePath 本地文件路径
  43. * @return Boolean
  44. */
  45. public function uploadFile($fileName, $filePath) {
  46. $options = array();
  47. try {
  48. $this->_ossClient->uploadFile($this->getBucketName(), $fileName, $filePath, $options);
  49. } catch (OssException $e) {
  50. throw_exception($e->getMessage());
  51. return false;
  52. }
  53. return true;
  54. }
  55. /**
  56. * @param $object 文件相对地址
  57. * @param string $acl 读写权限,可选值 ['default', 'private', 'public-read', 'public-read-write']
  58. * @return bool
  59. * @throws \think\Exception
  60. */
  61. public function putObjectAcl( $object, $acl = 'private') {
  62. $options = array();
  63. try {
  64. $this->_ossClient->putObjectAcl($this->getBucketName(), $object,$acl);
  65. } catch (OssException $e) {
  66. throw_exception($e->getMessage());
  67. return false;
  68. }
  69. return true;
  70. }
  71. /**
  72. * 列出Bucket内所有目录和文件, 注意如果符合条件的文件数目超过设置的max-keys, 用户需要使用返回的nextMarker作为入参,通过
  73. * 循环调用ListObjects得到所有的文件,具体操作见下面的 listAllObjects 示例
  74. *
  75. * @param OssClient $this->_ossClient OssClient实例
  76. * @param string $bucket 存储空间名称
  77. * @return null
  78. */
  79. public function listObjects($prefix="" ,$maxkeys=1000) {
  80. $delimiter = '/';
  81. $nextMarker = '';
  82. $options = array(
  83. 'delimiter' => $delimiter,
  84. 'prefix' => $prefix,
  85. 'max-keys' => $maxkeys,
  86. 'marker' => $nextMarker,
  87. );
  88. try {
  89. $listObjectInfo = $this->_ossClient->listObjects($this->getBucketName(), $options);
  90. } catch (OssException $e) {
  91. throw_exception($e->getMessage());
  92. return false;
  93. }
  94. $data = [];
  95. $objectList = $listObjectInfo->getObjectList(); // 文件列表
  96. $prefixList = $listObjectInfo->getPrefixList(); // 目录列表
  97. if (!empty($objectList)) {
  98. foreach ($objectList as $objectInfo) {
  99. print($objectInfo->getKey() . "\n");
  100. $data['file'][] = $objectInfo->getKey();
  101. }
  102. }
  103. if (!empty($prefixList)) {
  104. foreach ($prefixList as $prefixInfo) {
  105. print($prefixInfo->getPrefix() . "\n");
  106. $data['dir'][] = $objectInfo->getPrefix();
  107. }
  108. }
  109. return $data;
  110. }
  111. /**
  112. * 列出Bucket内所有目录和文件, 根据返回的nextMarker循环得到所有Objects
  113. *
  114. * @param OssClient $this->_ossClient OssClient实例
  115. * @param string $bucket 存储空间名称
  116. * @return null
  117. */
  118. public function listAllObjects($prefix = "") {
  119. $delimiter = '/';
  120. $nextMarker = '';
  121. $maxkeys = 30;
  122. while (true) {
  123. $options = array(
  124. 'delimiter' => $delimiter,
  125. 'prefix' => $prefix,
  126. 'max-keys' => $maxkeys,
  127. 'marker' => $nextMarker,
  128. );
  129. var_dump($options);
  130. try {
  131. $listObjectInfo = $this->_ossClient->listObjects($this->getBucketName(), $options);
  132. } catch (OssException $e) {
  133. printf(__FUNCTION__ . ": FAILED\n");
  134. printf($e->getMessage() . "\n");
  135. return;
  136. }
  137. // 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表
  138. $nextMarker = $listObjectInfo->getNextMarker();
  139. $listObject = $listObjectInfo->getObjectList();
  140. $listPrefix = $listObjectInfo->getPrefixList();
  141. var_dump(count($listObject));
  142. var_dump(count($listPrefix));
  143. if ($nextMarker === '') {
  144. break;
  145. }
  146. }
  147. }
  148. /**
  149. * 获取object的内容
  150. * @param string $objectPath 对象路径
  151. * @return string
  152. */
  153. public function getObject(string $objectPath) {
  154. try {
  155. $content = $this->_ossClient->getObject($this->getBucketName(), $objectPath, []);
  156. } catch (Exception $e) {
  157. throw new RuntimeException($e);
  158. }
  159. return $content;
  160. }
  161. /**
  162. * get_object_to_local_file
  163. *
  164. * 获取object
  165. * 将object下载到指定的文件
  166. *
  167. * @param OssClient $this->_ossClient OssClient实例
  168. * @param string $bucket 存储空间名称
  169. * @return null
  170. */
  171. public function getObjectToLocalFile()
  172. {
  173. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  174. $localfile = "upload-test-object-name.txt";
  175. $options = array(
  176. OssClient::OSS_FILE_DOWNLOAD => $localfile,
  177. );
  178. try {
  179. $this->_ossClient->getObject($this->getBucketName(), $object, $options);
  180. } catch (OssException $e) {
  181. printf(__FUNCTION__ . ": FAILED\n");
  182. printf($e->getMessage() . "\n");
  183. return;
  184. }
  185. print(__FUNCTION__ . ": OK, please check localfile: 'upload-test-object-name.txt'" . "\n");
  186. if (file_get_contents($localfile) === file_get_contents(__FILE__)) {
  187. print(__FUNCTION__ . ": FileContent checked OK" . "\n");
  188. } else {
  189. print(__FUNCTION__ . ": FileContent checked FAILED" . "\n");
  190. }
  191. if (file_exists($localfile)) {
  192. unlink($localfile);
  193. }
  194. }
  195. /**
  196. * 拷贝object
  197. * 当目的object和源object完全相同时,表示修改object的meta信息
  198. *
  199. * @param OssClient $this->_ossClient OssClient实例
  200. * @param string $bucket 存储空间名称
  201. * @return null
  202. */
  203. public function copyObject()
  204. {
  205. $fromBucket = $this->getBucketName();
  206. $fromObject = "oss-php-sdk-test/upload-test-object-name.txt";
  207. $toBucket = $bucket;
  208. $toObject = $fromObject . '.copy';
  209. $options = array();
  210. try {
  211. $this->_ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $options);
  212. } catch (OssException $e) {
  213. printf(__FUNCTION__ . ": FAILED\n");
  214. printf($e->getMessage() . "\n");
  215. return;
  216. }
  217. print(__FUNCTION__ . ": OK" . "\n");
  218. }
  219. /**
  220. * 修改Object Meta
  221. * 利用copyObject接口的特性:当目的object和源object完全相同时,表示修改object的meta信息
  222. *
  223. * @param OssClient $this->_ossClient OssClient实例
  224. * @param string $bucket 存储空间名称
  225. * @return null
  226. */
  227. public function modifyMetaForObject()
  228. {
  229. $fromBucket = $this->getBucketName();
  230. $fromObject = "oss-php-sdk-test/upload-test-object-name.txt";
  231. $toBucket = $this->getBucketName();
  232. $toObject = $fromObject;
  233. $copyOptions = array(
  234. OssClient::OSS_HEADERS => array(
  235. 'Cache-Control' => 'max-age=60',
  236. 'Content-Disposition' => 'attachment; filename="xxxxxx"',
  237. ),
  238. );
  239. try {
  240. $this->_ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);
  241. } catch (OssException $e) {
  242. printf(__FUNCTION__ . ": FAILED\n");
  243. printf($e->getMessage() . "\n");
  244. return;
  245. }
  246. print(__FUNCTION__ . ": OK" . "\n");
  247. }
  248. /**
  249. * 获取object meta, 也就是getObjectMeta接口
  250. *
  251. * @param OssClient $this->_ossClient OssClient实例
  252. * @param string $bucket 存储空间名称
  253. * @return null
  254. */
  255. public function getObjectMeta()
  256. {
  257. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  258. try {
  259. $objectMeta = $this->_ossClient->getObjectMeta($this->getBucketName(), $object);
  260. } catch (OssException $e) {
  261. printf(__FUNCTION__ . ": FAILED\n");
  262. printf($e->getMessage() . "\n");
  263. return;
  264. }
  265. print(__FUNCTION__ . ": OK" . "\n");
  266. if (isset($objectMeta[strtolower('Content-Disposition')]) &&
  267. 'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]
  268. ) {
  269. print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");
  270. } else {
  271. print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");
  272. }
  273. }
  274. /**
  275. * 删除object
  276. *
  277. * @param $object
  278. * @return bool
  279. */
  280. public function deleteObject($object) {
  281. try {
  282. $this->_ossClient->deleteObject($this->getBucketName(), $object);
  283. } catch (Exception $e) {
  284. throw new RuntimeException($e);
  285. }
  286. return true;
  287. }
  288. /**
  289. * 批量删除object
  290. *
  291. * @param OssClient $this->_ossClient OssClient实例
  292. * @param string $bucket 存储空间名称
  293. * @return null
  294. */
  295. public function deleteObjects()
  296. {
  297. $objects = array();
  298. $objects[] = "oss-php-sdk-test/upload-test-object-name.txt";
  299. $objects[] = "oss-php-sdk-test/upload-test-object-name.txt.copy";
  300. try {
  301. $this->_ossClient->deleteObjects($this->getBucketName(), $objects);
  302. } catch (OssException $e) {
  303. printf(__FUNCTION__ . ": FAILED\n");
  304. printf($e->getMessage() . "\n");
  305. return;
  306. }
  307. print(__FUNCTION__ . ": OK" . "\n");
  308. }
  309. /**
  310. * 判断object是否存在
  311. * @param string $object 存储空间名称
  312. * @return bool
  313. */
  314. public function doesObjectExist(string $object) {
  315. try {
  316. $exist = $this->_ossClient->doesObjectExist($this->getBucketName(), $object);
  317. } catch (Exception $e) {
  318. throw new RuntimeException($e);
  319. }
  320. return $exist;
  321. }
  322. }