Include/require 来自 Flysystem 的文件

Include/require file from Flysystem

我的 PHP 应用程序通过使用 requireinclude.

动态生成一些稍后 运行 的脚本文件

我现在正在将我的生产服务器迁移到 Amazon Web Services,以实现扩展和负载平衡。这意味着生成的脚本文件不应再存储在本地文件系统中,而是存储在 S3 存储桶中。

因此我想使用 Flysystem,这样我就不用考虑文件是存储在本地(开发)还是存储在 S3 存储桶(生产)中。

但是我如何 requireinclude 来自 Flysystem 的文件?我是否必须获取文件内容然后 eval 它?

本地存储:

use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;

$adapter = new Local(__DIR__.'/path/to/root');
$localFilesystem = new Filesystem($adapter);

S3 存储:

use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;

$client = S3Client::factory([
    'credentials' => [
        'key'    => 'your-key',
        'secret' => 'your-secret',
    ],
    'region' => 'your-region',
    'version' => 'latest|version',
]);

$adapter = new AwsS3Adapter($client, 'your-bucket-name', 'optional/path/prefix');
$s3Filesystem = new Filesystem($adapter);

然后,用一个打开你的文件,然后写入另一个:

$contents = $localFilesystem->read('path/to/file.txt');
$s3Filesystem->write('path/to/file.txt', $contents);

您不需要 require、include 或 eval 任何东西!希望这对您有所帮助!

无法直接从 Flysystem includerequire 文件。解决方法是获取文件的内容并 eval 它。请注意,您必须处理 evaled 文件中的任何前导 <?php 和尾随 ?> 标记。