FileSystemServiceProvider.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. $driver = new Filesystem($adapter);
  20. return new class($driver, $adapter, $config) extends FilesystemAdapter {
  21. protected $config;
  22. public function __construct(FilesystemOperator $driver, $adapter, array $config = [])
  23. {
  24. parent::__construct($driver, $adapter, $config);
  25. $this->config = $config;
  26. }
  27. public function url($path)
  28. {
  29. $filename = basename($path);
  30. return "/api/download?filename={$filename}&bucket={$this->config['bucket']}";
  31. }
  32. };
  33. });
  34. }
  35. }