我的 Laravel 应用程序应该将文件复制到另一个远程主机。远程主机只能通过带有私钥的 SCP 访问。我想配置一个新的 file storage ( similarly as FTP ),但我没有找到有关如何定义 SCP 驱动程序的信息。
最佳答案
您需要安装 SFTP driver对于 Flysystem,Laravel 用于其文件系统服务的库:
composer require league/flysystem-sftp
这是您可以调整的示例配置。添加到config/filesystems.php 中的disks
数组:
'sftp' => [
'driver' => 'sftp',
'host' => 'example.com',
'port' => 21,
'username' => 'username',
'password' => 'password',
'privateKey' => 'path/to/or/contents/of/privatekey',
'root' => '/path/to/root',
'timeout' => 10,
]
通过将以下代码添加到 AppServiceProvider
(或其他适当的服务提供商)的 boot()
方法,使用新驱动程序扩展 Laravel 的文件系统:
use Storage;
use League\Flysystem\Filesystem;
use League\Flysystem\Sftp\SftpAdapter;
...
public function boot()
{
Storage::extend('sftp', function ($app, $config) {
return new Filesystem(new SftpAdapter($config));
});
}
然后你可以像使用本地文件系统一样使用 Laravel 的 API:
Storage::disk('sftp')->put('path/filename.txt', $fileContents);
关于php - 如何配置 SCP/SFTP 文件存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46429322/