How can I configure an SCP/SFTP file storage?

大兔子大兔子 提交于 2019-12-07 07:25:42

问题


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.


回答1:


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

composer require league/flysystem-sftp

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

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));
    });
}

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

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


来源:https://stackoverflow.com/questions/46429322/how-can-i-configure-an-scp-sftp-file-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!