From 2e94ffd643e5ee18f80fbbd5ae7b5b9ca0a80873 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Wed, 11 Nov 2026 21:40:53 +0000 Subject: [PATCH] Don't make the directory before every write to a Flysystem disk --- src/Filesystem/FlysystemAdapter.php | 8 ++++++++ tests/Filesystem/FlysystemAdapterTest.php | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Filesystem/FlysystemAdapter.php b/src/Filesystem/FlysystemAdapter.php index 19b9f8c43dc..7253de14c43 100644 --- a/src/Filesystem/FlysystemAdapter.php +++ b/src/Filesystem/FlysystemAdapter.php @@ -56,6 +56,14 @@ public function exists($path) return parent::exists($path); } + public function put($path, $contents) + { + // Flysystem creates any missing parent directories as part of the write. Making + // the directory first is another request per write on a remote disk, and on + // object stores it leaves an empty "directory" object behind each time. + return $this->filesystem->put($this->normalizePath($path), $contents); + } + public function getFiles($path, $recursive = false) { return $this->collection( diff --git a/tests/Filesystem/FlysystemAdapterTest.php b/tests/Filesystem/FlysystemAdapterTest.php index b7f45a04687..47f438155e8 100644 --- a/tests/Filesystem/FlysystemAdapterTest.php +++ b/tests/Filesystem/FlysystemAdapterTest.php @@ -93,4 +93,14 @@ public function it_throws_an_exception_when_requesting_absolute_paths() $this->adapter->withAbsolutePaths(); } + + #[Test] + public function putting_a_file_does_not_make_the_directory_first() + { + $filesystem = \Mockery::mock(\Illuminate\Filesystem\FilesystemAdapter::class); + $filesystem->shouldReceive('put')->with('sub/dir/filename.txt', 'Hello World')->once()->andReturnTrue(); + $filesystem->shouldNotReceive('makeDirectory'); + + $this->assertTrue((new FlysystemAdapter($filesystem))->put('sub/dir/filename.txt', 'Hello World')); + } }