Installation and configuration of Flysystem and Flysystem S3

Created on 28 July 2015, over 9 years ago
Updated 9 August 2023, over 1 year ago

Basic information about Flysystem:-

Flysystem is a filesystem abstraction which allows you to easily swap out a local file system for a remote one. Reducing technical debt and chance of vendor lock-in.

You can sync your local files with a remote file storage like Dropbox / AWS S3 , other ftp or elsewhere. you can see below for the adaptors available for flysystem.

Also files can be directly stored from drupal content add form to remote file storage without manually syncing local file storage with the remote one.

Requirements:

Composer.
composer manager (optional).

If you have a different method of managing Composer dependencies, use that.

How to install Flysystem in Drupal:-

These are the basic steps in following below-

1. Download and install Composer_manager module.
2. Enter into /scripts directory inside composer_manager module directory and run init.sh in terminal. This registers the module's Composer command for Drupal core.
3. Download Flysystem module and place in /modules directory of drupal 8 setup.
4. Enter in /core directory in terminal and run “composer drupal-rebuild” and then “composer update” commands. This will install the dependencies required for the modules.
5. Install Flysystem module from drupal backend

Available adapters are:

- local
- ftp (Requires the ftp extension)
- dropbox ( https://www.drupal.org/project/flysystem_dropbox )
- rackspace ( https://www.drupal.org/project/flysystem_rackspace )
- aws s3 ( https://www.drupal.org/project/flysystem_s3 )
- sftp ( https://www.drupal.org/project/flysystem_sftp )
- zip ( https://www.drupal.org/project/flysystem_zip )

How to Configure:-

Stream wrappers are configured in settings.php.

$schemes = [
  'dropboxexample' => [
    'driver' => 'dropbox',
    'config' => [
      'token' => '[dropbox-token]',
      'client_id' => [dropbox-email-id],

      // Optional.
      'prefix' => '[/dropbox subdirectory]',
    ],
  ],
  's3example' => [
    'type' => 's3',
    'driver' => 's3',
    'config' => [
      'key'    => '[S3 access key]',
      'secret' => '[S3 secret key]',
      'region' => '[region like eu-west-1]',
      'bucket' => '[bucket-name]',

      // Optional.
      'prefix' => '[S3 subdirectory]',  // Directory prefix for all uploaded/viewed files.
      'cname' => '[S3 url]',   // A cname that resolves to your bucket. Used for URL generation.
    ],
  ],
  'localexample' => [            // The name of the stream wrapper. localexample://

    'driver' => 'local',         // The plugin key.

    'config' => [
      'root' => '[path to the file storage]',  // If 'root' is inside the public directory,
    ],                           // then files will be served directly. Can be
                                 // relative or absolute.

    // Optional settings that apply to all adapters.

    'cache' => FALSE,             // Cache filesystem metadata. Not necessary,
                                 // since this is a local filesystem.

    'replicate' => 'dropboxexample', // 'replicate' writes to both filesystems, but
                                 // reads from this one. Functions as a backup.

    'serve_js' => TRUE,          // Serve Javascript or CSS via this stream wrapper.
    'serve_css' => TRUE,         // This is useful for adapters that function as
                                 // CDNs like the S3 adapter.
  ]
];

$settings['flysystem'] = $schemes;

The above configuration will add storage options to Flysystem configuration.

How it works:

1) Sync local storage with remote

a) Go to /admin/config/media/file-system/flysystem.
b) Select the Sync From and Sync To from the dropdown and Sync.

2) Store file remote directly

a) Go to manage field link of content type .
b) edit the field setting and change the upload destination.

Installation and Configuring Flysystem s3 module

Installation :

1. Install Flysystem and composer_manager.
2. Download Flysystem S3 module and place in /modules.
3. Enter in /core directory in terminal
4. Run “composer drupal-rebuild” and “composer update” command in terminal
5. Install Flysystem S3 from drupal backend

Configuration :

Add the S3 scheme configuration in settings.php file.

$schemes = [
's3example' => [
    'type' => 's3',
    'driver' => 's3',
    'config' => [
      'key'    => '[S3 access key]',
      'secret' => '[S3 secret key]',
      'region' => '[region like eu-west-1]',
      'bucket' => '[bucket-name]',

      // Optional.
      'prefix' => '[S3 subdirectory]',  // Directory prefix for all uploaded/viewed files.
      'cname' => '[S3 url]',   // A cname that resolves to your bucket. Used for URL generation.
    ],
  ],
];

$settings['flysystem'] = $schemes;

Flysystem Scheme and file access control
.
Flysystem storage schemes works like private files in drupal 8.

It hides the actual link of the to file and delivers it via drupal generated path.Thus it restricts access to the original files and let other modules provide access control to the file.

But while using image style it provide access control only if the file is stored as private because the image style is rendered using ImageStyleDownloadController::deliver method and this method let other module to provide access control to the file if the file is stored as private file only.

// If using the private scheme, let other modules provide headers and
    // control access to the file.
    if ($scheme == 'private') {
      if (file_exists($derivative_uri)) {
        return parent::download($request, $scheme);
      }
      else {
        $headers = $this->moduleHandler()->invokeAll('file_download', array($image_uri));
        if (in_array(-1, $headers) || empty($headers)) {
          throw new AccessDeniedHttpException();
        }
      }
    }
📌 Task
Status

Fixed

Version

2.1

Component

Documentation

Created by

🇮🇳India rubela_shome

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024