What is the syntax for using a contrib module's service from within a hook implementation? I'm trying to get a list of objects from AWS using the S3fsService from the S3 File System module from within a hook_cron() implementation. The following code works:
// Fetch list of files in S3.
$config = \Drupal::config('s3fs.settings')-&gt;get();
$s3 = \Drupal\s3fs\S3fsService::getAmazonS3Client($config);
$iterator_args = ['Bucket' =&gt; $config['bucket']];
$iterator = $s3-&gt;getIterator('ListObjectVersions', $iterator_args);

…but I'm getting a message saying Deprecated function: Non-static method Drupal\s3fs\S3fsService::getAmazonS3Client() should not be called statically….
How should I be calling it differently?
Thanks!
You can access services via \Drupal::service('service.name').
You need to know the service's name to access it, though. Looking at the s3fs module, I can see in the module's s3fs.services.yml file that the class your requesting is available with the service name s3fs.
$s3fs = \Drupal::service('s3fs');
$result = $s3fs-&gt;getAmazonS3Client($config);