AliOssServiceProvider.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Jacobcyl\AliOSS;
  3. use Jacobcyl\AliOSS\Plugins\PutFile;
  4. use Jacobcyl\AliOSS\Plugins\PutRemoteFile;
  5. use Illuminate\Support\Facades\Storage;
  6. use Illuminate\Support\Facades\Log;
  7. use Illuminate\Support\ServiceProvider;
  8. use League\Flysystem\Filesystem;
  9. use OSS\OssClient;
  10. class AliOssServiceProvider extends ServiceProvider
  11. {
  12. /**
  13. * Bootstrap the application services.
  14. *
  15. * @return void
  16. */
  17. public function boot()
  18. {
  19. //发布配置文件
  20. /*
  21. if (function_exists('config_path')) {
  22. $this->publishes([
  23. __DIR__ . '/config/config.php' => config_path('alioss.php'),
  24. ], 'config');
  25. }
  26. */
  27. Storage::extend('oss', function($app, $config)
  28. {
  29. $accessId = $config['access_id'];
  30. $accessKey = $config['access_key'];
  31. $cdnDomain = empty($config['cdnDomain']) ? '' : $config['cdnDomain'];
  32. $bucket = $config['bucket'];
  33. $ssl = empty($config['ssl']) ? false : $config['ssl'];
  34. $isCname = empty($config['isCName']) ? false : $config['isCName'];
  35. $debug = empty($config['debug']) ? false : $config['debug'];
  36. $endPoint = $config['endpoint']; // 默认作为外部节点
  37. $epInternal= $isCname?$cdnDomain:(empty($config['endpoint_internal']) ? $endPoint : $config['endpoint_internal']); // 内部节点
  38. if($debug) Log::debug('OSS config:', $config);
  39. $client = new OssClient($accessId, $accessKey, $epInternal, $isCname);
  40. $adapter = new AliOssAdapter($client, $bucket, $endPoint, $ssl, $isCname, $debug, $cdnDomain);
  41. //Log::debug($client);
  42. $filesystem = new Filesystem($adapter);
  43. $filesystem->addPlugin(new PutFile());
  44. $filesystem->addPlugin(new PutRemoteFile());
  45. //$filesystem->addPlugin(new CallBack());
  46. return $filesystem;
  47. });
  48. }
  49. /**
  50. * Register the application services.
  51. *
  52. * @return void
  53. */
  54. public function register()
  55. {
  56. }
  57. }