Skip to content

Commit a31aebd

Browse files
committedOct 28, 2024
add forcePathStyle option.
1 parent 5b44cef commit a31aebd

File tree

3 files changed

+97
-10
lines changed

3 files changed

+97
-10
lines changed
 

‎src/OSS/OssClient.php

+8
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ private function __initNewClient($config = array())
169169
throw new OssException("endpoint is empty");
170170
}
171171
$this->hostname = $this->checkEndpoint($endpoint, $isCName);
172+
if (isset($config['forcePathStyle'])) {
173+
if ($config['forcePathStyle'] === true) {
174+
$this->hostType = self::OSS_HOST_TYPE_PATH_STYLE;
175+
}
176+
}
172177
$this->requestProxy = $requestProxy;
173178
if (!$provider instanceof CredentialsProvider) {
174179
throw new OssException("provider must be an instance of CredentialsProvider");
@@ -3333,6 +3338,9 @@ private function generatePath($bucket, $object)
33333338
if ('' !== $bucket) {
33343339
if ($this->hostType === self::OSS_HOST_TYPE_IP || $this->hostType === self::OSS_HOST_TYPE_PATH_STYLE) {
33353340
$paths[] = $bucket;
3341+
if ('' === $object) {
3342+
$paths[] = '';
3343+
}
33363344
}
33373345
}
33383346
// + object

‎tests/OSS/Tests/Common.php

+5
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ public static function getSignVersion()
133133
return OssClient::OSS_SIGNATURE_VERSION_V1;
134134
}
135135

136+
public static function getPathStyleBucket()
137+
{
138+
return getenv('OSS_TEST_PATHSTYLE_BUCKET');
139+
}
140+
136141
/**
137142
* Tool method, create a bucket
138143
*/

‎tests/OSS/Tests/OssClientForcePathStyleTest.php

+84-10
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
namespace OSS\Tests;
44

55
use OSS\Core\OssException;
6-
use OSS\Credentials\StaticCredentialsProvider;
7-
use OSS\Model\LifecycleConfig;
8-
use OSS\Model\LifecycleRule;
9-
use OSS\Model\LifecycleAction;
6+
use OSS\Http\RequestCore;
107
use OSS\OssClient;
118

129
require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
@@ -18,20 +15,23 @@ public function testForcePathStyle()
1815
{
1916
$config = array(
2017
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4,
21-
'hostType' => OssClient::OSS_HOST_TYPE_PATH_STYLE,
18+
'forcePathStyle' => true,
2219
);
23-
$this->ossClient = Common::getOssClient($config);
20+
21+
$pathStyleClient = Common::getOssClient($config);
2422

2523
try {
26-
$this->ossClient->getBucketInfo($this->bucket);
24+
$pathStyleClient->getBucketInfo($this->bucket);
25+
$this->assertTrue(false, "should not here");
2726
} catch (OssException $e) {
2827
$this->assertEquals($e->getErrorCode(), "SecondLevelDomainForbidden");
2928
$this->assertTrue(true);
3029
}
3130

3231
try {
3332
$object = "oss-php-sdk-test/upload-test-object-name.txt";
34-
$this->ossClient->putObject($this->bucket, $object, 'hi oss');
33+
$pathStyleClient->putObject($this->bucket, $object, 'hi oss');
34+
$this->assertTrue(false, "should not here");
3535
} catch (OssException $e) {
3636
$this->assertEquals($e->getErrorCode(), "SecondLevelDomainForbidden");
3737
$this->assertTrue(true);
@@ -40,11 +40,85 @@ public function testForcePathStyle()
4040
try {
4141
$endpoint = Common::getEndpoint();
4242
$endpoint = str_replace(array('http://', 'https://'), '', $endpoint);
43-
$strUrl = $this->bucket . '.' . $endpoint . "/" . $object;
44-
$signUrl = $this->ossClient->signUrl($this->bucket, $object, 3600);
43+
$strUrl = $endpoint . "/" . $this->bucket . '/' . $object;
44+
$signUrl = $pathStyleClient->signUrl($this->bucket, $object, 3600);
4545
$this->assertTrue(strpos($signUrl, $strUrl) !== false);
4646
} catch (OssException $e) {
4747
$this->assertFalse(true);
4848
}
4949
}
50+
51+
public function testForcePathStyleOKV1()
52+
{
53+
$bucket = Common::getPathStyleBucket();
54+
55+
$this->assertFalse(empty($bucket), "path style bucket is not set.");
56+
57+
$config = array(
58+
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V1,
59+
'forcePathStyle' => true,
60+
);
61+
62+
$pathStyleClient = Common::getOssClient($config);
63+
64+
// bucket
65+
$info = $pathStyleClient->getBucketInfo($bucket);
66+
$this->assertEquals($bucket, $info->getName());
67+
68+
// object
69+
$object = "upload-test-object-name.txt";
70+
$pathStyleClient->putObject($bucket, $object, 'hi oss');
71+
$res = $pathStyleClient->getObject($bucket, $object);
72+
$this->assertEquals($res, 'hi oss');
73+
74+
//presign
75+
$signUrl = $pathStyleClient->signUrl($bucket, $object, 3600);
76+
77+
$httpCore = new RequestCore($signUrl);
78+
$httpCore->set_body("");
79+
$httpCore->set_method("GET");
80+
$httpCore->connect_timeout = 10;
81+
$httpCore->timeout = 10;
82+
$httpCore->add_header("Content-Type", "");
83+
$httpCore->send_request();
84+
$this->assertEquals(200, $httpCore->response_code);
85+
}
86+
87+
public function testForcePathStyleOKV4()
88+
{
89+
$bucket = Common::getPathStyleBucket();
90+
91+
$this->assertFalse(empty($bucket), "path style bucket is not set.");
92+
93+
$config = array(
94+
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4,
95+
'forcePathStyle' => true,
96+
);
97+
98+
$pathStyleClient = Common::getOssClient($config);
99+
100+
// bucket
101+
$info = $pathStyleClient->getBucketInfo($bucket);
102+
$this->assertEquals($bucket, $info->getName());
103+
104+
// object
105+
$object = "upload-test-object-name.txt";
106+
$pathStyleClient->putObject($bucket, $object, 'hi oss');
107+
$res = $pathStyleClient->getObject($bucket, $object);
108+
$this->assertEquals($res, 'hi oss');
109+
110+
//presign
111+
$signUrl = $pathStyleClient->signUrl($bucket, $object, 3600);
112+
113+
#print("signUrl" . $signUrl . "\n");
114+
115+
$httpCore = new RequestCore($signUrl);
116+
$httpCore->set_body("");
117+
$httpCore->set_method("GET");
118+
$httpCore->connect_timeout = 10;
119+
$httpCore->timeout = 10;
120+
$httpCore->add_header("Content-Type", "");
121+
$httpCore->send_request();
122+
$this->assertEquals(200, $httpCore->response_code);
123+
}
50124
}

0 commit comments

Comments
 (0)
Failed to load comments.