代码同步

This commit is contained in:
Ghost
2025-03-24 14:59:19 +08:00
parent f0fa19f89f
commit bd2e1e5386
716 changed files with 90318 additions and 26155 deletions

View File

@@ -1,5 +1,9 @@
# ChangeLog - Aliyun OSS SDK for PHP
## v2.7.2 / 2024-10-28
* Added: presign supports response-* parameters
* Added: forcePathStyle option.
## v2.7.1 / 2024-02-28
* Fixed: fix deprecated

View File

@@ -169,6 +169,11 @@ class OssClient
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");
@@ -2372,7 +2377,7 @@ class OssClient
$options[self::OSS_OBJECT] = $object;
$options[self::OSS_SUB_RESOURCE] = 'x-oss-async-process';
$options[self::OSS_CONTENT_TYPE] = 'application/octet-stream';
$options[self::OSS_CONTENT] = 'x-oss-async-process='.$asyncProcess;
$options[self::OSS_CONTENT] = 'x-oss-async-process=' . $asyncProcess;
$response = $this->auth($options);
$result = new BodyResult($response);
return $result->getData();
@@ -2999,7 +3004,7 @@ class OssClient
return $this->getValue($options, self::OSS_CHECK_MD5, false, true, true);
}
/**
/**
* Gets value of the specified key from the options
*
* @param array $options
@@ -3269,7 +3274,7 @@ class OssClient
try {
$tmp_object = $options[self::OSS_OBJECT];
$encoding = array('UTF-8','GB2312', 'GBK');
$encoding = array('UTF-8', 'GB2312', 'GBK');
$encode = mb_detect_encoding($tmp_object, $encoding);
if ($encode === 'UTF-8' || $encode === false) {
return;
@@ -3333,6 +3338,9 @@ class OssClient
if ('' !== $bucket) {
if ($this->hostType === self::OSS_HOST_TYPE_IP || $this->hostType === self::OSS_HOST_TYPE_PATH_STYLE) {
$paths[] = $bucket;
if ('' === $object) {
$paths[] = '';
}
}
}
// + object
@@ -3354,6 +3362,12 @@ class OssClient
$query = array();
$queryList = array(
self::OSS_PART_NUM,
'response-content-type',
'response-content-language',
'response-cache-control',
'response-content-encoding',
'response-expires',
'response-content-disposition',
self::OSS_UPLOAD_ID,
self::OSS_COMP,
self::OSS_LIVE_CHANNEL_STATUS,
@@ -3381,6 +3395,7 @@ class OssClient
if (isset($options[self::OSS_SUB_RESOURCE])) {
$query[$options[self::OSS_SUB_RESOURCE]] = '';
}
return OssUtil::toQueryString($query);
}
@@ -3519,7 +3534,7 @@ class OssClient
}
try {
$encoding = array('UTF-8','GB2312', 'GBK');
$encoding = array('UTF-8', 'GB2312', 'GBK');
$encode = mb_detect_encoding($filepath, $encoding);
if ($encode !== 'UTF-8') {
return $filepath;
@@ -3534,7 +3549,7 @@ class OssClient
return $filepath;
}
/**
/**
* Decodes the file path from GBK to UTF-8.
*
* @param $filepath
@@ -3550,7 +3565,7 @@ class OssClient
}
try {
$encoding = array('UTF-8','GB2312', 'GBK');
$encoding = array('UTF-8', 'GB2312', 'GBK');
$encode = mb_detect_encoding($filepath, $encoding);
if ($encode === 'UTF-8' || $encode === false) {
return $filepath;
@@ -3745,8 +3760,8 @@ class OssClient
);
// OssClient version information
const OSS_NAME = "aliyun-sdk-php";
const OSS_VERSION = "2.7.1";
const OSS_BUILD = "20240228";
const OSS_VERSION = "2.7.2";
const OSS_BUILD = "20241028";
const OSS_AUTHOR = "";
const OSS_OPTIONS_ORIGIN = 'Origin';
const OSS_OPTIONS_REQUEST_METHOD = 'Access-Control-Request-Method';

View File

@@ -133,6 +133,11 @@ class Common
return OssClient::OSS_SIGNATURE_VERSION_V1;
}
public static function getPathStyleBucket()
{
return getenv('OSS_TEST_PATHSTYLE_BUCKET');
}
/**
* Tool method, create a bucket
*/

View File

@@ -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';
@@ -18,12 +15,14 @@ class OssClientForcePathStyleTest extends TestOssClientBase
{
$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);
@@ -31,7 +30,8 @@ class OssClientForcePathStyleTest extends TestOssClientBase
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);
@@ -40,11 +40,85 @@ class OssClientForcePathStyleTest extends TestOssClientBase
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);
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace OSS\Tests;
use OSS\Core\OssException;
use OSS\Credentials\StaticCredentialsProvider;
use OSS\Http\RequestCore;
use OSS\Http\ResponseCore;
use OSS\OssClient;
require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
class OssClientPresignTest extends TestOssClientBase
{
protected $stsOssClient;
public function testObjectWithSignV1()
{
$config = array(
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V1
);
$this->bucket = Common::getBucketName() . '-' . time();
$this->ossClient = Common::getOssClient($config);
$this->ossClient->createBucket($this->bucket);
Common::waitMetaSync();
$object = "a.file";
$this->ossClient->putObject($this->bucket, $object, "hi oss");
$timeout = 3600;
$options = array(
"response-content-disposition" => "inline"
);
try {
$signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $options);
} catch (OssException $e) {
$this->assertFalse(true);
}
$this->assertStringContainsString("response-content-disposition=inline", $signedUrl);
$options = array(
"response-content-disposition" => "attachment",
);
try {
$signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $options);
} catch (OssException $e) {
$this->assertFalse(true);
}
$this->assertStringContainsString("response-content-disposition=attachment", $signedUrl);
$httpCore = new RequestCore($signedUrl);
$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);
}
protected function tearDown(): void
{
$this->ossClient->deleteObject($this->bucket, "a.file");
parent::tearDown();
}
protected function setUp(): void
{
}
}

View File

@@ -305,6 +305,49 @@ class OssClientPresignV4Test extends TestOssClientBase
}
}
public function testObjectWithSignV4AndResponseQuery()
{
$config = array(
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4
);
$this->bucket = Common::getBucketName() . '-' . time();
$this->ossClient = Common::getOssClient($config);
$this->ossClient->createBucket($this->bucket);
Common::waitMetaSync();
$object = "a.file";
$this->ossClient->putObject($this->bucket, $object, "hi oss");
$timeout = 3600;
$options = array(
"response-content-disposition" => "inline"
);
try {
$signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $options);
} catch (OssException $e) {
$this->assertFalse(true);
}
$this->assertStringContainsString("response-content-disposition=inline", $signedUrl);
$options = array(
"response-content-disposition" => "attachment"
);
try {
$signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $options);
} catch (OssException $e) {
$this->assertFalse(true);
}
$this->assertStringContainsString("response-content-disposition=attachment", $signedUrl);
$httpCore = new RequestCore($signedUrl);
$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);
}
protected function tearDown(): void
{
$this->ossClient->deleteObject($this->bucket, "a.file");