Skip to content

Commit 4a3d136

Browse files
authored
feat(*):增加新特性并修复bug (#218)
- 新增单链接限速demo - 暴露getPresigned接口Headers和Params参数接口 - 补充textDetect UT - 修复stream_for废弃问题 - 修复x-cos头检测逻辑问题 - 修复UT部分bug
1 parent 193a4a4 commit 4a3d136

File tree

10 files changed

+109
-17
lines changed

10 files changed

+109
-17
lines changed

.gitattributes

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
/.gitattributes export-ignore
22
/.gitignore export-ignore
33
/.travis.yml export-ignore
4-
/phpunit.xml export-ignore
5-
/src/Qcloud/Cos/Tests export-ignore

sample/detectText.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
'Content' => base64_encode($content) // 文本需base64_encode
2222
),
2323
'Conf' => array(
24-
'DetectType' => 'Porn,Terrorism,Politics,Ads',
24+
'DetectType' => 'Porn,Terrorism,Politics,Ads', //Porn,Terrorism,Politics,Ads,Illegal,Abuse类型
2525
'BizType' => '',
2626
),
2727
));

sample/getObjectSensitiveContentRecognition.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,29 @@
1313
'secretId' => $secretId,
1414
'secretKey' => $secretKey)));
1515
try {
16+
//存储桶图片审核
1617
$result = $cosClient->getObjectSensitiveContentRecognition(array(
1718
'Bucket' => 'examplebucket-125000000', //格式:BucketName-APPID
1819
'Key' => 'exampleobject',
1920
'DetectType' => 'porn,politics' //可选四种参数:porn,politics,terrorist,ads,可使用多种规则,注意规则间不要加空格
21+
// 'Interval' => 5, // 审核gif时使用 截帧的间隔
22+
// 'MaxFrames' => 5, // 针对 GIF 动图审核的最大截帧数量,需大于0。
23+
// 'BizType' => '', // 审核策略
24+
));
25+
// 请求成功
26+
print_r($result);
27+
28+
29+
//图片链接审核
30+
$imgUrl = 'https://test.jpg';
31+
$result = $cosClient->getObjectSensitiveContentRecognition(array(
32+
'Bucket' => 'examplebucket-125000000', //格式:BucketName-APPID
33+
'Key' => '/', // 链接图片资源路径写 / 即可
34+
'DetectType' => 'porn,ads',//可选四种参数:porn,politics,terrorist,ads,可使用多种规则,注意规则间不要加空格
35+
'DetectUrl' => $imgUrl,
36+
// 'Interval' => 5, // 审核gif时使用 截帧的间隔
37+
// 'MaxFrames' => 5, // 针对 GIF 动图审核的最大截帧数量,需大于0。
38+
// 'BizType' => '', // 审核策略
2039
));
2140
// 请求成功
2241
print_r($result);

sample/getPresignedUrl.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
try {
1717
$signedUrl = $cosClient->getPresignedUrl(
1818
$method='putObject',
19-
$args=['Bucket'=>'examplebucket-1250000000', //格式:BucketName-APPID
20-
'Key'=>'exampleobject',
21-
'Body'=>''],
19+
$args=array(
20+
'Bucket'=>'examplebucket-1250000000', //格式:BucketName-APPID
21+
'Key'=>'exampleobject',
22+
'Body'=>'',
23+
'Params'=>array(),
24+
'Headers'=>array()),
2225
$expires='+30 minutes');
2326
// 请求成功
2427
echo($signedUrl);

sample/trafficLimit.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
require dirname(__FILE__) . '/../vendor/autoload.php';
4+
5+
$secretId = getenv('SECRET_ID'); //"云 API 密钥 SecretId";
6+
$secretKey = getenv('SECRET_KEY'); //"云 API 密钥 SecretKey";
7+
$region = getenv('COS_REGION'); //设置一个默认的存储桶地域
8+
$cosClient = new Qcloud\Cos\Client(
9+
array(
10+
'region' => $region,
11+
'schema' => 'https', //协议头部,默认为http
12+
'credentials'=> array(
13+
'secretId' => $secretId ,
14+
'secretKey' => $secretKey)));
15+
$local_path = "/Users/tuuna/Desktop/123hello.txt";
16+
17+
$tagSet = http_build_query( array(
18+
urlencode("key1") => urlencode("value1"),
19+
urlencode("key2") => urlencode("value2")),
20+
'',
21+
'&'
22+
);
23+
24+
try {
25+
//上传对象,单链接限速
26+
$result = $cosClient->putObject(array(
27+
'Bucket' => getenv('COS_BUCKET'), //格式:BucketName-APPID
28+
'Key' => 'test191.txt',
29+
'Body' => fopen($local_path, 'rb'),
30+
'TrafficLimit' => 8 * 1000 * 1000 // 限制为1MB/s
31+
));
32+
// 请求成功
33+
print_r($result);
34+
35+
//下载对象,单链接限速
36+
$result = $cosClient->getObject(array(
37+
'Bucket' => 'examplebucket-125000000', //格式:BucketName-APPID
38+
'Key' => 'exampleobject',
39+
'SaveAs' => '/data/exampleobject',
40+
'TrafficLimit' => 8 * 1000 * 1000 // 限制为1MB/s
41+
));
42+
// 请求成功
43+
print_r($result);
44+
} catch (\Exception $e) {
45+
// 请求失败
46+
echo($e);
47+
}

src/Qcloud/Cos/Client.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
* @method object GetBucketTagging (array $arg)
5151
* @method object UploadPart (array $arg)
5252
* @method object PutObject (array $arg)
53-
* @method object AppendObject (array $arg)
5453
* @method object PutObjectAcl (array $arg)
5554
* @method object PutBucketAcl (array $arg)
5655
* @method object PutBucketCors (array $arg)
@@ -88,14 +87,15 @@
8887
* @method object GetBucketImageStyle (array $arg)
8988
* @method object DeleteBucketImageStyle (array $arg)
9089
* @method object PutBucketGuetzli (array $arg)
90+
* @method object AppendObject (array $arg)
9191
* @method object GetBucketGuetzli (array $arg)
9292
* @method object DeleteBucketGuetzli (array $arg)
9393
* @method object GetObjectSensitiveContentRecognition (array $arg)
9494
* @method object DetectText (array $arg)
9595
* @method object CreateMediaTranscodeJobs (array $arg)
9696
*/
9797
class Client extends GuzzleClient {
98-
const VERSION = '2.2.3';
98+
const VERSION = '2.3.0';
9999

100100
public $httpClient;
101101

@@ -294,7 +294,7 @@ public function getObjectUrlWithoutSign($bucket, $key, array $args = array()) {
294294
}
295295

296296
public function upload($bucket, $key, $body, $options = array()) {
297-
$body = Psr7\stream_for($body);
297+
$body = Psr7\Utils::streamFor($body);
298298
$options['Retry'] = $this->cosConfig['retry'];
299299
$options['PartSize'] = isset($options['PartSize']) ? $options['PartSize'] : MultipartUpload::DEFAULT_PART_SIZE;
300300
if ($body->getSize() < $options['PartSize']) {
@@ -355,7 +355,7 @@ public function download($bucket, $key, $saveAs, $options = array()) {
355355
}
356356

357357
public function resumeUpload($bucket, $key, $body, $uploadId, $options = array()) {
358-
$body = Psr7\stream_for($body);
358+
$body = Psr7\Utils::streamFor($body);
359359
$options['PartSize'] = isset($options['PartSize']) ? $options['PartSize'] : MultipartUpload::DEFAULT_PART_SIZE;
360360
$multipartUpload = new MultipartUpload($this, $body, array(
361361
'Bucket' => $bucket,

src/Qcloud/Cos/Common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function startWith( $haystack, $needle ) {
3838
if ( $length == 0 ) {
3939
return true;
4040
}
41-
return ( substr( $haystack, $length ) === $needle );
41+
return ( substr( $haystack, 0, $length ) === $needle );
4242
}
4343

4444
function headersMap( $command, $request ) {

src/Qcloud/Cos/Request/BodyLocation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ public function visit(
4444
}
4545
// binary string data from bound parameter
4646
$value = $command[$param->getName()];
47-
return $request->withBody(Psr7\stream_for($value));
47+
return $request->withBody(Psr7\Utils::streamFor($value));
4848
}
4949
}

src/Qcloud/Cos/Service.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4195,9 +4195,9 @@ public static function getService() {
41954195
'data' => array(
41964196
'xmlFlattened' => true,
41974197
),
4198-
'items' => [
4198+
'items' => array(
41994199
'type' => 'string',
4200-
]
4200+
)
42014201
),
42024202
'AllowedMethods' => array(
42034203
'type' => 'array',

src/Qcloud/Cos/Tests/COSTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected function setUp(): void
2525
$this->testTaggingValues = array(
2626
'value1', 'value2'
2727
);
28-
$this->cosClient = new Client(array('region' => $this->region,
28+
$this->cosClient = new Client(array('region' => $this->region,'schema' => 'https',
2929
'credentials' => array(
3030
'secretId' => getenv('COS_KEY'),
3131
'secretKey' => getenv('COS_SECRET'))));
@@ -87,7 +87,7 @@ public function testValidRegionBucket()
8787
'gz','ap-guangzhou',
8888
'cd','ap-chengdu',
8989
'sgp','ap-singapore');
90-
foreach ($regionlist as$region) {
90+
foreach ($regionlist as $region) {
9191
try {
9292
$this->cosClient = new Client(array('region' => $region,
9393
'schema' =>'https',
@@ -1594,7 +1594,7 @@ public function testGetObjectUrlWithoutSign() {
15941594
$key = 'hello.txt';
15951595
try{
15961596
$result = $this->cosClient->getObjectUrlWithoutSign($this->bucket, $key);
1597-
$tmpUrl = 'http://' . $this->bucket . '.cos.' . $this->region . '.myqcloud.com/' . $key;
1597+
$tmpUrl = 'https://' . $this->bucket . '.cos.' . $this->region . '.myqcloud.com/' . $key;
15981598
$this->assertEquals($result, $tmpUrl);
15991599
} catch (ServiceResponseException $e) {
16001600
$this->assertFalse(TRUE);
@@ -2054,4 +2054,29 @@ public function testPutObjectTaggingObjectNonExisted()
20542054
}
20552055
}
20562056

2057+
2058+
/*
2059+
* 文本检测
2060+
*
2061+
* 200
2062+
*/
2063+
public function testDetectText() {
2064+
$content = '约炮';
2065+
try {
2066+
$result = $this->cosClient->detectText(array(
2067+
'Bucket' => $this->bucket, //格式:BucketName-APPID
2068+
'Input' => array(
2069+
'Content' => base64_encode($content) // 文本需base64_encode
2070+
),
2071+
'Conf' => array(
2072+
'DetectType' => 'Porn,Terrorism,Politics,Ads',
2073+
'BizType' => '',
2074+
),
2075+
));
2076+
$this->assertTrue(true);
2077+
} catch (ServiceResponseException $e) {
2078+
$this->assertFalse(true);
2079+
}
2080+
2081+
}
20572082
}

0 commit comments

Comments
 (0)