在我的 Symfony 5 项目中,我需要有两个不同的 AWS_S3 配置。
我到目前为止所做的步骤:
<?php
namespace App\Factory;
use League\Flysystem\Filesystem;
class NewFileSystem extends Filesystem
{
}
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"
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
public function createAction(Request $request, NewFileSystem $fileSystem): BaseOutput
{
dd($fileSystem);
}
我有以下错误。
参数 2 传递给 应用\控制器\Api\迁移\ProcessController::createAction() 必须是 App\Factory\NewFileSystem 的实例, 给出联盟\Flysystem\文件系统
为什么系统不想注入类“NewFileSystem”。 我还应该配置什么才能拥有具有不同 s3 配置的新类。
谢谢你。
我认为你尝试将文件系统注入控制器的方式有点混乱。 根据 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 类。