Я нашел решение. Я сделал фильтр веток , чтобы получить путь к файлу, загруженному с помощью gaufrette. Решения, используемые для Amazon S3 и локальный .
приложение \ Config \ services.yml
app.aws_s3.client:
class: Aws\S3\S3Client
arguments:
-
version: '%amazon_s3.version%'
region: '%amazon_s3.region%'
credentials:
key: '%amazon_s3.key%'
secret: '%amazon_s3.secret%'
html.twig
<a class="btn btn-primary" href="{{ pdf|get_path }}" target="_blank">Afficher le PDF actuel</a>
AppBundle \ Resources \ Config \ services.yml
app.twig_filter_extension:
class: AppBundle\Twig\AppFilterExtension
public: true
arguments:
- '%kernel.environment%'
- '%prod_env%'
- '%kernel.root_dir%'
- '@router.request_context'
- '@app.aws_s3.client'
- '%amazon_s3.media_bucket%'
tags:
- { name: twig.extension }
AppBundle \ Twig \ AppFilterExtension.php
<?php
namespace AppBundle\Twig;
use Aws\S3\S3Client;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Routing\RequestContext;
class AppFilterExtension extends \Twig_Extension
{
/**
* @var string
*/
private $env;
/**
* @var bool
*/
private $prodEnv;
/**
* @var S3Client
*/
private $s3Client;
/**
* @var string
*/
private $s3Bucket;
/**
* @var Filesystem
*/
private $fileSystem;
/**
* @var string
*/
private $rootPath;
/**
* @var string
*/
private $baseUrl;
/**
* AppFilterExtension constructor.
*
* @param string $env
* @param bool $prodEnv
*/
public function __construct(string $env, bool $prodEnv, string $rootDir, RequestContext $requestContext, S3Client $s3Client, string $s3Bucket)
{
$this->env = $env;
$this->prodEnv = $prodEnv;
$this->fileSystem = new Filesystem();
$this->s3Client = $s3Client;
$this->s3Bucket = $s3Bucket;
$this->rootPath = $rootDir . '/../web/';
$this->baseUrl = sprintf('%s://%s', $requestContext->getScheme(), $requestContext->getHost());
}
public function getFilters()
{
return [
new \Twig_SimpleFilter('get_path', [$this, 'getPath']),
];
}
/**
* @param string $uri
* @param null|string $folder
*
* @return string
*/
public function getPath(string $uri, string $folder = null)
{
if (
(
'dev' === $this->env ||
false === $this->prodEnv
) &&
true === $this->fileSystem->exists(str_replace('//', '/', sprintf('%s/%s/%s', $this->rootPath, ($folder ?? 'media'), $uri)))
) {
return $this->baseUrl . str_replace('//', '/', sprintf('/%s/%s', ($folder ?? 'media'), $uri));
}
return str_replace('//', '/', $this->s3Client->getObjectUrl($folder ?? $this->s3Bucket, $uri));
}
/**
* @return string
*/
public function getName()
{
return 'app_filter_extension';
}
}