FileSystemServiceProvider.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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\Local\LocalFilesystemAdapter;
  8. class FileSystemServiceProvider extends ServiceProvider
  9. {
  10. public function boot()
  11. {
  12. Storage::extend('s3', function ($app, $config) {
  13. $adapter = new \League\Flysystem\AwsS3V3\AwsS3V3Adapter(
  14. new \Aws\S3\S3Client($config),
  15. $config['bucket'],
  16. $config['root'] ?? ''
  17. );
  18. return new class($adapter, $config) extends FilesystemAdapter {
  19. protected $config;
  20. public function __construct(FilesystemOperator $adapter, $config)
  21. {
  22. parent::__construct($adapter);
  23. $this->config = $config;
  24. }
  25. public function url($path)
  26. {
  27. return "/api/download?filename={$path}&bucket={$this->config['bucket']}";
  28. }
  29. };
  30. });
  31. }
  32. }