Symfony 两个 AWS_S3 配置

问题描述 投票:0回答:1

在我的 Symfony 5 项目中,我需要有两个不同的 AWS_S3 配置。

我到目前为止所做的步骤:

  1. 创建了扩展基类 FileSystem 的类“NewFileSystem”。
<?php

namespace App\Factory;

use League\Flysystem\Filesystem;

class NewFileSystem extends Filesystem
{
}
  1. 在 services.yaml 中添加配置
services:
    usr_data.s3_client:
        class: Aws\S3\S3Client
        arguments:
        -
            version: 'latest'
            region: "%env(AWS_S3_REGION)%"
            credentials:
                key: "%env(AWS_S3_KEY)%"
                secret: "%env(AWS_S3_SECRET)%"

    usr_data.s3_legacy_client:
        class: Aws\S3\S3Client
        arguments:
            - version: 'latest'
              region: "%env(AWS_S3_LEGACY_REGION)%"
              credentials:
                  key: "%env(AWS_S3_LEGACY_KEY)%"
                  secret: "%env(AWS_S3_LEGACY_SECRET)%"

    League\Flysystem\Filesystem: "@oneup_flysystem.s3_filesystem_filesystem"
    App\Factory\NewFileSystem: "@oneup_flysystem.s3_legacy_filesystem"
  1. 在oneup_flysystem.yaml中添加配置
oneup_flysystem:
    adapters:
        default_adapter:
            local:
                directory: '%kernel.root_dir%/../data/invoice-attachment'
        tutorial_adapter:
            local:
                directory: '%kernel.root_dir%/../public/assets/tutorial'
        permissions_adapter:
            local:
                directory: '%kernel.root_dir%/../public/assets/permissions'
        images_adapter:
            local:
                directory: '%kernel.root_dir%/../public/assets/images'
        awsS3_adapter_usr_data:
            awss3v3:
                client: usr_data.s3_client
                bucket: "%env(AWS_S3_UPLOAD_BUCKET)%"
                prefix: ~
        awsS3_legacy_usr_data:
            awss3v3:
                client: usr_data.s3_legacy_client
                bucket: "%env(AWS_S3_LEGACY_UPLOAD_BUCKET)%"
                prefix: ~
    filesystems:
        default_filesystem:
            adapter: default_adapter
            alias: my_filesystem
        tutorial_filesystem:
            adapter: tutorial_adapter
        permissions_filesystem:
            adapter: permissions_adapter
        images_filesystem:
            adapter: images_adapter
        s3_filesystem:
            adapter: awsS3_adapter_usr_data
            alias: League\Flysystem\Filesystem
        s3_legacy:
            adapter: awsS3_legacy_usr_data
            alias: App\Factory\NewFileSystem
  1. 通过控制器中的依赖注入来注入新类
    public function createAction(Request $request, NewFileSystem $fileSystem): BaseOutput
    {
        dd($fileSystem);
    }

我有以下错误。

参数 2 传递给 应用\控制器\Api\迁移\ProcessController::createAction() 必须是 App\Factory\NewFileSystem 的实例, 给出联盟\Flysystem\文件系统

为什么系统不想注入类“NewFileSystem”。 我还应该配置什么才能拥有具有不同 s3 配置的新类。

谢谢你。

php symfony
1个回答
0
投票

我认为你尝试将文件系统注入控制器的方式有点混乱。 根据 TheLeague 文档 以及 OneUp 文档,并假设您使用自动装配:

你的服务定义应该改为:

oneup_flysystem:
    adapters:
        default_adapter:
            local:
                directory: '%kernel.root_dir%/../data/invoice-attachment'
        tutorial_adapter:
            local:
                directory: '%kernel.root_dir%/../public/assets/tutorial'
        permissions_adapter:
            local:
                directory: '%kernel.root_dir%/../public/assets/permissions'
        images_adapter:
            local:
                directory: '%kernel.root_dir%/../public/assets/images'
        awsS3_adapter_usr_data:
            awss3v3:
                client: usr_data.s3_client
                bucket: "%env(AWS_S3_UPLOAD_BUCKET)%"
                prefix: ~
        awsS3_legacy_usr_data:
            awss3v3:
                client: usr_data.s3_legacy_client
                bucket: "%env(AWS_S3_LEGACY_UPLOAD_BUCKET)%"
                prefix: ~
    filesystems:
        default_filesystem:
            adapter: default_adapter
            alias: my_filesystem
        tutorial_filesystem:
            adapter: tutorial_adapter
        permissions_filesystem:
            adapter: permissions_adapter
        images_filesystem:
            adapter: images_adapter
        s3_filesystem:
            adapter: awsS3_adapter_usr_data
            alias: League\Flysystem\Filesystem
        s3_legacy:
            adapter: awsS3_legacy_usr_data
            # mind the change here <<<<<<<<<<<<<<
            alias: s3_legacy_filesystem

你的控制器的代码应该如下所示:

use League\Flysystem\FilesystemOperator;
{
    //...
    // The variable name $s3LegacyFilesystem matters
    public function createAction(Request $request, FilesystemOperator $s3LegacyFilesystem)
    {
        // ...
    }
}

现在您不再需要整个 NewFileSystem 类。

© www.soinside.com 2019 - 2024. All rights reserved.