FileSystemServiceProvider.php 1.2 KB

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