oddity-agency/laravel-filesystem-adapter

此包已被废弃且不再维护。未建议替代包。
此包的最新版本(1.0.3)没有可用的许可信息。

Laravel的文件系统

1.0.3 2020-02-07 12:44 UTC

This package is not auto-updated.

Last update: 2024-03-02 08:56:49 UTC


README

文件系统为Laravel

为Laravel 5.x提供的存储操作服务,目前支持:本地、s3、azure。

安装

将以下内容添加到你的项目的composer.json文件中的require块

  • "oddity-agency/laravel-filesystem-adapter": "dev-master"

配置

对于Laravel 5.3及之前版本,你需要将以下内容注册到config/app.php文件中的providers数组中

  • Filesystem\FileStorageServiceProvider::class

对于Laravel 5.4及更高版本

  • package:discover会自动从包中发现ServiceProvider

在filesystems.php中设置配置

'default' => 'desired-supported-storage', // 'local', 's3', azure'

  'disks' => [

      // if default is local
      'local' => [
          'driver' => 'local',
          'root' => storage_path('app/public'),
      ],

      // if default is s3
      's3' => [
          'driver' => 's3',
          'key' => env('AWS_KEY'),
          'secret' => env('AWS_SECRET'),
          'region' => env('AWS_REGION'),
          'bucket' => env('AWS_BUCKET'),
          'credentials' => [ env('AWS_KEY'), env('AWS_SECRET')],
      ],

      // if default is azure
      'azure' => [
          'driver'    => 'azure',
          'name'      => env('AZURE_STORAGE_NAME'),
          'key'       => env('AZURE_STORAGE_KEY'),
          'container' => env('AZURE_STORAGE_CONTAINER'),
      ],
  ],

然后创建一个控制器,其构造函数将接受

  • Filesystem\Interfaces\FileSystemInterface ,它已经绑定在FileSystemServiceProvider中,并将返回default存储服务的实例。

方法


namespace Filesystem\Interfaces;

use Symfony\Component\HttpFoundation\File\UploadedFile;

   /**
    * Interface FileSystemInterface
    * @package App\Interfaces
    */
   interface FileSystemInterface
   {
   	/**
   	 * @param $file
   	 * @return mixed
   	 */
   	public function checkIfFileExists($file);

   	/**
   	 * @param $path
   	 * @return mixed
   	 */
   	public function browse($path);

   	/**
   	 * @param UploadedFile $file
   	 * @param $path
   	 * @return mixed
   	 */
   	public function put(UploadedFile $file, $path);

   	/**
   	 * @param $fileName
   	 * @param $download
   	 * @return mixed
   	 */
   	public function get($fileName, $download);

   	/**
   	 * @param $fileName
   	 * @return mixed
   	 */
   	public function delete($fileName);
   }


所有这些方法都在S3StorageService、AzureStorageService、LocalStorageService中实现

public function get($fileName, $download);

将返回文件或如果请求参数中的download参数设置为true,则下载文件。

public function put(UploadedFile $file, $path);

注意:如果设置了GET['path'],文件将上传到该路径(文件夹)中,而不是存储的根目录。