From 556872575937f6c7ffce9ea4cfc6df82ed0e4fb4 Mon Sep 17 00:00:00 2001 From: shenjiaqi Date: Thu, 9 Mar 2017 14:54:41 +0800 Subject: [PATCH 1/2] Fix metadata not set in completeMultipartUpload, Add setEndpoint in FDSClientConfiguration --- README.md | 4 ++ src/FDS/FDSClientConfiguration.php | 50 +++++++++++------------- src/FDS/GalaxyFDS.php | 4 +- src/FDS/GalaxyFDSClient.php | 2 +- tests/FDS/FDSClientConfigurationTest.php | 37 ++++++++++-------- tests/FDS/GalaxyFDSClientTest.php | 29 ++++++++++++++ tests/Httpful/HttpfulTest.php | 2 +- 7 files changed, 79 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index f15482a..dbc4562 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,10 @@ php ./examples/galaxy-fds.php (注意: 需要在样例中设置正确的App Key和Secret) +##### Release Notes: +* 20170309 + * Fix metadata not set in completeMultipartUpload + * Add setEndpoint in FDSClientConfiguration FDS PHP SDK User Guide ======================== diff --git a/src/FDS/FDSClientConfiguration.php b/src/FDS/FDSClientConfiguration.php index e4bcbbc..597e417 100644 --- a/src/FDS/FDSClientConfiguration.php +++ b/src/FDS/FDSClientConfiguration.php @@ -12,13 +12,12 @@ class FDSClientConfiguration { const URI_HTTP_PREFIX = "http://"; const URI_HTTPS_PREFIX = "https://"; - const URI_FILES = "files"; const URI_CDN = "cdn"; - const URI_FDS_SUFFIX = ".fds.api.xiaomi.com/"; - const URI_FDS_SSL_SUFFIX = ".fds-ssl.api.xiaomi.com/"; + const URI_SUFFIX = "fds.api.xiaomi.com"; + const URI_CDN_SUFFIX = "fds.api.mi-img.com"; const DEFAULT_RETRY_NUM = 3; const DEFAULT_CONNECTION_TIMEOUT_SECS = 30; - const DEFAULT_MAX_BATCH_DELETE_SIZE = 100; + const DEFAULT_MAX_BATCH_DELETE_SIZE = 1000; private $region_name; private $enable_https; @@ -30,18 +29,20 @@ class FDSClientConfiguration { private $retry; private $connection_timeout_secs; private $batch_delete_size; + private $endpoint; private $enable_unit_test_mode; private $base_uri_for_unit_test; public function __construct() { $this->enable_https = true; - $this->region_name = ""; + $this->region_name = "cnbj0"; $this->enable_cdn_for_upload = false; $this->enable_cdn_for_download = true; $this->enable_md5_calculate = false; $this->enable_debug = false; $this->enable_metrics = false; + $this->endpoint = ""; $this->enable_unit_test_mode = false; $this->base_uri_for_unit_test = ""; @@ -101,6 +102,14 @@ public function setBaseUriforunittest($base_uri_for_unit_test) { $this->base_uri_for_unit_test = $base_uri_for_unit_test; } + public function setEndpoint($endpoint) { + $this->endpoint = $endpoint; + } + + public function getEndpoint($endpoint) { + return $this->endpoint; + } + public function getBaseUri() { return $this->buildBaseUri(false); } @@ -123,30 +132,15 @@ public function buildBaseUri($enableCdn) { } $uri = $this->enable_https ? self::URI_HTTPS_PREFIX : self::URI_HTTP_PREFIX; - $uri .= $this->getBaseUriPrefix($enableCdn, $this->region_name); - $uri .= $this->getBaseUriSuffix($enableCdn, $this->enable_https); - return $uri; - } - - private function getBaseUriPrefix($enableCdn, $regionName) { - if (empty($regionName)) { - if ($enableCdn) { - return self::URI_CDN; - } - return self::URI_FILES; + if (!empty($this->endpoint)) { + $uri .= $this->endpoint; + } else if ($enableCdn) { + $uri .= self::URI_CDN . '.' . $this->region_name . '.' . self::URI_CDN_SUFFIX; } else { - if ($enableCdn) { - return $regionName . '-' . self::URI_CDN; - } - return $regionName . '-' . self::URI_FILES; + $uri .= $this->region_name . '.' . self::URI_SUFFIX; } - } - - private function getBaseUriSuffix($enableCdn, $enableHttps) { - if ($enableCdn && $enableHttps) { - return self::URI_FDS_SSL_SUFFIX; - } - return self::URI_FDS_SUFFIX; + $uri .= '/'; + return $uri; } public function isDebugEnabled() { @@ -193,4 +187,4 @@ public function setBatchDeleteSize($size) { $this->batch_delete_size = min($size, SELF::DEFAULT_MAX_BATCH_DELETE_SIZE); $this->batch_delete_size = max(1, $this->batch_delete_size); } -} +} \ No newline at end of file diff --git a/src/FDS/GalaxyFDS.php b/src/FDS/GalaxyFDS.php index c5a7ac4..9548d3b 100644 --- a/src/FDS/GalaxyFDS.php +++ b/src/FDS/GalaxyFDS.php @@ -225,7 +225,7 @@ public function deleteObject($bucket_name, $object_name); * * @param string $bucket_name The name of the bucket * @param $object_name_list array of names to delete, count($object_name_list) - * should less than 100 + * should less than 1k * @return array of failure reason: * (..., * ( @@ -362,4 +362,4 @@ public function generatePresignedUri($bucket_name, $object_name, $expiration, * @throws GalaxyFDSClientException */ public function generateDownloadObjectUri($bucket_name, $object_name); -} +} \ No newline at end of file diff --git a/src/FDS/GalaxyFDSClient.php b/src/FDS/GalaxyFDSClient.php index 80de7c8..932cef5 100644 --- a/src/FDS/GalaxyFDSClient.php +++ b/src/FDS/GalaxyFDSClient.php @@ -831,7 +831,7 @@ public function completeMultipartUpload($bucket_name, $object_name, $upload_id, $metadata, $upload_part_result_list) { $uri = $this->fds_config->getBaseUri() . $bucket_name . "/" . $object_name . "?uploadId=" . $upload_id; - $headers = $this->prepareRequestHeader($uri, Http::PUT, Mime::JSON); + $headers = $this->prepareRequestHeader($uri, Http::PUT, Mime::JSON, $metadata); $response = $this->invoke(Action::CompleteMultipartUpload, $uri, $headers, Http::PUT, null, json_encode($upload_part_result_list)); diff --git a/tests/FDS/FDSClientConfigurationTest.php b/tests/FDS/FDSClientConfigurationTest.php index 153f702..62592ee 100644 --- a/tests/FDS/FDSClientConfigurationTest.php +++ b/tests/FDS/FDSClientConfigurationTest.php @@ -14,12 +14,12 @@ class FDSClientConfigurationTest extends \PHPUnit_Framework_TestCase { - const URI_FDS_SUFFIX = ".fds.api.xiaomi.com/"; - const URI_FDS_SSL_SUFFIX = ".fds-ssl.api.xiaomi.com/"; + const URI_SUFFIX = "fds.api.xiaomi.com"; + const URI_CDN_SUFFIX = "fds.api.mi-img.com"; public function testDefaultConfigurationValue() { $fds_config = new FDSClientConfiguration(); - $this->assertEquals("", $fds_config->getRegionName()); + $this->assertEquals("cnbj0", $fds_config->getRegionName()); $this->assertEquals(true, $fds_config->isHttpsEnabled()); $this->assertEquals(false, $fds_config->isCdnEnabledForUpload()); $this->assertEquals(true, $fds_config->isCdnEnabledForDownload()); @@ -30,29 +30,29 @@ public function testDefaultConfigurationValue() { public function testCdnChosen() { $fdsConfig = new FDSClientConfiguration(); - $fdsConfig->setRegionName(""); + $fdsConfig->setRegionName("regionName"); $fdsConfig->enableHttps(true); // Test flag enableCdnForUpload $fdsConfig->enableCdnForUpload(false); - $this->assertEquals("https://files" . self::URI_FDS_SUFFIX, + $this->assertEquals("https://regionName." . self::URI_SUFFIX . '/', $fdsConfig->getUploadBaseUri()); $fdsConfig->enableCdnForUpload(true); - $this->assertEquals("https://cdn" . self::URI_FDS_SSL_SUFFIX, + $this->assertEquals("https://cdn.regionName." . self::URI_CDN_SUFFIX . '/', $fdsConfig->getUploadBaseUri()); $fdsConfig->enableHttps(false); - $this->assertEquals("http://cdn" . self::URI_FDS_SUFFIX, + $this->assertEquals("http://cdn.regionName." . self::URI_CDN_SUFFIX . '/', $fdsConfig->getUploadBaseUri()); // Test flag enableCdnForDownload $fdsConfig->enableCdnForDownload(false); - $this->assertEquals("http://files" . self::URI_FDS_SUFFIX, + $this->assertEquals("http://regionName." . self::URI_SUFFIX . '/', $fdsConfig->getDownloadBaseUri()); $fdsConfig->enableCdnForDownload(true); - $this->assertEquals("http://cdn" . self::URI_FDS_SUFFIX, + $this->assertEquals("http://cdn.regionName." . self::URI_CDN_SUFFIX . '/', $fdsConfig->getDownloadBaseUri()); $fdsConfig->enableHttps(true); - $this->assertEquals("https://cdn" . self::URI_FDS_SSL_SUFFIX, + $this->assertEquals("https://cdn.regionName." . self::URI_CDN_SUFFIX . '/', $fdsConfig->getDownloadBaseUri()); } @@ -62,18 +62,21 @@ public function testBuildBaseUri() { $fds_config = new FDSClientConfiguration(); // Test against flag enable https. - $fds_config->setRegionName(""); + $fds_config->setRegionName($region_name); $fds_config->enableHttps(true); - $this->assertEquals("https://files" . self::URI_FDS_SUFFIX, + $this->assertEquals("https://" . $region_name . '.' . self::URI_SUFFIX . '/', $fds_config->buildBaseUri(false)); $fds_config->enableHttps(false); - $this->assertEquals("http://files" . self::URI_FDS_SUFFIX, + $this->assertEquals("http://" . $region_name . '.' . self::URI_SUFFIX . '/', $fds_config->buildBaseUri(false)); - // Test against region name. - $fds_config->setRegionName($region_name); + $endpoint = "cnbj1.api.xiaomi.net"; + $fds_config->enableHttps(false); + $fds_config->setEndpoint($endpoint); + $this->assertEquals("http://" . $endpoint . "/", $fds_config->buildBaseUri(false)); + $this->assertEquals("http://" . $endpoint . "/", $fds_config->buildBaseUri(true)); $fds_config->enableHttps(true); - $this->assertEquals("https://" . $region_name . "-cdn" . - self::URI_FDS_SSL_SUFFIX , $fds_config->buildBaseUri(true)); + $this->assertEquals("https://" . $endpoint . "/", $fds_config->buildBaseUri(false)); + $this->assertEquals("https://" . $endpoint . "/", $fds_config->buildBaseUri(true)); } } \ No newline at end of file diff --git a/tests/FDS/GalaxyFDSClientTest.php b/tests/FDS/GalaxyFDSClientTest.php index 050c017..ee9f51e 100644 --- a/tests/FDS/GalaxyFDSClientTest.php +++ b/tests/FDS/GalaxyFDSClientTest.php @@ -19,6 +19,7 @@ use FDS\model\Grant; use FDS\model\Grantee; use FDS\model\Permission; +use FDS\model\UploadPartResultList; use Httpful\Request; class GalaxyFDSClientTest extends \PHPUnit_Framework_TestCase { @@ -422,6 +423,34 @@ public function testPresigedUri() { $this->assertEquals($content_type, $object->getObjectMetadata()->getContentType()); } + /** + * @depends testCreateBucket + */ + public function testMultipartUpload() { + $object_name = "multipart-upload"; + $content = "multipart-upload"; + $initMultipartUploadResult = self::$fds_client->initMultipartUpload(self::$bucket_name, $object_name); + + $uploadPartResult = self::$fds_client->uploadPart(self::$bucket_name, + $object_name, $initMultipartUploadResult->getUploadId(), 1, $content); + + + $uploadPartResultArray = array(); + array_push($uploadPartResultArray, $uploadPartResult); + $uploadPartResultList = new UploadPartResultList(); + $uploadPartResultList->setUploadPartResultList($uploadPartResultArray); + $metadata = new FDSObjectMetadata(); + $metadata->setContentType("text/html"); + + self::$fds_client->completeMultipartUpload(self::$bucket_name, $object_name, + $initMultipartUploadResult->getUploadId(), $metadata, $uploadPartResultList); + + $object = self::$fds_client->getObject(self::$bucket_name, $object_name); + $actualObjectContent = $object->getObjectContent(); + $this->assertEquals($content, $actualObjectContent); + $this->assertEquals("text/html", $object->getObjectMetadata()->getContentType()); + } + private function emptyBucket() { self::$fds_client->deleteObjectsByPrefix(self::$bucket_name, ""); } diff --git a/tests/Httpful/HttpfulTest.php b/tests/Httpful/HttpfulTest.php index 2e2708c..eb47313 100644 --- a/tests/Httpful/HttpfulTest.php +++ b/tests/Httpful/HttpfulTest.php @@ -24,7 +24,7 @@ class HttpfulTest extends \PHPUnit_Framework_TestCase { const TEST_SERVER = TEST_SERVER; - const TEST_URL = '127.0.0.1:8008'; + const TEST_URL = 'http://127.0.0.1:8008'; const TEST_URL_400 = 'http://127.0.0.1:8008/400'; const SAMPLE_JSON_HEADER = From 28e8b435e6c63b8a8a4ac4afc50dec5e667e34a8 Mon Sep 17 00:00:00 2001 From: shenjiaqi Date: Thu, 9 Mar 2017 14:57:08 +0800 Subject: [PATCH 2/2] Add version in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dbc4562..eac7252 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ php ./examples/galaxy-fds.php (注意: 需要在样例中设置正确的App Key和Secret) ##### Release Notes: -* 20170309 +* 20170309 - v1.0.0 * Fix metadata not set in completeMultipartUpload * Add setEndpoint in FDSClientConfiguration