Oss.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace plugin\ali_oss\service;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use laytp\traits\Error;
  7. use OSS\OssClient;
  8. class Oss
  9. {
  10. use Error;
  11. protected static $instance;
  12. /**
  13. * 单例
  14. * @return static
  15. */
  16. public static function instance()
  17. {
  18. if (is_null(self::$instance)) {
  19. self::$instance = new static();
  20. }
  21. return self::$instance;
  22. }
  23. /**
  24. * 服务端上传文件
  25. * @param $localFileName string 已经上传到服务器的文件名
  26. * @param $saveFileName string 要保存到OSS的文件名
  27. * @param $config array 阿里云OSS配置
  28. * @return bool
  29. * @throws Exception
  30. */
  31. public function upload($localFileName, $saveFileName, $config){
  32. try{
  33. $accessKey = $config['accessKeyID'];
  34. if (!$accessKey) {
  35. $this->setError('上传失败,阿里云OSS的accessKey为空,请到.env文件进行配置');
  36. return false;
  37. }
  38. $secretKey = $config['accessKeySecret'];
  39. if (!$secretKey) {
  40. $this->setError('上传失败,阿里云OSS的secretKey为空,请到.env文件进行配置');
  41. return false;
  42. }
  43. $endpoint = $config['endpoint'];
  44. if (!$endpoint) {
  45. $this->setError('上传失败,阿里云OSS的endpoint为空,请到.env文件进行配置');
  46. return false;
  47. }
  48. $bucket = $config['bucket'];
  49. if (!$bucket) {
  50. $this->setError('上传失败,阿里云OSS的bucket为空,请到.env文件进行配置');
  51. return false;
  52. }
  53. $domain = $config['domain'];
  54. if (!$domain) {
  55. $this->setError('上传失败,阿里云OSS的domain为空,请到.env文件进行配置');
  56. return false;
  57. }
  58. $ossClient = new OssClient($accessKey, $secretKey, $endpoint);
  59. $ossClient->uploadFile($bucket, $saveFileName, $localFileName);
  60. return $domain . '/' . $saveFileName;
  61. }catch (\Exception $e){
  62. $this->setError('上传失败,'.$e->getMessage());
  63. return false;
  64. }
  65. }
  66. /**
  67. * 获取一个临时凭证用于让客户端进行上传操作
  68. * @param $config
  69. * @return bool
  70. */
  71. public function sts($config){
  72. $accessKey = $config['accessKeyID'];
  73. if (!$accessKey) {
  74. $this->setError('上传失败,阿里云STS的accessKey为空,请到.env文件进行配置');
  75. return false;
  76. }
  77. $secretKey = $config['accessKeySecret'];
  78. if (!$secretKey) {
  79. $this->setError('上传失败,阿里云STS的secretKey为空,请到.env文件进行配置');
  80. return false;
  81. }
  82. $ARN = $config['ARN'];
  83. if (!$ARN) {
  84. $this->setError('上传失败,阿里云STS的ARN为空,请到.env文件进行配置');
  85. return false;
  86. }
  87. $endpoint = $config['endpoint'];
  88. if (!$endpoint) {
  89. $this->setError('上传失败,阿里云STS的endpoint为空,请到.env文件进行配置');
  90. return false;
  91. }
  92. $domain = $config['domain'];
  93. if (!$domain) {
  94. $this->setError('上传失败,阿里云STS的domain为空,请到.env文件进行配置');
  95. return false;
  96. }
  97. $bucket = $config['bucket'];
  98. if (!$bucket) {
  99. $this->setError('上传失败,阿里云STS的bucket为空,请到.env文件进行配置');
  100. return false;
  101. }
  102. try {
  103. AlibabaCloud::accessKeyClient($accessKey, $secretKey)
  104. ->regionId('cn-hangzhou')
  105. ->asDefaultClient();
  106. $result = AlibabaCloud::rpc()
  107. ->product('Sts')
  108. ->scheme('https') // https | http
  109. ->version('2015-04-01')
  110. ->action('AssumeRole')
  111. ->method('POST')
  112. ->host('sts.aliyuncs.com')
  113. ->options([
  114. 'query' => [
  115. 'RegionId' => $endpoint,
  116. 'RoleArn' => $ARN,
  117. 'RoleSessionName' => "upload",
  118. ],
  119. ])
  120. ->request();
  121. $resultArr = $result->toArray();
  122. $resultArr['Credentials']['endpoint'] = $endpoint;
  123. $resultArr['Credentials']['domain'] = $domain;
  124. $resultArr['Credentials']['bucket'] = $bucket;
  125. return $resultArr['Credentials'];
  126. } catch (ClientException $e) {
  127. $this->setError('ClientException:'.$e->getMessage());
  128. return false;
  129. } catch (ServerException $e) {
  130. $this->setError('ServerException:'.$e->getMessage());
  131. return false;
  132. }
  133. }
  134. }