diff --git a/vendor/magento/module-media-gallery-ui/Model/Directories/GetDirectoryTree.php b/vendor/magento/module-media-gallery-ui/Model/Directories/GetDirectoryTree.php
index 897d0d34a5c8..bff9d9867dd0 100644
--- a/vendor/magento/module-media-gallery-ui/Model/Directories/GetDirectoryTree.php
+++ b/vendor/magento/module-media-gallery-ui/Model/Directories/GetDirectoryTree.php
@@ -7,7 +7,9 @@
 
 namespace Magento\MediaGalleryUi\Model\Directories;
 
+use Magento\Framework\App\Config\ScopeConfigInterface;
 use Magento\Framework\App\Filesystem\DirectoryList;
+use Magento\Framework\App\ObjectManager;
 use Magento\Framework\Exception\ValidatorException;
 use Magento\Framework\Filesystem;
 use Magento\Framework\Filesystem\Directory\Read;
@@ -18,6 +20,8 @@
  */
 class GetDirectoryTree
 {
+    private const XML_PATH_MEDIA_GALLERY_IMAGE_FOLDERS
+        = 'system/media_storage_configuration/allowed_resources/media_gallery_image_folders';
     /**
      * @var Filesystem
      */
@@ -28,16 +32,24 @@ class GetDirectoryTree
      */
     private $isPathExcluded;
 
+    /**
+     * @var ScopeConfigInterface
+     */
+    private $coreConfig;
+
     /**
      * @param Filesystem $filesystem
      * @param IsPathExcludedInterface $isPathExcluded
+     * @param ScopeConfigInterface|null $coreConfig
      */
     public function __construct(
         Filesystem $filesystem,
-        IsPathExcludedInterface $isPathExcluded
+        IsPathExcludedInterface $isPathExcluded,
+        ?ScopeConfigInterface $coreConfig = null
     ) {
         $this->filesystem = $filesystem;
         $this->isPathExcluded = $isPathExcluded;
+        $this->coreConfig = $coreConfig ?? ObjectManager::getInstance()->get(ScopeConfigInterface::class);
     }
 
     /**
@@ -74,30 +86,54 @@ private function getDirectories(): array
     {
         $directories = [];
 
-        /** @var Read $directory */
-        $directory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
-
-        if (!$directory->isDirectory()) {
-            return $directories;
-        }
-
-        foreach ($directory->readRecursively() as $path) {
-            if (!$directory->isDirectory($path) || $this->isPathExcluded->execute($path)) {
-                continue;
+        /** @var Read $mediaDirectory */
+        $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
+
+        if ($mediaDirectory->isDirectory()) {
+            $imageFolderPaths = $this->coreConfig->getValue(
+                self::XML_PATH_MEDIA_GALLERY_IMAGE_FOLDERS,
+                ScopeConfigInterface::SCOPE_TYPE_DEFAULT
+            );
+            sort($imageFolderPaths);
+
+            foreach ($imageFolderPaths as $imageFolderPath) {
+                $imageDirectory = $this->filesystem->getDirectoryReadByPath(
+                    $mediaDirectory->getAbsolutePath($imageFolderPath)
+                );
+                if ($imageDirectory->isDirectory()) {
+                    $directories[] = $this->getDirectoryData($imageFolderPath);
+                    foreach ($imageDirectory->readRecursively() as $path) {
+                        if ($imageDirectory->isDirectory($path)) {
+                            $directories[] = $this->getDirectoryData(
+                                $mediaDirectory->getRelativePath($imageDirectory->getAbsolutePath($path))
+                            );
+                        }
+                    }
+                }
             }
-
-            $pathArray = explode('/', $path);
-            $directories[] = [
-                'text' => count($pathArray) > 0 ? end($pathArray) : $path,
-                'id' => $path,
-                'li_attr' => ['data-id' => $path],
-                'path' => $path,
-                'path_array' => $pathArray
-            ];
         }
+
         return $directories;
     }
 
+    /**
+     * Return jstree data for given path
+     *
+     * @param string $path
+     * @return array
+     */
+    private function getDirectoryData(string $path): array
+    {
+        $pathArray = explode('/', $path);
+        return [
+            'text' => count($pathArray) > 0 ? end($pathArray) : $path,
+            'id' => $path,
+            'li_attr' => ['data-id' => $path],
+            'path' => $path,
+            'path_array' => $pathArray
+        ];
+    }
+
     /**
      * Find parent directory
      *
@@ -121,9 +157,9 @@ private function findParent(array &$node, array &$treeNode, int $level = 0): arr
             $tNodePathLength = count($tnode['path_array']);
             $found = false;
             while ($level < $tNodePathLength) {
-                if ($node['path_array'][$level] === $tnode['path_array'][$level]) {
+                $found = $node['path_array'][$level] === $tnode['path_array'][$level];
+                if ($found) {
                     $level ++;
-                    $found = true;
                 } else {
                     break;
                 }
