kitpages/file-system-bundle

Symfony Kitpages FileSystem Bundle。文件抽象层(本地、S3)

v4.0.0 2019-01-23 08:52 UTC

README

这是一个提供文件系统抽象层的 Symfony2 扩展包。基本上,它与 KnpLabs 的 gaufrette 库做同样的事情,但管理大型文件更高效。我们可以用 128Mo 的 PHP 进程内存限制来管理 2GB 的文件。我们从不将整个内容传输到 $content 变量中。

使用此扩展包,您可以将文件保存到不同的文件系统(S3、本地文件系统、FTP 等)

配置系统的一些元素基于 KnpGaufretteBundle 的代码。

版本

2019-01-23 : v4.0.0

  • [新增] 升级到 Symfony 2.8、3.x、4.x
  • [文档] 最好的测试文档

2018-05-16 : v3.2.1

  • [修复] Swift 中 flysystem 系统中文件的 URL
  • [修复] 当文件在 flysystem 适配器中不存在时的错误
  • [修复] flysystem 适配器中的 MIME 类型问题

2018-05-15 : v3.2.0

  • [新增] 与 flysystem 集成

这里缺少大量的文档...

2012-05-24 : v1.0.0

  • 首次稳定发布

当前状态

此扩展包是稳定的。适配器包括

  • 本地适配器:服务器文件系统
  • S3 适配器:用于 Amazon Web 服务 AWS S3
  • FTP 适配器:用于 FTP 访问
  • FlySystem 适配器:用于飞系统链接

安装

您需要在您的 deps 中添加以下行

使用 Composer,只需 $ composer require kitpages/file-system-bundle 包或

{
  "require": {
    "kitpages/file-system-bundle": "dev-master"
  }
}

仅当您使用 AmazonS3 时

{
  "require": {
    amazonwebservices/aws-sdk-for-php: ~1.5
  }
}

仅当您使用 Flysystem 时

{
  "require": {
    "league/flysystem": "^1.0",
    "oneup/flysystem-bundle": "^1.14",
  }
}

AppKernel.php

$bundles = array(
    ...
    new Kitpages\FileSystemBundle\KitpagesFileSystemBundle(),
);

// AWS SDK 需要特殊的自动加载器

require_once __DIR__.'/../vendor/aws-sdk/sdk.class.php';

配置示例

以下配置定义了 3 个文件系统

  • kitpagesFile:本地文件系统
  • kitpagesAmazon:Amazon S3 上的文件系统
  • kitpagesFlysystem:另一个文件系统抽象

让我们看看 config.yml 中的配置

kitpages_file_system:
    file_system_list:
        kitpagesFile:
            local:
                directory_public: %kernel.root_dir%/../web
                directory_private: %kernel.root_dir%
                base_url: %base_url%
        kitpagesAmazon:
            amazon_s3:
                bucket_name: %kitpagesFile_amazons3_bucketname%
                key: %kitpagesFile_amazons3_key%
                secret_key: %kitpagesFile_amazons3_secretkey%
        kitpagesFlysystem:
            flysystem:
                flysystem_adapter: oneup_flysystem.your_filesystem
                file_uri_prefix: https://your.custom.url/4687311687643/FRA/

使用示例

// use AdapterFile at the beginning of the file
use Kitpages\FileSystemBundle\Model\AdapterFile;

// get the adapter
$localAdapter = $this->get("kitpages_file_system.file_system.kitpagesFile");
$s3Adapter = $this->get("kitpages_file_system.file_system.kitpagesAmazon");

// private files (without direct public URL)
$adapter->copyTempToAdapter("/my_physical_dir/foo.txt", new AdapterFile("bar/foo.txt") );
$adapter->copyAdapterToTemp(new AdapterFile("bar/foo.txt"), "/my_physical_dir/foo.txt" );

// public files (with a direct URL given by the adapter)
$adapter->copyTempToAdapter("/my_physical_dir/foo.txt", new AdapterFile("bar/foo.txt", true) );
$url = $adapter->getFileLocation(new AdapterFile("bar/foo.txt", true));

// some functions of the adapter :
$adapterFile = new AdapterFile("bar/foo.txt");
$adapter->copyTempToAdapter("/my_physical_dir/foo.txt", $adapterFile );
$content = $adapter->getFileContent($adapterFile);
$adapter->sendFileToBrowser($adapterFile);
if ($adapter->isFile($adapterFile) ) {
    // if file exists in the adapter
}

运行测试

cd Tests/app/web
php -S localhost:8888