Symfony: declare service with multidimensional array yaml DI

I am using symfony 5.1 and I want to create an S3Client as a service to be able to use it on other services.

Without services it is initialized lilke this:

$s3client = new S3Client([
    'credentials' => [
        'key'     => 'your_key_here',
        'secret'  => 'your_secret',
    ],
    'version' => 'latest',
    'region'  => 'eu-west-1',
]);

The argument is a multidimensional array.

I’m not sure how to map an array as an argument in a service declaration, so I checked the symfony documentation and I reached a configuration like this:

// services.yaml
app.client.amazonS3:
class: AwsS3S3Client
factory: [AwsS3S3Client, 'factory']
arguments:
    - credentials:
      - key: '%env(AWS_KEY)%'
      - secret: '%env(AWS_SECRET_KEY)%'
    - version: '%env(AWS_S3_VERSION)%'
    - region: '%env(AWS_S3_REGION)%'

It seems to work since I try to upload a file, then I get an error that says that there are Missing required client configuration options, concretely it says that I have to declare region and version options.

It is not recognizing region and version, so surely my array argument is not well formed. How should I form this array?

I’ve also tried something like this:

app.client.amazonS3:
    class: AwsS3S3Client
    factory: [AwsS3S3Client, 'factory']
    arguments:
      -
        credentials:
          - key: '%env(AWS_KEY)%'
          - secret: '%env(AWS_SECRET_KEY)%'
        version: '%env(AWS_S3_VERSION)%'
        region: '%env(AWS_S3_REGION)%'

But with this I get the error:

Credentials must be an instance of AwsCredentialsCredentialsInterface, an associative array that contains "key", "secret", and an optional "token" key-value pairs, a credentials provider function, or false.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *