1

I'm using Symfony3 with the KnpGaufretteBundle to connect to an Amazon S3 bucket with the AWS S3 method outlined on their Github Readme

aws_s3_adapter:
  key:        "%aws_key%"
  secret_key: "%aws_secret%"
  region:     "%aws_region%"

knp_gaufrette:
  adapters:
    images:
        aws_s3:
            service_id: 'aws_s3_adapter.client'
            bucket_name: '%aws_bucket%'
            options:
                directory: 'images'
  filesystems:
    images:
        adapter: images
        alias:   images_fs

I also have a service defined that I want to use to manage this filesystem (and others) with.

Definition:

services:
  test.image_manager:
    class: TestBundle\Filesystem\FileManager
    arguments: 
      filesystem: "@images_fs"
      filesystem_name: "images"
      mimetypes: ["image/jpeg", "image/png", "image/gif"]

Class:

<?php

namespace TestBundle\Filesystem;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

use Gaufrette\Filesystem;
use Gaufrette\StreamWrapper;

class FileManager
{
    private $allowedMimeTypes;

    private $filesystem;

    private $filsystem_name;

    public function __construct(Filesystem $filesystem, $filesystem_name, $mimetypes = array())
    {
        $this->filesystem = $filesystem;
        $this->filesystem_name = $filesystem_name;
        $this->allowedMimeTypes = $mimetypes;
    }

    public function upload(UploadedFile $file, $filename)
    {
        // Check if the file's mime type is in the list of allowed mime types.
        if (!in_array($file->getClientMimeType(), $this->allowedMimeTypes)) {
            throw new \InvalidArgumentException(sprintf('Files of type %s are not allowed.', $file->getClientMimeType()));
        }

        $adapter = $this->filesystem->getAdapter();
        $adapter->setMetadata($filename, array('contentType' => $file->getClientMimeType()));
        return $adapter->write($filename, file_get_contents($file->getPathname()));
    }

    public function fetch( $filename )
    {
        if( ! $this->filesystem->has( $filename ) )
        {
            return false;
        }


        /* -- PROBLEM -- */

        StreamWrapper::register();
        return new BinaryFileResponse( "gaufrette://{$this->filesystem_name}/{$filename}" );

        /* -- PROBLEM -- */

    }

    public function delete( $filename )
    {
        if( ! $this->filesystem->has( $filename ) )
        {
            return false;
        }
        return $this->filesystem->delete( $filename );
    }
}

I'm able to upload successfully to the bucket using the upload function, telling me that the filesystem exists and is working properly.

I am not, however, able to use the Gaufrette\StreamWrapper to serve the file using a BinaryFileResponse as it says I should do. Instead it is giving me the error that I put in the title: There is no filesystem defined for the "images" domain.

The filesystem definitely exists, as I'm using it to upload the images. Any clues as to what the problem might be that's preventing me from using that filesystem would be very helpful. The Gaufrette documentation is really sparse online so far as I've found, but I'm going to keep digging.

Scott M
  • 417
  • 5
  • 16

1 Answers1

1

Looking at MainConfiguration.php showed that there's a steam_wrapper option in the configuration for the bundle. I added this into my config.yml under where the filesystems are defined like so:

knp_gaufrette:
  adapters:
    images:
        aws_s3:
            service_id: 'aws_s3_adapter.client'
            bucket_name: '%aws_bucket%'
            options:
                directory: 'images'
    .
    .
    .
  filesystems:
    images:
        adapter: images
        alias:   images_fs
    .
    .
    .
  stream_wrapper:
    filesystems: [ images, ... ]

and the above code now works.

Scott M
  • 417
  • 5
  • 16