如何配置SCP/SFTP文件存储? [英] How can I configure an SCP/SFTP file storage?

查看:140
本文介绍了如何配置SCP/SFTP文件存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Laravel应用程序应将文件复制到另一个远程主机.只能通过带有私钥的SCP访问远程主机.我想配置一个新的文件存储(

My Laravel application should copy files to another remote host. The remote host is accessible only via SCP with a private key. I would like to configure a new file storage (similarly as FTP), but I have found no information, how to define an SCP driver.

推荐答案

您需要安装用于Flysystem的SFTP驱动程序,Laravel库将其用于文件系统服务:

You'll need to install the SFTP driver for Flysystem, the library Laravel uses for its filesystem services:

composer require league/flysystem-sftp

这是您可以调整的示例配置.添加到 config/filesystems.php 中的disks数组:

Here's an example configuration that you can tweak. Add to the disks array in config/filesystems.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的文件系统:

Extend Laravel's filesystem with the new driver by adding the following code to the boot() method of AppServiceProvider (or other appropriate service provider):

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:

Then you can use Laravel's API as you would for the local filesystem:

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

这篇关于如何配置SCP/SFTP文件存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆