For a project with Symfony/API Platform I'm working on I need to set up 1 Amazon S3 bucket per region, so 1 bucket for EU, 1 for US, ...
When a user posts a file, the api first has to determine the region of the user, then decide which bucket to use, then upload the file.
So, I created a second document adapter and filesystem in the knp_gauffrette config:
knp_gaufrette:
stream_wrapper: ~
adapters:
document_adapter_eu:
aws_s3:
service_id: ct_file_store_eu.s3
bucket_name: '%env(AWS_BUCKET_EU)%'
detect_content_type: true
options:
create: true
directory: 'files/%env(AWS_S3_ENVIRONMENT)%'
document_adapter_us:
aws_s3:
service_id: ct_file_store_us.s3
bucket_name: '%env(AWS_BUCKET_US)%'
detect_content_type: true
options:
create: true
directory: 'files/%env(AWS_S3_ENVIRONMENT)%'
filesystems:
document_fs_eu:
adapter: document_adapter_eu
document_fs_us:
adapter: document_adapter_us
Then for my vich_uploader config I have the following:
vich_uploader:
db_driver: orm
storage: gaufrette
mappings:
file_eu:
inject_on_load: true
uri_prefix: "%env(AWS_BASE_URL_EU)%files/%env(AWS_S3_ENVIRONMENT)%"
delete_on_update: false
delete_on_remove: false
upload_destination: document_fs_eu
namer: App\Service\FileNamer
file_us:
inject_on_load: true
uri_prefix: "%env(AWS_BASE_URL_US)%files/%env(AWS_S3_ENVIRONMENT)%"
delete_on_update: false
delete_on_remove: false
upload_destination: document_fs_us
namer: App\Service\FileNamer
Then on my File entity I have this config:
/**
* @Vich\Uploadable()
*/
#[ORM\Entity]
#[ApiResource(
collectionOperations: [
'post' => [
'controller' => CreateFileAction::class,
'path' => '/metadata/{id}/file',
'deserialize' => false,
'validation_groups' => ['Default', 'file_create'],
'openapi_context' => [
'parameters' => [
[
'name' => 'id',
'in' => 'path',
'required' => true
]
],
'requestBody' => [
'content' => [
'multipart/form-data' => [
'schema' => [
'type' => 'object',
'properties' => [
'file' => [
'type' => 'string',
'format' => 'binary',
],
],
],
],
],
],
],
],
],
iri: 'http://schema.org/MediaObject',
itemOperations: [
'get' => [
'path' => '/metadata/files/{id}'
]
],
normalizationContext: ['groups' => ['file:read']]
)]
class File
{
use UuidTrait;
/**
* @Vich\UploadableField(mapping="file_eu", fileNameProperty="filePath")
*/
#[Assert\NotNull(groups: ['file_create'])]
public ?File $file = null;
#[ORM\Column(nullable: true)]
public ?string $filePath = null;
}
This obviously works for the EU-mapping, but what I would like to do is to change the @vich/UploadableField
mapping in my CreateFileAction
controller based on the users's region; Is anything like this possible? If not, any suggestions?