Streaming files from AWS S3 with a BinaryFileResponse is really slow

1.4k Views Asked by At

I'm working on a Symfony2.8 project, and using Gaufrette to manage files I have stored in an AWS S3 bucket. My files need to be private, so instead of using S3 public URLs, I am trying to stream the files from S3 to my end user. I have it working with StreamWrapper and BinaryFileResponse, but it is incredibly slow -- a 2.5 MB file takes over 20 seconds, and any larger than that fails due to my web server's 30 second time limit. What am I doing wrong?

Here is the code for the relevant action in my controller class:

/**
 * @Route("/docs/get/{file}", name="document_read")
 */
public function documentReadAction(Request $request, $file)
{
    $filesystem = $this->container->get('knp_gaufrette.filesystem_map')->get('docs');
    $map = StreamWrapper::getFilesystemMap();
    $map->set('docs', $filesystem);
    $filepath = 'gaufrette://docs/'.$file;

    return new BinaryFileResponse($filepath);
}
0

There are 0 best solutions below