如何配置 SCP/SFTP 文件存储?
How can I configure an SCP/SFTP file storage?
我的 Laravel 应用程序应该将文件复制到另一个远程主机。远程主机只能通过带有私钥的 SCP 访问。我想配置一个新的file storage (similarly as FTP),但是我没有找到任何资料,如何定义一个SCP驱动程序。
您需要为 Flysystem 安装 SFTP driver,库 Laravel 用于其文件系统服务:
composer require league/flysystem-sftp
这是您可以调整的示例配置。添加到config/filesystems中的disks
数组。php:
'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);
现在官方文档有SFTP连接的说明:
https://laravel.com/docs/8.x/filesystem#sftp-driver-configuration
SFTP:composer require league/flysystem-sftp "~1.0"
SFTP Driver Configuration Laravel's Flysystem integrations work great
with SFTP; however, a sample configuration is not included with the
framework's default filesystems.php configuration file. If you need to
configure an SFTP filesystem, you may use the configuration example
below:
'sftp' => [
'driver' => 'sftp',
'host' => 'example.com',
'username' => 'your-username',
'password' => 'your-password',
// Settings for SSH key based authentication...
'privateKey' => '/path/to/privateKey',
'password' => 'encryption-password',
// Optional SFTP Settings...
// 'port' => 22,
// 'root' => '',
// 'timeout' => 30,
],
我的 Laravel 应用程序应该将文件复制到另一个远程主机。远程主机只能通过带有私钥的 SCP 访问。我想配置一个新的file storage (similarly as FTP),但是我没有找到任何资料,如何定义一个SCP驱动程序。
您需要为 Flysystem 安装 SFTP driver,库 Laravel 用于其文件系统服务:
composer require league/flysystem-sftp
这是您可以调整的示例配置。添加到config/filesystems中的disks
数组。php:
'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);
现在官方文档有SFTP连接的说明: https://laravel.com/docs/8.x/filesystem#sftp-driver-configuration
SFTP:composer require league/flysystem-sftp "~1.0"
SFTP Driver Configuration Laravel's Flysystem integrations work great with SFTP; however, a sample configuration is not included with the framework's default filesystems.php configuration file. If you need to configure an SFTP filesystem, you may use the configuration example below:
'sftp' => [
'driver' => 'sftp',
'host' => 'example.com',
'username' => 'your-username',
'password' => 'your-password',
// Settings for SSH key based authentication...
'privateKey' => '/path/to/privateKey',
'password' => 'encryption-password',
// Optional SFTP Settings...
// 'port' => 22,
// 'root' => '',
// 'timeout' => 30,
],