OneupFlysystemBundle Documentation

repository·main·Indexed 20 days ago

https://github.com/1up-lab/oneupflysystembundle

A Symfony bundle that integrates the Flysystem filesystem abstraction library, allowing developers to switch between storage backends like local, S3, Azure, Google Cloud Storage, FTP, SFTP, and GitLab using a unified API. The bundle supports Flysystem V2 & V3 (via the 4.x branch) and Flysystem 1.x (via the 3.x branch).

Tokens
8.7K
Snippets
30
Records
35
Agent score
69%

What's inside OneupFlysystemBundle

  1. Overview of OneupFlysystemBundle

    main

    OneupFlysystemBundle provides a Symfony integration for Flysystem, a filesystem abstraction library. It allows you to swap local filesystems for remote ones by configuring different adapters within your Symfony project.

    Supported adapters include:

    • Google Cloud Storage
    • AsyncAwsS3
    • AwsS3
    • AzureBlobStorage
    • Ftp
    • Local filesystem
    • Sftp
  2. Set up the development environment for OneupFlysystemBundle

    main

    To run unit tests or development tools, first clone the repository and install the development dependencies using Composer.

    git clone git://github.com/1up-lab/OneupFlysystemBundle.git
    cd OneupFlysystemBundle
    composer install --dev
    git clone git://github.com/1up-lab/OneupFlysystemBundle.git
    composer install --dev
  3. Enable OneupFlysystemBundle in the Symfony Kernel

    main

    Register the bundle in your application's kernel (e.g., app/AppKernel.php) to make it available.

    <?php
    // app/AppKernel.php
    
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Oneup\FlysystemBundle\OneupFlysystemBundle(),
        );
    }
  4. Configure the SFTP adapter

    main

    To use the SFTP adapter in oneup_flysystem, you must define an adapter configuration under the oneup_flysystem.adapters key. At a minimum, the host key must be provided within the options block. Other parameters such as username and root can also be configured to define the connection and the base directory for the filesystem.

    # app/config/config.yml
    oneup_flysystem:
        adapters:
            my_adapter:
                sftp:
                    options:
                        host: 'ftp.domain.com'
                        username: 'foo'
                        root: '/upload'
  5. Create and use your filesystems

    main

    To create a filesystem, define it under the filesystems key in your oneup_flysystem configuration. You must specify an adapter (the service ID of your configured adapter).

    Once configured, the bundle automatically creates a service for your filesystem using the naming convention: oneup_flysystem.%s_filesystem, where %s is the name of your configuration key.

    The resulting service implements \League\Flysystem\Filesystem (or FilesystemOperator).

    oneup_flysystem:
        adapter: ~
        filesystems:
            acme:
                adapter: my_adapter
                alias: ~
                mount: ~
                visibility: ~
                directory_visibility: ~
    // Accessing the filesystem via the container
    $filesystem = $container->get('oneup_flysystem.acme_filesystem');
  6. Inject your filesystem in your services

    main

    Instead of using the service locator (container), it is recommended to inject filesystems directly into your services via Dependency Injection.

    When injecting, use the resolved service name (e.g., @oneup_flysystem.acme_filesystem) or the alias you defined. In your PHP class, type-hint against \League\Flysystem\FilesystemOperator.

    Symfony 4.2+ Tip: You can use automatic dependency injection by type-hinting the specific filesystem. For a filesystem named acme, you can type-hint AcmeFilesystem (or similar depending on your autowiring setup) if you leverage ContainerBuilder::registerAliasForArgument().

    services:
        app.my_service:
            class: App\MyService
            arguments:
                - '@oneup_flysystem.acme_filesystem'
    use League\Flysystem\FilesystemOperator;
    
    class MyService
    {
        private FilesystemOperator $filesystem;
    
        public function __construct(FilesystemOperator $acmeFilesystem)
        {
            $this->filesystem = $acmeFilesystem;
        }
    }
  7. Configure the AwsS3v3 adapter

    main

    To use the awss3v3 adapter, you must first define an Aws\S3\S3Client as a service in your Symfony configuration. This adapter requires the aws/aws-sdk-php package (version 3 or higher) and uses the 'v4' signature.

    You must also define a League\Flysystem\AwsS3V3\PortableVisibilityConverter service to handle file visibility.

    Once the services are defined, register the adapter under oneup_flysystem.adapters by referencing the S3 client service in the client key.

    services:
        acme.awss3v3.portable_visibility_converter:
            class: League\Flysystem\AwsS3V3\PortableVisibilityConverter
    
        acme.s3_client:
            class: Aws\S3\S3Client
            arguments:
                -
                    version: '2006-03-01' # or 'latest'
                    region: "region-id"
                    credentials:
                        key: "s3-key"
                        secret: "s3-secret"
    
    oneup_flysystem:
        adapters:
            acme.flysystem_adapter:
                awss3v3:
                    client: acme.s3_client
                    bucket: 'bucket-name'
                    prefix: 'path/prefix' # Optional path prefix
                    visibilityConverter: acme.awss3v3.portable_visibility_converter
  8. Pass additional parameters to AWS S3 via options

    main

    You can pass arbitrary parameters to the underlying AWS SDK by using the options configuration key within your adapter definition. This is useful for setting specific AWS settings like ACL (Access Control List).

    For example, if you are uploading files to an S3 bucket owned by a different AWS account, you may need to set the ACL to bucket-owner-full-control so the bucket owner can access the objects.

    oneup_flysystem:
        adapters:
            acme.flysystem_adapter:
                awss3v3:
                    client: acme.s3_client
                    bucket: 'bucket-name'
                    prefix: 'path/prefix'
                    options:
                        ACL: bucket-owner-full-control
  9. Use the AsyncAwsS3 adapter

    main

    To use the async_aws_s3 adapter, you must first define an AsyncAws\S3\S3Client service in your Symfony service container. You can configure this service manually or use the AsyncAws SymfonyBundle.

    Once the client is defined, you must also provide a visibilityConverter (typically League\Flysystem\AsyncAwsS3\PortableVisibilityConverter) to handle file visibility settings.

    Finally, register the adapter under oneup_flysystem.adapters by referencing your S3 client service via the client key.

    # 1. Define the S3 Client and Visibility Converter services
    services:
        acme.async.portable_visibility_converter:
            class: League\Flysystem\AsyncAwsS3\PortableVisibilityConverter
    
        acme.async_s3_client:
            class: AsyncAws\S3\S3Client
            arguments:
                - region: 'eu-central-1'
                  accessKeyId: 'AKIAIOSFODNN7EXAMPLE'
                  accessKeySecret: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
    
    # 2. Configure the OneupFlysystem adapter
    oneup_flysystem:
        adapters:
            acme.flysystem_adapter:
                async_aws_s3:
                    client: acme.async_s3_client
                    bucket: 'my_image_bucket'
                    prefix: ''
                    visibilityConverter: acme.async.portable_visibility_converter
  10. Configure the GitLab adapter

    main

    To use the GitLab adapter, you must first define a GitLab client service in your Symfony service container and then reference that service within the oneup_flysystem configuration.

    1. Define the GitLab client service: Create a service using the RoyVoetman\FlysystemGitlab\Client class. You must provide the project-id, branch, base-url, and personal-access-token as arguments.
    2. Register the adapter: In your oneup_flysystem configuration, define an adapter under adapters and set the client key to the service ID of your GitLab client. You can optionally provide a prefix for a path prefix within the repository.
    # 1. Define the GitLab client service
    services:
        acme.gitlab_client:
            class: RoyVoetman\FlysystemGitlab\Client
            arguments:
                - 'project-id'
                - 'branch'
                - 'base-url'
                - 'personal-access-token'
    
    # 2. Configure the OneupFlysystem adapter
    oneup_flysystem:
        adapters:
            acme.gitlab_adapter:
                gitlab:
                    client: acme.gitlab_client
                    prefix: 'optional/path/prefix'