· 6 years ago · Aug 12, 2019, 09:34 AM
1/**
2 * Factory method to create a new S3 client
3 *
4 * @param array|Collection $config Configuration data. Array keys:
5 * base_url - Base URL of web service. Default: {{scheme}}://{{region}}/
6 * scheme - Set to http or https. Defaults to http
7 * region - AWS region. Defaults to s3.amazonaws.com
8 * access_key - AWS access key ID. Set to sign requests.
9 * secret_key - AWS secret access key. Set to sign requests.
10 *
11 * @return S3Client
12 */
13 public static function factory($config)
14 {
15 $defaults = array('base_url' => '{{scheme}}://{{region}}/', 'region' => self::REGION_DEFAULT, 'scheme' => 'http');
16 $required = array('region', 'scheme');
17 $config = Inspector::prepareConfig($config, $defaults, $required);
18 // Filter our the Timestamp and Signature query string values from cache
19 $config->set('cache.key_filter', 'header=Date, Authorization; query=Timestamp, Signature');
20 // If an access key and secret access key were provided, then the client
21 // requests will be authenticated
22 if ($config->get('access_key') && $config->get('secret_key')) {
23 $signature = new S3Signature($config->get('access_key'), $config->get('secret_key'));
24 }
25 $client = new self($config->get('base_url'), $config->get('access_key'), $config->get('secret_key'), null, $signature);
26 $client->setConfig($config);
27 // If signing requests, add the request signing plugin
28 if ($signature) {
29 $client->getEventManager()->attach(new SignS3RequestPlugin($signature), -99999);
30 }
31 // Retry 500 and 503 failures using exponential backoff
32 $client->getEventManager()->attach(new ExponentialBackoffPlugin());
33 // If Amazon DevPay tokens were provided, then add a DevPay filter
34 if ($config->get('devpay_user_token') && $config->get('devpay_product_token')) {
35 // Add the devpay plugin pretty soon in the event emissions
36 $client->getEventManager()->attach(new DevPayPlugin($config->get('devpay_user_token'), $config->get('devpay_product_token')), 9999);
37 }
38 return $client;
39 }