Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Added scoped filesystem driver #44105

Merged
merged 5 commits into from
Sep 13, 2022
Merged

[9.x] Added scoped filesystem driver #44105

merged 5 commits into from
Sep 13, 2022

Conversation

frankdejonge
Copy link
Contributor

@frankdejonge frankdejonge commented Sep 12, 2022

Often, files are placed on a single filesystem (like S3) but at different paths. This base path needs to be kept in sync all across the codebase. The scoped driver makes it easy to re-use disk configurations, and decorated the internal adapter.

Consider the following situation, when I have an existing s3 disk:

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
            'throw' => false,
        ],

Using the new scoped driver, I can create a version of the existing s3 driver with a deeper base path:

        's3_videos' => [
            'driver' => 'scoped',
            'prefix' => 'path/for/videos',
            'disk' => 's3',
        ],

Possible optimisation, since this is a decorated adapter, we could re-use the inner adapter. I didn't find an elegant way to do so, but I'm open to suggestions.

@taylorotwell taylorotwell merged commit de7c408 into laravel:9.x Sep 13, 2022
@frankdejonge frankdejonge deleted the feature/prefixed-filesystem-adapter branch September 13, 2022 15:22
@iotron
Copy link

iotron commented Sep 27, 2022

@frankdejonge Hi, I cannot get the correct URL for the scoped drivers with $storage->url(). The prefix is missing as it's not implemented in the url function in class FilesystemAdapter.

    /**
     * Get the URL for the file at the given path.
     *
     * @param  string  $path
     * @return string
     *
     * @throws \RuntimeException
     */
    public function url($path)
    {
        $adapter = $this->adapter;

        if (method_exists($adapter, 'getUrl')) {
            return $adapter->getUrl($path);
        } elseif (method_exists($this->driver, 'getUrl')) {
            return $this->driver->getUrl($path);
        } elseif ($adapter instanceof FtpAdapter || $adapter instanceof SftpAdapter) {
            return $this->getFtpUrl($path);
        } elseif ($adapter instanceof LocalAdapter) {
            return $this->getLocalUrl($path);
        } else {
            throw new RuntimeException('This driver does not support retrieving URLs.');
        }
    }

@iotron
Copy link

iotron commented Sep 27, 2022

I have created an issue with details.
#44327

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants