diff --git a/src/StreamWrapper/S3fsStream.php b/src/StreamWrapper/S3fsStream.php index 64d4264..f554b66 100644 --- a/src/StreamWrapper/S3fsStream.php +++ b/src/StreamWrapper/S3fsStream.php @@ -1017,7 +1017,8 @@ class S3fsStream extends StreamWrapper implements StreamWrapperInterface { /** * Try to fetch an object from the metadata cache. * - * If that file isn't in the cache, we assume it doesn't exist. + * If that file isn't in the cache, try to get directly from s3 + * and then save in cache if found. * * @param string $uri * The uri of the resource to check. @@ -1033,21 +1034,26 @@ class S3fsStream extends StreamWrapper implements StreamWrapperInterface { // Trim any trailing '/', in case this is a folder request. $uri = rtrim($uri, '/'); + $cache_enabled = empty($this->config['ignore_cache']); - // Check if this URI is in the cache. - $metadata = $this->readCache($uri); + $metadata = false; + // Do a cache lookup if cache is not ignored + if ($cache_enabled) { + $metadata = $this->readCache($uri); + } - // If cache ignore is enabled, query S3 for all URIs which aren't in the - // cache, and non-folder URIs which are. - if (!empty($this->config['ignore_cache']) && !$metadata['dir']) { - try { - // If getS3Metadata() returns FALSE, the file doesn't exist. - $metadata = $this->getS3Metadata($uri); - } - catch (\Exception $e) { - return $this->triggerError($e->getMessage()); + // If cache did not return anything or is disabled, do a direct lookup + if (!$metadata) { + $metadata = $this->getS3Metadata($uri); + if (!empty($metadata) && $cache_enabled) { + try { + $this->writeCache($metadata); + } catch (S3fsException $exception) { + $this->triggerError($exception->getMessage()); + } } } + return $metadata; } @@ -1170,12 +1176,9 @@ class S3fsStream extends StreamWrapper implements StreamWrapperInterface { * @param string $uri * The URI for the object in S3. * - * @return array - * An array of DB-compatible file metadata. + * @return array|bool + * An array of DB-compatible file metadata or false if lookup fails. * - * @throws \Aws\S3\Exception\S3Exception - * Any exception raised by the listObjects() S3 command will percolate - * out of this function. */ protected function getS3Metadata($uri) { $params = $this->getCommandParams($uri);