GrahamCampbell/Laravel-Flysystem

Change connection in loop

MarkusKuhfuss opened this issue · 2 comments

Hi, i need to change the flysystem (web-dav) connection in a loop to retrieve several files from different web-dav servers.
At the moment i'm only changing the login-credentials but that doesn't work. I think i must "re-initiate" the flysystem connection.
Flysystem::setDefaultConnection(); is no option because i have too many different connections/users. So i must do this re-connection on the fly.

Any help i would be very grateful
Greatings
Markus

Yeh, don't keep setting the config. Laravel's config is really not meant to be changed while the app is running. Config::set only exists so service providers can call it, or tests etc.

The correct way to make a new connection at runtime is to use the flysystem factory, and not to hack the config to make new connections. You can do something like:

$adapter = Flysystem::getFactory()->createAdapter($config);

where $config is something like https://github.com/GrahamCampbell/Laravel-Flysystem/blob/v7.0.1/tests/FlysystemFactoryTest.php#L157, only with the webdav driver and the options you need to go with it.

Ah ok, thank you very much. That works for me!
Here what i did:

$config = array( 'driver' => 'webdav', 'baseUri' => 'https://www.example.com', 'userName' => 'USER', 'password' => 'PASS', 'name' => 'webdav' );

$adapter = Flysystem::getFactory()->createAdapter($config);
$filesystem = new Filesystem($adapter);
$filesystem->write('...')

Have a nice day and thanks for the help.
Markus