简体   繁体   English

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

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

My Laravel application should copy files to another remote host.我的 Laravel 应用程序应该将文件复制到另一个远程主机。 The remote host is accessible only via SCP with a private key.远程主机只能通过 SCP 使用私钥访问。 I would like to configure a new file storage ( similarly as FTP ), but I have found no information, how to define an SCP driver.我想配置一个新的文件存储与 FTP 类似),但我没有找到有关如何定义 SCP 驱动程序的信息。

You'll need to install the SFTP driver for Flysystem, the library Laravel uses for its filesystem services:您需要为 Flysystem 安装SFTP 驱动程序,Laravel 用于其文件系统服务的库:

composer require league/flysystem-sftp

Here's an example configuration that you can tweak.这是您可以调整的示例配置。 Add to the disks array in config/filesystems.php :添加到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,
]

Extend Laravel's filesystem with the new driver by adding the following code to the boot() method of AppServiceProvider (or other appropriate service provider):通过将以下代码添加到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));
    });
}

Then you can use Laravel's API as you would for the local filesystem:然后你可以像使用本地文件系统一样使用 Laravel 的 API:

Storage::disk('sftp')->put('path/filename.txt', $fileContents);

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

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

SFTP Driver Configuration Laravel's Flysystem integrations work great with SFTP; SFTP 驱动程序配置 Laravel 的 Flysystem 集成与 SFTP 配合得很好; however, a sample configuration is not included with the framework's default filesystems.php configuration file.但是,框架的默认 filesystems.php 配置文件中不包含示例配置。 If you need to configure an SFTP filesystem, you may use the configuration example below:如果您需要配置 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