Oss.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Servers\Aliyun;
  3. use OSS\OssClient;
  4. use OSS\Core\OssException;
  5. use App\Facades\Servers\Logs\Log;
  6. use OSS\Credentials\StaticCredentialsProvider;
  7. /**
  8. * 阿里oss
  9. *
  10. * @author jun
  11. */
  12. class Oss {
  13. private \OSS\OssClient $ossClient;
  14. function __construct()
  15. {
  16. $accessKeyId = config('aliyun.accessKeyId','');
  17. $accessKeySecret = config('aliyun.accessKeySecret','');
  18. $provider = new StaticCredentialsProvider($accessKeyId,$accessKeySecret);
  19. $endpoint = "https://oss-cn-shenzhen.aliyuncs.com";
  20. $config = array(
  21. "provider" => $provider,
  22. "endpoint" => $endpoint,
  23. "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
  24. "region"=> "cn-shenzhen"
  25. );
  26. $this->ossClient = new OssClient($config);
  27. }
  28. /**
  29. * 使用签名URL上传
  30. * @param string bucket 存储空间名称
  31. * @param string object 不包含Bucket名称在内的Object完整路径
  32. *
  33. * */
  34. function signUrl($bucket, $object, $options=['Content-Type'=>'multipart/form-data'] )
  35. {
  36. // 指定签名URL的过期时间为600s(最长可达32400s)。
  37. $timeout = 600;
  38. try {
  39. // 生成签名URL。
  40. $signedUrl = $this->ossClient->signUrl($bucket, $object, $timeout, "PUT",$options);
  41. // 返回结果
  42. return $signedUrl;
  43. } catch (OssException $e) {
  44. Log::error('oss/signUrl','获取签名URL上传失败,错误:'.json_encode($e->getMessage()));
  45. return ['error'=>$e->getMessage()];
  46. }
  47. }
  48. }