繁体   English   中英

如何配置 SCP/SFTP 文件存储?

[英]How can I configure an SCP/SFTP file storage?

我的 Laravel 应用程序应该将文件复制到另一个远程主机。 远程主机只能通过 SCP 使用私钥访问。 我想配置一个新的文件存储与 FTP 类似),但我没有找到有关如何定义 SCP 驱动程序的信息。

您需要为 Flysystem 安装SFTP 驱动程序,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);

现在,官方文档有 SFTP 连接的说明: https : //laravel.com/docs/8.x/filesystem#sftp-driver-configuration

SFTP: composer require league/flysystem-sftp "~1.0"

SFTP 驱动程序配置 Laravel 的 Flysystem 集成与 SFTP 配合得很好; 但是,框架的默认 filesystems.php 配置文件中不包含示例配置。 如果您需要配置 SFTP 文件系统,您可以使用下面的配置示例:

'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,
    ],

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM