There is not a direct way into VichUploader, but it is possible to tell Symfony to use a custom Storage and add the plugin logic inside there.
Install presigned-URLs plugin for Flysystem
composer require sistemi-etime/flysystem-plugin-aws-s3-v3
Use the plugin with the Flysystem, via the bundle oneup_flysystem.yaml
config:
oneup_flysystem:
filesystems:
aws_s3:
...
...
plugins:
- s3.plugin.presigned_url
Define your own Storage
<?php
use Vich\UploaderBundle\Storage\FlysystemStorage;
class CustomFlysystemStorage extends FlysystemStorage
{
// Copy the parent method code but then return the presigned URL
public function resolveUri($obj, ?string $fieldName = null, ?string $className = null)
{
[$mapping, $filename] = $this->getFilename($obj, $fieldName, $className);
if (empty($filename)) {
return null;
}
$dir = $mapping->getUploadDir($obj);
$path = !empty($dir) ? $dir.'/'.$filename : $filename;
$fs = $this->getFilesystem($mapping, $obj, $fieldName, $className);
return $fs->getPresignedUrl($path);
}
}
Make use of it inside services.yaml
:
# alias the custom storage to be used instead of the one defined by VichUploader
vich_uploader.storage.flysystem: '@Your\Own\Namespace\CustomFlysystemStorage'
Fix autowiring (why? 🤷♂️)
[Edit: that's why]
Additionally I had to explicitly alias the arguments required by the FlysystemStorage
in order for Symfony Autowire to initialise the custom storage:
Vich\UploaderBundle\Mapping\PropertyMappingFactory: '@vich_uploader.property_mapping_factory'
League\Flysystem\MountManager: '@oneup_flysystem.mount_manager'
Although I am not sure why this was necessary.
I would expect that either vich_uploader.property_mapping_factory
and oneup_flysystem.mount_manager
are already defined and aliased by the configs from VichUploaderBundle and OneupFlysystemBundle
If anybody knows how to skip this fix, please comment.
resolveUri
method toreturn $filesystem->getPresignedUrl($path)
and then override thevich_uploader.storage.flysystem
with the custom storage class, intoservices.yaml
. – Kamafeather