OSS.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Api\Storage\OSS;
  3. vendor('aliyun-oss-php-sdk.autoload');
  4. use app\core\util\SystemConfigService;
  5. use OSS\Core\OssException;
  6. use OSS\OssClient;
  7. use think\Request;
  8. use think\Cache;
  9. /**
  10. * 阿里云OSS上传
  11. * Class OSS
  12. * @package Api\Storage\OSS
  13. */
  14. class OSS
  15. {
  16. protected static $accessKey;
  17. protected static $secretKey;
  18. protected static $auth = null;
  19. //TODO 空间域名 Domain
  20. protected static $uploadUrl;
  21. //TODO 存储空间名称 公开空间
  22. protected static $storageName;
  23. /**
  24. * TODO 初始化
  25. * @return null|OssClient
  26. * @throws \OSS\Core\OssException
  27. */
  28. protected static function autoInfo(){
  29. if(($storageName = Cache::get('storageName')) && ($uploadUrl = Cache::get('uploadUrl')) && ($accessKey = Cache::get('accessKey')) && ($secretKey = Cache::get('secretKey'))){
  30. self::$accessKey = $accessKey;
  31. self::$secretKey = $secretKey;
  32. self::$uploadUrl = $uploadUrl;
  33. self::$storageName = $storageName;
  34. }else{
  35. self::$accessKey = trim(SystemConfigService::get('accessKey'));
  36. self::$secretKey = trim(SystemConfigService::get('secretKey'));
  37. self::$uploadUrl = trim(SystemConfigService::get('uploadUrl')).'/';
  38. self::$storageName = trim(SystemConfigService::get('storage_name'));
  39. Cache::set('accessKey',self::$accessKey);
  40. Cache::set('secretKey',self::$secretKey);
  41. Cache::set('uploadUrl',self::$uploadUrl);
  42. Cache::set('storageName',self::$storageName);
  43. }
  44. if(!self::$accessKey || !self::$secretKey || !self::$uploadUrl || !self::$storageName){
  45. exception('请设置 secretKey 和 accessKey 和 空间域名 和 存储空间名称');
  46. }
  47. if(self::$auth == null) {
  48. self::$auth = new OssClient(self::$accessKey,self::$secretKey,self::$uploadUrl);
  49. if(!self::$auth->doesBucketExist(self::$storageName)) self::$auth->createBucket(self::$storageName,self::$auth::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
  50. }
  51. return self::$auth;
  52. }
  53. /**
  54. * TODO 文件上传 名称
  55. * @param string $filename
  56. * @return string
  57. */
  58. public static function uploadImage($filename = 'image'){
  59. $request = Request::instance();
  60. $file = $request->file($filename);
  61. $filePath = $file->getRealPath();
  62. $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  63. $key = substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
  64. try{
  65. self::autoInfo();
  66. return self::$auth->uploadFile(self::$storageName,$key,$filePath);
  67. }catch (OssException $e){
  68. return $e->getMessage();
  69. }
  70. }
  71. /**
  72. * TODO 文件上传 内容
  73. * @param $key
  74. * @param $content
  75. * @return string
  76. */
  77. public static function uploadImageStream($key, $content){
  78. try{
  79. self::autoInfo();
  80. return self::$auth->putObject(self::$storageName,$key,$content);
  81. }catch (OssException $e){
  82. return $e->getMessage();
  83. }
  84. }
  85. /**
  86. * TODO 删除资源
  87. * @param $key
  88. * @return mixed
  89. */
  90. public static function delete($key){
  91. try {
  92. self::autoInfo();
  93. return self::$auth->deleteObject(self::$storageName,$key);
  94. } catch (OssException $e) {
  95. return $e->getMessage();
  96. }
  97. }
  98. }