AliYunOSService.php 992 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /**
  3. * 阿里云oss
  4. * @copyright 引擎力
  5. * @author lc
  6. * @since 2021-07-02
  7. */
  8. namespace App\Http\Services;
  9. use OSS\Core\OssException;
  10. use OSS\OssClient;
  11. class AliYunOSService
  12. {
  13. const ACCESS_KEY_ID = 'LTAI5tR6RRZmNb92xeWEdnZA';
  14. const ACCESS_KEY_SECRET = 'vHG2vjEE9hCPtednFbd8xKt0owfp38';
  15. const ENDPOINT = 'http://oss-cn-hangzhou.aliyuncs.com';
  16. const BUCKET = 'yqlguestdata';
  17. public function __construct()
  18. {
  19. require_once(base_path() . '/sdk/aliyun-oss-php-sdk-master/autoload.php');
  20. }
  21. public function upload($path, $file)
  22. {
  23. try {
  24. $file = explode('.', $file);
  25. $ossClient = new OssClient(self::ACCESS_KEY_ID, self::ACCESS_KEY_SECRET, self::ENDPOINT);
  26. $result = $ossClient->uploadFile(self::BUCKET, $file[0] . date('YmdHis') . '.' . $file[1], $path);
  27. return $result['info']['url'] ?? '';
  28. } catch (OssException $e) {
  29. return $e->getMessage();
  30. }
  31. }
  32. }