1313use GuzzleHttp \Exception \ConnectException ;
1414use GuzzleHttp \Middleware ;
1515use GuzzleHttp \Psr7 ;
16- use Qcloud \Cos \Exception \CosException ;
1716
1817/**
1918 * @method object AbortMultipartUpload (array $arg)
@@ -125,8 +124,8 @@ public function __construct($cosConfig) {
125124 $ this ->cosConfig ['schema ' ] = isset ($ cosConfig ['schema ' ]) ? $ cosConfig ['schema ' ] : 'http ' ;
126125 $ this ->cosConfig ['region ' ] = isset ($ cosConfig ['region ' ]) ? region_map ($ cosConfig ['region ' ]) : null ;
127126 $ this ->cosConfig ['appId ' ] = isset ($ cosConfig ['credentials ' ]['appId ' ]) ? $ cosConfig ['credentials ' ]['appId ' ] : null ;
128- $ this ->cosConfig ['secretId ' ] = isset ($ cosConfig ['credentials ' ]['secretId ' ]) ? $ cosConfig ['credentials ' ]['secretId ' ] : "" ;
129- $ this ->cosConfig ['secretKey ' ] = isset ($ cosConfig ['credentials ' ]['secretKey ' ]) ? $ cosConfig ['credentials ' ]['secretKey ' ] : "" ;
127+ $ this ->cosConfig ['secretId ' ] = isset ($ cosConfig ['credentials ' ]['secretId ' ]) ? $ cosConfig ['credentials ' ]['secretId ' ] : '' ;
128+ $ this ->cosConfig ['secretKey ' ] = isset ($ cosConfig ['credentials ' ]['secretKey ' ]) ? $ cosConfig ['credentials ' ]['secretKey ' ] : '' ;
130129 $ this ->cosConfig ['anonymous ' ] = isset ($ cosConfig ['credentials ' ]['anonymous ' ]) ? $ cosConfig ['anonymous ' ]['anonymous ' ] : false ;
131130 $ this ->cosConfig ['token ' ] = isset ($ cosConfig ['credentials ' ]['token ' ]) ? $ cosConfig ['credentials ' ]['token ' ] : null ;
132131 $ this ->cosConfig ['timeout ' ] = isset ($ cosConfig ['timeout ' ]) ? $ cosConfig ['timeout ' ] : 3600 ;
@@ -141,11 +140,10 @@ public function __construct($cosConfig) {
141140 $ this ->cosConfig ['pathStyle ' ] = isset ($ cosConfig ['pathStyle ' ]) ? $ cosConfig ['pathStyle ' ] : false ;
142141 $ this ->cosConfig ['allow_redirects ' ] = isset ($ cosConfig ['allow_redirects ' ]) ? $ cosConfig ['allow_redirects ' ] : false ;
143142 $ this ->cosConfig ['allow_accelerate ' ] = isset ($ cosConfig ['allow_accelerate ' ]) ? $ cosConfig ['allow_accelerate ' ] : false ;
144- try {
145- $ this ->inputCheck ();
146- } catch (\Exception $ e ) {
147- throw $ e ;
148- }
143+
144+ // check config
145+ $ this ->inputCheck ();
146+
149147 $ service = Service::getService ();
150148 $ handler = HandlerStack::create ();
151149 $ handler ->push (Middleware::retry ($ this ->retryDecide (), $ this ->retryDelay ()));
@@ -182,7 +180,7 @@ public function inputCheck() {
182180 $ this ->cosConfig ['domain ' ] == null &&
183181 $ this ->cosConfig ['endpoint ' ] == null &&
184182 $ this ->cosConfig ['ip ' ] == null ) {
185- $ e = new CosException ('Region is empty ' );
183+ $ e = new Exception \ CosException ('Region is empty ' );
186184 $ e ->setExceptionCode ('Invalid Argument ' );
187185 throw $ e ;
188186 }
@@ -202,7 +200,7 @@ public function retryDecide() {
202200 if ($ response != null && $ response ->getStatusCode () >= 400 ) {
203201 return true ;
204202 }
205- if ($ exception instanceof \ Qcloud \ Cos \ Exception \ServiceResponseException) {
203+ if ($ exception instanceof Exception \ServiceResponseException) {
206204 if ($ exception ->getStatusCode () >= 400 ) {
207205 return true ;
208206 }
@@ -331,25 +329,23 @@ public function upload($bucket, $key, $body, $options = array()) {
331329 public function download ($ bucket , $ key , $ saveAs , $ options = array ()) {
332330 $ options ['PartSize ' ] = isset ($ options ['PartSize ' ]) ? $ options ['PartSize ' ] : RangeDownload::DEFAULT_PART_SIZE ;
333331 $ contentLength = 0 ;
334- $ versionId = isset ($ options ['VersionId ' ]) ? $ options ['VersionId ' ] : "" ;
335- try {
336- $ rt = $ this ->headObject (array (
337- 'Bucket ' =>$ bucket ,
338- 'Key ' =>$ key ,
339- 'VersionId ' =>$ versionId ,
340- )
341- );
342- $ contentLength = $ rt ['ContentLength ' ];
343- $ resumableJson = [
344- 'LastModified ' => $ rt ['LastModified ' ],
345- 'ContentLength ' => $ rt ['ContentLength ' ],
346- 'ETag ' => $ rt ['ETag ' ],
347- 'Crc64ecma ' => $ rt ['Crc64ecma ' ]
348- ];
349- $ options ['ResumableJson ' ] = $ resumableJson ;
350- } catch (\Exception $ e ) {
351- throw ($ e );
352- }
332+ $ versionId = isset ($ options ['VersionId ' ]) ? $ options ['VersionId ' ] : '' ;
333+
334+ $ rt = $ this ->headObject (array (
335+ 'Bucket ' =>$ bucket ,
336+ 'Key ' =>$ key ,
337+ 'VersionId ' =>$ versionId ,
338+ )
339+ );
340+ $ contentLength = $ rt ['ContentLength ' ];
341+ $ resumableJson = [
342+ 'LastModified ' => $ rt ['LastModified ' ],
343+ 'ContentLength ' => $ rt ['ContentLength ' ],
344+ 'ETag ' => $ rt ['ETag ' ],
345+ 'Crc64ecma ' => $ rt ['Crc64ecma ' ]
346+ ];
347+ $ options ['ResumableJson ' ] = $ resumableJson ;
348+
353349 if ($ contentLength < $ options ['PartSize ' ]) {
354350 $ rt = $ this ->getObject (array (
355351 'Bucket ' => $ bucket ,
@@ -388,26 +384,22 @@ public function copy($bucket, $key, $copySource, $options = array()) {
388384 $ sourceConfig = $ this ->rawCosConfig ;
389385 $ sourceConfig ['region ' ] = $ copySource ['Region ' ];
390386 $ cosSourceClient = new Client ($ sourceConfig );
391- $ copySource ['VersionId ' ] = isset ($ copySource ['VersionId ' ]) ? $ copySource ['VersionId ' ] : "" ;
392- try {
393- $ rt = $ cosSourceClient ->headObject (
394- array ('Bucket ' =>$ copySource ['Bucket ' ],
395- 'Key ' =>$ copySource ['Key ' ],
396- 'VersionId ' =>$ copySource ['VersionId ' ],
397- )
398- );
399- } catch (\Exception $ e ) {
400- throw $ e ;
401- }
387+ $ copySource ['VersionId ' ] = isset ($ copySource ['VersionId ' ]) ? $ copySource ['VersionId ' ] : '' ;
388+
389+ $ rt = $ cosSourceClient ->headObject (
390+ array ('Bucket ' =>$ copySource ['Bucket ' ],
391+ 'Key ' =>$ copySource ['Key ' ],
392+ 'VersionId ' =>$ copySource ['VersionId ' ],
393+ )
394+ );
402395
403- $ contentLength =$ rt ['ContentLength ' ];
396+ $ contentLength = $ rt ['ContentLength ' ];
404397 // sample copy
405398 if ($ contentLength < $ options ['PartSize ' ]) {
406399 $ rt = $ this ->copyObject (array (
407400 'Bucket ' => $ bucket ,
408401 'Key ' => $ key ,
409- 'CopySource ' => $ copySource ['Bucket ' ]. '.cos. ' . $ copySource ['Region ' ].
410- ".myqcloud.com/ " . $ copySource ['Key ' ]. "?versionId= " . $ copySource ['VersionId ' ],
402+ 'CopySource ' => "{$ copySource ['Bucket ' ]}.cos. {$ copySource ['Region ' ]}.myqcloud.com/ {$ copySource ['Key ' ]}?versionId= {$ copySource ['VersionId ' ]}" ,
411403 ) + $ options
412404 );
413405 return $ rt ;
@@ -427,9 +419,9 @@ public function doesBucketExist($bucket, array $options = array())
427419 try {
428420 $ this ->HeadBucket (array (
429421 'Bucket ' => $ bucket ));
430- return True ;
422+ return true ;
431423 } catch (\Exception $ e ){
432- return False ;
424+ return false ;
433425 }
434426 }
435427
@@ -439,9 +431,9 @@ public function doesObjectExist($bucket, $key, array $options = array())
439431 $ this ->HeadObject (array (
440432 'Bucket ' => $ bucket ,
441433 'Key ' => $ key ));
442- return True ;
434+ return true ;
443435 } catch (\Exception $ e ){
444- return False ;
436+ return false ;
445437 }
446438 }
447439
0 commit comments