public function testAppendUri() { $this->assertEquals('a/b/c', Coder::appendUri('a', 'b', 'c')); $this->assertEquals('a/b/c/', Coder::appendUri('a', '/b/', '/c/')); $this->assertEquals('a/b/c/', Coder::appendUri('a/', '/b/', '/c/')); $this->assertEquals('a/b/foo/bar/c/', Coder::appendUri('a/', '/b/foo//bar//', '/c/')); $this->assertEquals('a/c/', Coder::appendUri('a/', '', '/c/')); $this->assertEquals('a/c/', Coder::appendUri('a/', null, '/c/')); $this->assertEquals('a/', Coder::appendUri('a/', null, null)); $this->assertEquals('/v1', Coder::appendUri('/v1', null, null)); $this->assertEquals('/v1', Coder::appendUri('/v1', '')); }
/** * Generate BCE Authorization Signature * * @param string $method HTTP Request Method. * @param string $resource The resource path. * @param array $params The query strings. * @param array $headers The extra http request headers. * @param number $timestamp The customized timestamp, if it's 0, the will use time() instead. * @param number $expiration_in_seconds The valid time in seconds. * @param array $headers_to_sign The extra http request headers will be included in the signature * * @return string */ public function generateAuthorization($method, $resource, $params = array(), $headers = array(), $timestamp = 0, $expiration_in_seconds = 1800, $headers_to_sign = null) { $raw_session_key = sprintf("bce-auth-v1/%s/%s/%d", $this->credentials->access_key_id, Time::bceTimeNow($timestamp), $expiration_in_seconds); $session_key = hash_hmac("sha256", $raw_session_key, $this->credentials->secret_access_key, false); $canonical_uri = Coder::urlEncodeExceptSlash($resource); $canonical_query_string = $this->queryStringCanonicalization($params); list($canonical_headers, $signed_headers) = $this->headersCanonicalization($headers, $headers_to_sign); $raw_signature = $method . "\n" . $canonical_uri . "\n" . $canonical_query_string . "\n" . $canonical_headers; $signature = hash_hmac("sha256", $raw_signature, $session_key, false); if (count($signed_headers) > 0) { return sprintf('%s/%s/%s', $raw_session_key, implode(';', $signed_headers), $signature); } return sprintf('%s//%s', $raw_session_key, $signature); }
public function testReadRequestBodyFromString() { $grant_list = array(); $grant_list[] = array('grantee' => array(array('id' => 'a0a2fe988a774be08978736ae2a1668b'), array('id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')), 'permission' => array('FULL_CONTROL')); $bucket = 'no-such-bucket-name'; // Prepare the request body $body = json_encode(array('accessControlList' => $grant_list)); $config = json_decode(__BOS_TEST_CONFIG, true); $client = new HttpClient($config); $path = Coder::appendUri(Bce::URL_PREFIX, $bucket); // Create the bucket $client->sendRequest(HttpMethod::PUT, $path, null, array(), array(), array($this, 'generateAuthorization'), null); // Set bucket acl $response = $client->sendRequest(HttpMethod::PUT, $path, $body, array(), array('acl' => ''), array($this, 'generateAuthorization'), null); $this->assertArrayHasKey('http_headers', $response); $this->assertArrayHasKey('body', $response); $this->assertArrayHasKey('x-bce-request-id', $response['http_headers']); $this->assertArrayHasKey('x-bce-debug-id', $response['http_headers']); $this->assertArrayHasKey('content-length', $response['http_headers']); $this->assertArrayHasKey('date', $response['http_headers']); $this->assertArrayHasKey('server', $response['http_headers']); $this->assertEquals(array(), $response['body']); // Delete the bucket $client->sendRequest(HttpMethod::DELETE, $path, null, array(), array(), array($this, 'generateAuthorization'), null); }
/** * @param string $path The bucket and object path. * @param array @param The query strings * * @return string The complete request url path with query string. */ private function getRequestUrl($path, $params) { $uri = Coder::urlEncodeExceptSlash($path); $query_string = implode("&", array_map(function ($k, $v) { return $k . "=" . Coder::urlEncode($v); }, array_keys($params), $params)); if (!is_null($query_string) && $query_string != "") { $uri = $uri . "?" . $query_string; } $parsed_url = parse_url($this->config['endpoint']); $port = ''; $protocol = isset($parsed_url['scheme']) ? $parsed_url['scheme'] : 'http'; if ($protocol !== 'http' && $protocol !== 'https') { throw new \InvalidArgumentException(sprintf("Invalid protocol: %s, either HTTP or HTTPS is expected.", $protocol)); } return sprintf("%s%s", $this->config['endpoint'], $uri); }
public function testMultipartUploadX() { // superfile size is over 1M $file_size = 20 * 1024 * 1024 + 317; $this->client->createBucket($this->bucket); $this->prepareTemporaryFile($file_size); $response = $this->client->initiateMultipartUpload($this->bucket, $this->key); $upload_id = $response['body']['uploadId']; $left_size = filesize($this->filename); $offset = 0; $part_number = 1; $part_list = array(); $etags = ''; while ($left_size > 0) { $part_size = min(BosClient::MIN_PART_SIZE, $left_size); $response = $this->client->uploadPartFromFile($this->bucket, $this->key, $upload_id, $part_number, $part_size, $this->filename, $offset); $this->checkProperties($response); $this->assertEquals(0, $response['http_headers']['content-length']); $part_list[] = array('partNumber' => $part_number, 'eTag' => $response['http_headers']['etag']); $etags .= $response['http_headers']['etag']; $left_size -= $part_size; $offset += $part_size; $part_number += 1; } $response = $this->client->completeMultipartUpload($this->bucket, $this->key, $upload_id, $part_list); $this->checkProperties($response); $this->assertArrayHasKey('location', $response['body']); $this->assertEquals($this->bucket, $response['body']['bucket']); $this->assertEquals($this->key, $response['body']['key']); $this->assertEquals('-' . md5($etags), $response['body']['eTag']); $response = $this->client->getObjectMetadata($this->bucket, $this->key); $this->checkProperties($response); $this->assertEquals($file_size, $response['http_headers']['content-length']); $this->assertEquals(Coder::guessMimeType($this->key), $response['http_headers']['content-type']); $this->assertEquals('-' . md5($etags), $response['http_headers']['etag']); }
/** * @param mixed $config The http client config. * @param string $bucket_name The bucket name. * @param string $key The object path. * * @return string */ private function getPath($config, $bucket_name = null, $key = null) { return Coder::appendUri(Bce::URL_PREFIX, $bucket_name, $key); }