FileSystemServiceProvider.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Support\Facades\Storage;
  5. use Illuminate\Filesystem\FilesystemAdapter;
  6. use League\Flysystem\FilesystemOperator;
  7. use League\Flysystem\Filesystem;
  8. use League\Flysystem\Local\LocalFilesystemAdapter;
  9. class FileSystemServiceProvider extends ServiceProvider
  10. {
  11. public function boot()
  12. {
  13. Storage::extend('s3', function ($app, $config) {
  14. $adapter = new \League\Flysystem\AwsS3V3\AwsS3V3Adapter(
  15. new \Aws\S3\S3Client($config),
  16. $config['bucket'],
  17. $config['root'] ?? ''
  18. );
  19. return new class($adapter, $adapter, $config) extends FilesystemAdapter {
  20. protected $config;
  21. public function __construct(FilesystemOperator $driver, $adapter, array $config = [])
  22. {
  23. parent::__construct($driver, $adapter, $config);
  24. $this->config = $config;
  25. }
  26. public function url($path)
  27. {
  28. return "/api/download?filename={$path}&bucket={$this->config['bucket']}";
  29. }
  30. };
  31. });
  32. }
  33. }