How can I configure an SCP/SFTP file storage?
Solution 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);
Solution 2
Now, official documentation has instructions for SFTP connection: https://laravel.com/docs/8.x/filesystem#sftp-driver-configuration
SFTP: composer require league/flysystem-sftp "~1.0"
SFTP Driver Configuration Laravel's Flysystem integrations work great with SFTP; however, a sample configuration is not included with the framework's default filesystems.php configuration file. If you need to configure an SFTP filesystem, you may use the configuration example below:
'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,
],
Antonín Slejška
Updated on June 06, 2022Comments
-
Antonín Slejška almost 2 years
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.
-
Antonín Slejška over 6 yearsThank you. It looks promising. I installed the SFTP driver and added the configuration, but I receive the following error message: '[InvalidArgumentException] Driver [sftp] is not supported.'
-
Antonín Slejška over 6 yearsI tried the configuration as described at: github.com/thephpleague/flysystem-sftp It works perfectly. I can not use the Storage facade, but it is ok for me.
-
code-8 almost 4 yearsIt takes about 8-10 seconds to SFTP in, and list files. Am I doing something wrong ? or Is this normal for SFTP into a server and access files ?
-
Cy Rossignol almost 4 years@cyber8200 It depends on the number of files in the directory listing and the network conditions. Of course, a directory with thousands of files on a server in a different region will take much longer than one located in the same building with just a handful of files. If you're having trouble with latency from SFTP, and you need to list the contents of directories often, you might try fetching the list via SSH instead. You may also want to check whether it's doing a recursive directory listing.
-
code-8 almost 4 yearsIf I run the SCP command, it downloaded instantly, so the network connection is very fast.
-
Yohanim over 3 yearshow to delete file?