array('onCommandAfterPrepare', -255)); } /** * Changes how buckets are referenced in the HTTP request * * @param Event $event Event emitted */ public function onCommandAfterPrepare(Event $event) { $command = $event['command']; $bucket = $command['Bucket']; $request = $command->getRequest(); $pathStyle = false; if ($key = $command['Key']) { // Modify the command Key to account for the {/Key*} explosion into an array if (is_array($key)) { $command['Key'] = $key = implode('/', $key); } } // Set the key and bucket on the request $request->getParams()->set('bucket', $bucket)->set('key', $key); // Switch to virtual if PathStyle is disabled, or not a DNS compatible bucket name, or the scheme is // http, or the scheme is https and there are no dots in the host header (avoids SSL issues) if (!$command['PathStyle'] && $command->getClient()->isValidBucketName($bucket) && !($command->getRequest()->getScheme() == 'https' && strpos($bucket, '.')) ) { // Switch to virtual hosted bucket $request->setHost($bucket . '.' . $request->getHost()); $request->setPath(preg_replace("#^/{$bucket}#", '', $request->getPath())); } else { $pathStyle = true; } if (!$bucket) { $request->getParams()->set('s3.resource', '/'); } elseif ($pathStyle) { // Path style does not need a trailing slash $request->getParams()->set( 's3.resource', '/' . rawurlencode($bucket) . ($key ? ('/' . S3Client::encodeKey($key)) : '') ); } else { // Bucket style needs a trailing slash $request->getParams()->set( 's3.resource', '/' . rawurlencode($bucket) . ($key ? ('/' . S3Client::encodeKey($key)) : '/') ); } } }