/** * 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) : '/')); } }
/** * Create a canonicalized resource for a request * * @param RequestInterface $request Request for the resource * * @return string */ private function createCanonicalizedResource(RequestInterface $request) { $buffer = $request->getParams()->get('s3.resource'); // When sending a raw HTTP request (e.g. $client->get()) if (null === $buffer) { $bucket = $request->getParams()->get('bucket') ?: $this->parseBucketName($request); // Use any specified bucket name, the parsed bucket name, or no bucket name when interacting with GetService $buffer = $bucket ? "/{$bucket}" : ''; // Remove encoding from the path and use the S3 specific encoding $path = S3Client::encodeKey(rawurldecode($request->getPath())); // if the bucket was path style, then ensure that the bucket wasn't duplicated in the resource $buffer .= preg_replace("#^/{$bucket}/{$bucket}#", "/{$bucket}", $path); } // Remove double slashes $buffer = str_replace('//', '/', $buffer); // Add sub resource parameters $query = $request->getQuery(); $first = true; foreach ($this->signableQueryString as $key) { if ($query->hasKey($key)) { $value = $query[$key]; $buffer .= $first ? '?' : '&'; $first = false; $buffer .= $key; // Don't add values for empty sub-resources if ($value !== '' && $value !== false && $value !== null && $value !== QueryString::BLANK) { $buffer .= "={$value}"; } } } return $buffer; }
/** * Get a query string authenticated URL * * @param string $path Object URI * * @return string */ public function getAuthenticatedURL($path) { // Pre-signed URLs need to use the old S3 signature method. Therefore we need a new S3Client object. $amazonCredentials = new Credentials(self::$accessKey, self::$secretKey); $clientOptions = array('credentials' => $amazonCredentials, 'scheme' => self::$useSSL ? 'https' : 'http', 'signature' => 's3'); if (self::$useSSL) { $clientOptions['ssl.certificate_authority'] = realpath(JPATH_LIBRARIES . '/fof30/Download/Adapter/cacert.pem'); } else { $clientOptions['ssl.certificate_authority'] = false; } // Create the S3 client instance $s3Client = S3Client::factory($clientOptions); return $s3Client->getObjectUrl(self::$bucket, $path, '+' . self::$timeForSignedRequests . ' seconds'); }