1

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?

MrSkippy
  • 348
  • 4
  • 17
  • 1
    I'm not really an expert with this bundle, yet, my intuition tells me to try to set up a listener to this event: https://github.com/dustin10/VichUploaderBundle/tree/master/src/Event then maybe try (through breakpoint/debugger) to check what you have in container (like in here https://github.com/dustin10/VichUploaderBundle/blob/master/src/DependencyInjection/VichUploaderExtension.php#L43) and see if you can "switch" the mapper based on the client's IP location.. – medunes Feb 02 '22 at 22:31
  • Thanks for your reply. It’s not based on the user’s ip location, but the region that is in the user’s JWT-token (not really relevant for this question). Nonetheless, that’s somerhing I can work with, thank you! – MrSkippy Feb 03 '22 at 00:43
  • 1
    the thanks button in stackoverflow is the upvote button :) but also glad it helped anyway, good luck! – medunes Feb 03 '22 at 00:51

0 Answers0