laravel 如何配置 SCP/SFTP 文件存储?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46429322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I configure an SCP/SFTP file storage?
提问by Antonín Slej?ka
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.
我的 Laravel 应用程序应该将文件复制到另一个远程主机。远程主机只能通过 SCP 使用私钥访问。我想配置一个新的文件存储(类似于 FTP),但我没有找到有关如何定义 SCP 驱动程序的信息。
回答by Cy Rossignol
You'll need to install the SFTP driverfor 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):
通过将以下代码添加到(或其他适当的服务提供者)的boot()
方法中,使用新驱动程序扩展 Laravel 的文件系统AppServiceProvider
:
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);