Skip to content

Commit

Permalink
add forcePathStyle option.
Browse files Browse the repository at this point in the history
  • Loading branch information
huiguangjun committed Oct 28, 2024
1 parent 5b44cef commit a31aebd
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/OSS/OssClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ private function __initNewClient($config = array())
throw new OssException("endpoint is empty");
}
$this->hostname = $this->checkEndpoint($endpoint, $isCName);
if (isset($config['forcePathStyle'])) {
if ($config['forcePathStyle'] === true) {
$this->hostType = self::OSS_HOST_TYPE_PATH_STYLE;
}
}
$this->requestProxy = $requestProxy;
if (!$provider instanceof CredentialsProvider) {
throw new OssException("provider must be an instance of CredentialsProvider");
Expand Down Expand Up @@ -3333,6 +3338,9 @@ private function generatePath($bucket, $object)
if ('' !== $bucket) {
if ($this->hostType === self::OSS_HOST_TYPE_IP || $this->hostType === self::OSS_HOST_TYPE_PATH_STYLE) {
$paths[] = $bucket;
if ('' === $object) {
$paths[] = '';
}
}
}
// + object
Expand Down
5 changes: 5 additions & 0 deletions tests/OSS/Tests/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ public static function getSignVersion()
return OssClient::OSS_SIGNATURE_VERSION_V1;
}

public static function getPathStyleBucket()
{
return getenv('OSS_TEST_PATHSTYLE_BUCKET');
}

/**
* Tool method, create a bucket
*/
Expand Down
94 changes: 84 additions & 10 deletions tests/OSS/Tests/OssClientForcePathStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
namespace OSS\Tests;

use OSS\Core\OssException;
use OSS\Credentials\StaticCredentialsProvider;
use OSS\Model\LifecycleConfig;
use OSS\Model\LifecycleRule;
use OSS\Model\LifecycleAction;
use OSS\Http\RequestCore;
use OSS\OssClient;

require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
Expand All @@ -18,20 +15,23 @@ public function testForcePathStyle()
{
$config = array(
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4,
'hostType' => OssClient::OSS_HOST_TYPE_PATH_STYLE,
'forcePathStyle' => true,
);
$this->ossClient = Common::getOssClient($config);

$pathStyleClient = Common::getOssClient($config);

try {
$this->ossClient->getBucketInfo($this->bucket);
$pathStyleClient->getBucketInfo($this->bucket);
$this->assertTrue(false, "should not here");
} catch (OssException $e) {
$this->assertEquals($e->getErrorCode(), "SecondLevelDomainForbidden");
$this->assertTrue(true);
}

try {
$object = "oss-php-sdk-test/upload-test-object-name.txt";
$this->ossClient->putObject($this->bucket, $object, 'hi oss');
$pathStyleClient->putObject($this->bucket, $object, 'hi oss');
$this->assertTrue(false, "should not here");
} catch (OssException $e) {
$this->assertEquals($e->getErrorCode(), "SecondLevelDomainForbidden");
$this->assertTrue(true);
Expand All @@ -40,11 +40,85 @@ public function testForcePathStyle()
try {
$endpoint = Common::getEndpoint();
$endpoint = str_replace(array('http://', 'https://'), '', $endpoint);
$strUrl = $this->bucket . '.' . $endpoint . "/" . $object;
$signUrl = $this->ossClient->signUrl($this->bucket, $object, 3600);
$strUrl = $endpoint . "/" . $this->bucket . '/' . $object;
$signUrl = $pathStyleClient->signUrl($this->bucket, $object, 3600);
$this->assertTrue(strpos($signUrl, $strUrl) !== false);
} catch (OssException $e) {
$this->assertFalse(true);
}
}

public function testForcePathStyleOKV1()
{
$bucket = Common::getPathStyleBucket();

$this->assertFalse(empty($bucket), "path style bucket is not set.");

$config = array(
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V1,
'forcePathStyle' => true,
);

$pathStyleClient = Common::getOssClient($config);

// bucket
$info = $pathStyleClient->getBucketInfo($bucket);
$this->assertEquals($bucket, $info->getName());

// object
$object = "upload-test-object-name.txt";
$pathStyleClient->putObject($bucket, $object, 'hi oss');
$res = $pathStyleClient->getObject($bucket, $object);
$this->assertEquals($res, 'hi oss');

//presign
$signUrl = $pathStyleClient->signUrl($bucket, $object, 3600);

$httpCore = new RequestCore($signUrl);
$httpCore->set_body("");
$httpCore->set_method("GET");
$httpCore->connect_timeout = 10;
$httpCore->timeout = 10;
$httpCore->add_header("Content-Type", "");
$httpCore->send_request();
$this->assertEquals(200, $httpCore->response_code);
}

public function testForcePathStyleOKV4()
{
$bucket = Common::getPathStyleBucket();

$this->assertFalse(empty($bucket), "path style bucket is not set.");

$config = array(
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4,
'forcePathStyle' => true,
);

$pathStyleClient = Common::getOssClient($config);

// bucket
$info = $pathStyleClient->getBucketInfo($bucket);
$this->assertEquals($bucket, $info->getName());

// object
$object = "upload-test-object-name.txt";
$pathStyleClient->putObject($bucket, $object, 'hi oss');
$res = $pathStyleClient->getObject($bucket, $object);
$this->assertEquals($res, 'hi oss');

//presign
$signUrl = $pathStyleClient->signUrl($bucket, $object, 3600);

#print("signUrl" . $signUrl . "\n");

$httpCore = new RequestCore($signUrl);
$httpCore->set_body("");
$httpCore->set_method("GET");
$httpCore->connect_timeout = 10;
$httpCore->timeout = 10;
$httpCore->add_header("Content-Type", "");
$httpCore->send_request();
$this->assertEquals(200, $httpCore->response_code);
}
}

0 comments on commit a31aebd

Please sign in to comment.