-
Notifications
You must be signed in to change notification settings - Fork 346
/
OssClient.php
3802 lines (3551 loc) · 139 KB
/
OssClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace OSS;
use OSS\Core\MimeTypes;
use OSS\Core\OssException;
use OSS\Credentials\Credentials;
use OSS\Credentials\CredentialsProvider;
use OSS\Credentials\StaticCredentialsProvider;
use OSS\Http\RequestCore;
use OSS\Http\RequestCore_Exception;
use OSS\Http\ResponseCore;
use OSS\Model\BucketInfo;
use OSS\Model\CorsConfig;
use OSS\Model\CnameConfig;
use OSS\Model\GetLiveChannelHistory;
use OSS\Model\GetLiveChannelInfo;
use OSS\Model\GetLiveChannelStatus;
use OSS\Model\LoggingConfig;
use OSS\Model\LiveChannelConfig;
use OSS\Model\LiveChannelInfo;
use OSS\Model\LiveChannelListInfo;
use OSS\Model\ObjectListInfoV2;
use OSS\Model\StorageCapacityConfig;
use OSS\Result\AclResult;
use OSS\Result\BodyResult;
use OSS\Result\GetCorsResult;
use OSS\Result\GetLifecycleResult;
use OSS\Result\GetLocationResult;
use OSS\Result\GetLoggingResult;
use OSS\Result\GetRefererResult;
use OSS\Result\GetStorageCapacityResult;
use OSS\Result\GetWebsiteResult;
use OSS\Result\GetCnameResult;
use OSS\Result\HeaderResult;
use OSS\Result\InitiateMultipartUploadResult;
use OSS\Result\ListBucketsResult;
use OSS\Result\ListMultipartUploadResult;
use OSS\Model\ListMultipartUploadInfo;
use OSS\Result\ListObjectsResult;
use OSS\Result\ListObjectsV2Result;
use OSS\Result\ListPartsResult;
use OSS\Result\PutSetDeleteResult;
use OSS\Result\DeleteObjectsResult;
use OSS\Result\CopyObjectResult;
use OSS\Result\CallbackResult;
use OSS\Result\ExistResult;
use OSS\Result\PutLiveChannelResult;
use OSS\Result\GetLiveChannelHistoryResult;
use OSS\Result\GetLiveChannelInfoResult;
use OSS\Result\GetLiveChannelStatusResult;
use OSS\Result\ListLiveChannelResult;
use OSS\Result\AppendResult;
use OSS\Model\ObjectListInfo;
use OSS\Result\SymlinkResult;
use OSS\Result\UploadPartResult;
use OSS\Model\BucketListInfo;
use OSS\Model\LifecycleConfig;
use OSS\Model\RefererConfig;
use OSS\Model\WebsiteConfig;
use OSS\Core\OssUtil;
use OSS\Model\ListPartsInfo;
use OSS\Result\GetBucketInfoResult;
use OSS\Model\BucketStat;
use OSS\Result\GetBucketStatResult;
use OSS\Model\ServerSideEncryptionConfig;
use OSS\Result\GetBucketEncryptionResult;
use OSS\Model\RequestPaymentConfig;
use OSS\Result\GetBucketRequestPaymentResult;
use OSS\Model\Tag;
use OSS\Model\TaggingConfig;
use OSS\Result\GetBucketTagsResult;
use OSS\Model\VersioningConfig;
use OSS\Result\GetBucketVersioningResult;
use OSS\Model\InitiateWormConfig;
use OSS\Result\InitiateBucketWormResult;
use OSS\Model\ExtendWormConfig;
use OSS\Result\GetBucketWormResult;
use OSS\Model\RestoreConfig;
use OSS\Model\ObjectVersionListInfo;
use OSS\Result\ListObjectVersionsResult;
use OSS\Model\DeleteObjectInfo;
use OSS\Model\DeletedObjectInfo;
use OSS\Result\DeleteObjectVersionsResult;
use OSS\Model\TransferAccelerationConfig;
use OSS\Result\GetBucketTransferAccelerationResult;
use OSS\Model\CnameTokenInfo;
use OSS\Result\CreateBucketCnameTokenResult;
use OSS\Result\GetBucketCnameTokenResult;
use OSS\Signer\SignerInterface;
use OSS\Signer\SignerV1;
use OSS\Signer\SignerV4;
/**
* Class OssClient
*
* Object Storage Service(OSS)'s client class, which wraps all OSS APIs user could call to talk to OSS.
* Users could do operations on bucket, object, including MultipartUpload or setting ACL via an OSSClient instance.
* For more details, please check out the OSS API document:https://www.alibabacloud.com/help/doc-detail/31947.htm
*/
class OssClient
{
/**
* OssClient constructor.
*/
public function __construct()
{
$argNum = func_num_args();
$args = func_get_args();
if ($argNum == 1 && is_array($args[0])) {
call_user_func_array(array($this, '__initNewClient'), $args);
} else {
call_user_func_array(array($this, '__initClient'), $args);
}
}
/**
* There're a few different ways to create an OssClient object:
* 1. Most common one from access Id, access Key and the endpoint: $ossClient = new OssClient($id, $key, $endpoint)
* 2. If the endpoint is the CName (such as www.testoss.com, make sure it's CName binded in the OSS console),
* uses $ossClient = new OssClient($id, $key, $endpoint, true)
* 3. If using Alicloud's security token service (STS), then the AccessKeyId, AccessKeySecret and STS token are all got from STS.
* Use this: $ossClient = new OssClient($id, $key, $endpoint, false, $token)
* 4. If the endpoint is in IP format, you could use this: $ossClient = new OssClient($id, $key, “1.2.3.4:8900”)
*
* @param string $accessKeyId The AccessKeyId from OSS or STS
* @param string $accessKeySecret The AccessKeySecret from OSS or STS
* @param string $endpoint The domain name of the datacenter,For example: oss-cn-hangzhou.aliyuncs.com
* @param boolean $isCName If this is the CName and binded in the bucket.
* @param string $securityToken from STS.
* @param string $requestProxy
* @throws OssException
*/
private function __initClient($accessKeyId, $accessKeySecret, $endpoint, $isCName = false, $securityToken = NULL, $requestProxy = NULL)
{
$accessKeyId = trim($accessKeyId);
$accessKeySecret = trim($accessKeySecret);
$endpoint = trim(trim($endpoint), "/");
if (empty($accessKeyId)) {
throw new OssException("access key id is empty");
}
if (empty($accessKeySecret)) {
throw new OssException("access key secret is empty");
}
$provider = new StaticCredentialsProvider($accessKeyId, $accessKeySecret, $securityToken);
$config = array(
'endpoint' => $endpoint,
'cname' => $isCName,
'request_proxy' => $requestProxy,
'provider' => $provider
);
$this->__initNewClient($config);
}
/**
* @param array $config
* @throws OssException
*/
private function __initNewClient($config = array())
{
$isCName = isset($config['cname']) ? $config['cname'] : false;
$endpoint = isset($config['endpoint']) ? $config['endpoint'] : '';
$requestProxy = isset($config['request_proxy']) ? $config['request_proxy'] : null;
$provider = isset($config['provider']) ? $config['provider'] : '';
if (empty($endpoint)) {
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");
}
$this->provider = $provider;
$this->region = isset($config['region']) ? $config['region'] : '';
$this->cloudBoxId = isset($config['cloudBoxId']) ? $config['cloudBoxId'] : '';
// $enableStrictObjName
$this->enableStrictObjName = true;
if (isset($config['strictObjectName'])) {
if ($config['strictObjectName'] === false) {
$this->enableStrictObjName = false;
}
}
// sign version
$signatureVersion = self::OSS_SIGNATURE_VERSION_V1;
if (isset($config['signatureVersion']) && $config['signatureVersion'] === self::OSS_SIGNATURE_VERSION_V4) {
$signatureVersion = self::OSS_SIGNATURE_VERSION_V4;
}
if ($signatureVersion === self::OSS_SIGNATURE_VERSION_V4) {
$this->enableStrictObjName = false;
$this->signer = new SignerV4();
} else {
$this->signer = new SignerV1();
}
//checkObjectEncoding
$this->checkObjectEncoding = false;
if (isset($config['checkObjectEncoding'])) {
if ($config['checkObjectEncoding'] === true) {
$this->checkObjectEncoding = true;
}
}
//filePathCompatible
$this->filePathCompatible = false;
if (version_compare(phpversion(), '7.0.0', '<')) {
if (OssUtil::isWin()) {
$this->filePathCompatible = true;
}
}
if (isset($config['filePathCompatible'])) {
if ($config['filePathCompatible'] === true) {
$this->filePathCompatible = true;
} else if ($config['filePathCompatible'] === false) {
$this->filePathCompatible = false;
}
}
self::checkEnv();
}
/**
* Lists the Bucket [GetService]. Not applicable if the endpoint is CName (because CName must be binded to a specific bucket).
*
* @param array $options
* @return BucketListInfo|null
* @throws OssException|RequestCore_Exception
*/
public function listBuckets($options = NULL)
{
if ($this->hostType === self::OSS_HOST_TYPE_CNAME) {
throw new OssException("operation is not permitted with CName host");
}
$this->precheckOptions($options);
$options[self::OSS_BUCKET] = '';
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$response = $this->auth($options);
$result = new ListBucketsResult($response);
return $result->getData();
}
/**
* Creates bucket,The ACL of the bucket created by default is OssClient::OSS_ACL_TYPE_PRIVATE
*
* @param string $bucket
* @param string $acl
* @param array $options
* @return null
* @throws OssException|RequestCore_Exception
*/
public function createBucket($bucket, $acl = self::OSS_ACL_TYPE_PRIVATE, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
$options[self::OSS_HEADERS][self::OSS_ACL] = $acl;
if (isset($options[self::OSS_STORAGE])) {
$this->precheckStorage($options[self::OSS_STORAGE]);
$options[self::OSS_CONTENT] = OssUtil::createBucketXmlBody($options[self::OSS_STORAGE]);
unset($options[self::OSS_STORAGE]);
}
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Deletes bucket
* The deletion will not succeed if the bucket is not empty (either has objects or parts)
* To delete a bucket, all its objects and parts must be deleted first.
*
* @param string $bucket
* @param array $options
* @return null
* @throws OssException|RequestCore_Exception
*/
public function deleteBucket($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Checks if a bucket exists
*
* @param string $bucket
* @return bool|null
* @throws OssException|RequestCore_Exception
*/
public function doesBucketExist($bucket)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_SUB_RESOURCE] = 'acl';
$response = $this->auth($options);
$result = new ExistResult($response);
return $result->getData();
}
/**
* Get the data center location information for the bucket
*
* @param string $bucket
* @param array $options
* @return string|null
* @throws OssException|RequestCore_Exception
*/
public function getBucketLocation($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_SUB_RESOURCE] = 'location';
$response = $this->auth($options);
$result = new GetLocationResult($response);
return $result->getData();
}
/**
* Get the Meta information for the Bucket
*
* @param string $bucket
* @param array $options Refer to the SDK documentation
* @return array|null
* @throws OssException|RequestCore_Exception
*/
public function getBucketMeta($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_HEAD;
$response = $this->auth($options);
$result = new HeaderResult($response);
return $result->getData();
}
/**
* Gets the bucket ACL
*
* @param string $bucket
* @param array $options
* @return string|null
* @throws OssException|RequestCore_Exception
*/
public function getBucketAcl($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_SUB_RESOURCE] = 'acl';
$response = $this->auth($options);
$result = new AclResult($response);
return $result->getData();
}
/**
* Sets the bucket ACL
*
* @param string $bucket bucket name
* @param string $acl access permissions, valid values are ['private', 'public-read', 'public-read-write']
* @param array $options by default is empty
* @return null
* @throws OssException|RequestCore_Exception
*/
public function putBucketAcl($bucket, $acl, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
$options[self::OSS_HEADERS][self::OSS_ACL] = $acl;
$options[self::OSS_SUB_RESOURCE] = 'acl';
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Gets object ACL
*
* @param string $bucket
* @param string $object
* @param array $options
* @return string|null
* @throws OssException|RequestCore_Exception
*/
public function getObjectAcl($bucket, $object, $options = NULL)
{
$this->precheckCommon($bucket, $object, $options, true);
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_OBJECT] = $object;
$options[self::OSS_SUB_RESOURCE] = 'acl';
$response = $this->auth($options);
$result = new AclResult($response);
return $result->getData();
}
/**
* Sets the object ACL
*
* @param string $bucket bucket name
* @param string $object object name
* @param string $acl access permissions, valid values are ['default', 'private', 'public-read', 'public-read-write']
* @param array $options
* @return null
* @throws OssException|RequestCore_Exception
*/
public function putObjectAcl($bucket, $object, $acl, $options = NULL)
{
$this->precheckCommon($bucket, $object, $options, true);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
$options[self::OSS_OBJECT] = $object;
$options[self::OSS_HEADERS][self::OSS_OBJECT_ACL] = $acl;
$options[self::OSS_SUB_RESOURCE] = 'acl';
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Gets the bucket logging config
*
* @param string $bucket bucket name
* @param array $options by default is empty
* @return LoggingConfig|null
* @throws OssException|RequestCore_Exception
*/
public function getBucketLogging($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_SUB_RESOURCE] = 'logging';
$response = $this->auth($options);
$result = new GetLoggingResult($response);
return $result->getData();
}
/**
* Sets the bycket logging config. Only owner can call this API.
*
* @param string $bucket bucket name
* @param string $targetBucket The logging file's bucket
* @param string $targetPrefix The logging file's prefix
* @param array $options By default is empty.
* @return null
* @throws OssException|RequestCore_Exception
*/
public function putBucketLogging($bucket, $targetBucket, $targetPrefix, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$this->precheckBucket($targetBucket, 'targetbucket is not allowed empty');
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
$options[self::OSS_SUB_RESOURCE] = 'logging';
$options[self::OSS_CONTENT_TYPE] = 'application/xml';
$loggingConfig = new LoggingConfig($targetBucket, $targetPrefix);
$options[self::OSS_CONTENT] = $loggingConfig->serializeToXml();
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Deletes the bucket logging config
*
* @param string $bucket bucket name
* @param array $options
* @return null
* @throws OssException|RequestCore_Exception
*/
public function deleteBucketLogging($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
$options[self::OSS_SUB_RESOURCE] = 'logging';
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Sets the website config in bucket---that is could make the bucket as a static website once the CName is binded.
*
* @param string $bucket bucket name
* @param WebsiteConfig $websiteConfig
* @param array $options
* @return null
* @throws OssException|RequestCore_Exception
*/
public function putBucketWebsite($bucket, $websiteConfig, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
$options[self::OSS_SUB_RESOURCE] = 'website';
$options[self::OSS_CONTENT_TYPE] = 'application/xml';
$options[self::OSS_CONTENT] = $websiteConfig->serializeToXml();
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Gets the website config in the bucket
*
* @param string $bucket bucket name
* @param array $options
* @return WebsiteConfig|null
* @throws OssException|RequestCore_Exception
*/
public function getBucketWebsite($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_SUB_RESOURCE] = 'website';
$response = $this->auth($options);
$result = new GetWebsiteResult($response);
return $result->getData();
}
/**
* Deletes the website config in the bucket
*
* @param string $bucket bucket name
* @param array $options
* @return null
* @throws OssException|RequestCore_Exception
*/
public function deleteBucketWebsite($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
$options[self::OSS_SUB_RESOURCE] = 'website';
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Sets the cross-origin-resource-sharing (CORS) rule. It would overwrite the originl one.
*
* @param string $bucket bucket name
* @param CorsConfig $corsConfig CORS config. Check out the details from OSS API document
* @param array $options array
* @return null
* @throws OssException|RequestCore_Exception
*/
public function putBucketCors($bucket, $corsConfig, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
$options[self::OSS_SUB_RESOURCE] = 'cors';
$options[self::OSS_CONTENT_TYPE] = 'application/xml';
$options[self::OSS_CONTENT] = $corsConfig->serializeToXml();
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Gets the bucket CORS config
*
* @param string $bucket bucket name
* @param array $options
* @return CorsConfig|null
* @throws OssException|RequestCore_Exception
*/
public function getBucketCors($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_SUB_RESOURCE] = 'cors';
$response = $this->auth($options);
$result = new GetCorsResult($response);
return $result->getData();
}
/**
* Deletes the bucket's CORS config and disable the CORS on the bucket.
*
* @param string $bucket bucket name
* @param array $options
* @return null
* @throws OssException|RequestCore_Exception
*/
public function deleteBucketCors($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
$options[self::OSS_SUB_RESOURCE] = 'cors';
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Bind a CName for the bucket
*
* @param string $bucket bucket name
* @param string $cname
* @param array $options
* @return null
* @throws OssException|RequestCore_Exception
*/
public function addBucketCname($bucket, $cname, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_POST;
$options[self::OSS_CONTENT_TYPE] = 'application/xml';
$cnameConfig = new CnameConfig();
$cnameConfig->addCname($cname);
$options[self::OSS_CONTENT] = $cnameConfig->serializeToXml();
$options[self::OSS_COMP] = 'add';
$options[self::OSS_CNAME] = '';
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Gets the binded CName list of the bucket
*
* @param string $bucket bucket name
* @param array $options
* @return CnameConfig|null
* @throws OssException|RequestCore_Exception
*/
public function getBucketCname($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_CNAME] = '';
$response = $this->auth($options);
$result = new GetCnameResult($response);
return $result->getData();
}
/**
* Remove a CName binding from the bucket
*
* @param string $bucket bucket name
* @param CnameConfig $cname
* @param array $options
* @return null
* @throws OssException|RequestCore_Exception
*/
public function deleteBucketCname($bucket, $cname, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_POST;
$options[self::OSS_CONTENT_TYPE] = 'application/xml';
$cnameConfig = new CnameConfig();
$cnameConfig->addCname($cname);
$options[self::OSS_CONTENT] = $cnameConfig->serializeToXml();
$options[self::OSS_COMP] = 'delete';
$options[self::OSS_CNAME] = '';
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* create a cname token for a bucket
*
* @param string $bucket bucket name
* @param array $options
* @return CnameTokenInfo|null
* @throws OssException|RequestCore_Exception
*/
public function createBucketCnameToken($bucket, $cname, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_POST;
$options[self::OSS_CONTENT_TYPE] = 'application/xml';
$cnameConfig = new CnameConfig();
$cnameConfig->addCname($cname);
$options[self::OSS_CONTENT] = $cnameConfig->serializeToXml();
$options[self::OSS_COMP] = 'token';
$options[self::OSS_CNAME] = '';
$response = $this->auth($options);
$result = new CreateBucketCnameTokenResult($response);
return $result->getData();
}
/**
* get a cname token for a bucket
*
* @param string $bucket bucket name
* @param array $options
* @return CnameTokenInfo|null
* @throws OssException|RequestCore_Exception
*/
public function getBucketCnameToken($bucket, $cname, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_COMP] = 'token';
$options[self::OSS_CNAME] = $cname;
$response = $this->auth($options);
$result = new GetBucketCnameTokenResult($response);
return $result->getData();
}
/**
* Creates a Live Channel under a bucket
*
* @param string $bucket bucket name
* @param string channelName $channelName
* @param LiveChannelConfig $channelConfig
* @param array $options
* @return LiveChannelInfo|null
* @throws OssException|RequestCore_Exception
*/
public function putBucketLiveChannel($bucket, $channelName, $channelConfig, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
$options[self::OSS_OBJECT] = $channelName;
$options[self::OSS_SUB_RESOURCE] = 'live';
$options[self::OSS_CONTENT_TYPE] = 'application/xml';
$options[self::OSS_CONTENT] = $channelConfig->serializeToXml();
$response = $this->auth($options);
$result = new PutLiveChannelResult($response);
$info = $result->getData();
$info->setName($channelName);
$info->setDescription($channelConfig->getDescription());
return $info;
}
/**
* Sets the LiveChannel status
*
* @param string $bucket bucket name
* @param string channelName $channelName
* @param string channelStatus $channelStatus enabled or disabled
* @param array $options
* @return null
* @throws OssException|RequestCore_Exception
*/
public function putLiveChannelStatus($bucket, $channelName, $channelStatus, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
$options[self::OSS_OBJECT] = $channelName;
$options[self::OSS_SUB_RESOURCE] = 'live';
$options[self::OSS_LIVE_CHANNEL_STATUS] = $channelStatus;
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Gets the LiveChannel information by the channel name
*
* @param string $bucket bucket name
* @param string channelName $channelName
* @param array $options
* @return GetLiveChannelInfo|null
* @throws OssException|RequestCore_Exception
*/
public function getLiveChannelInfo($bucket, $channelName, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_OBJECT] = $channelName;
$options[self::OSS_SUB_RESOURCE] = 'live';
$response = $this->auth($options);
$result = new GetLiveChannelInfoResult($response);
return $result->getData();
}
/**
* Gets the status of LiveChannel
*
* @param string $bucket bucket name
* @param string channelName $channelName
* @param array $options
* @return GetLiveChannelStatus|null
* @throws OssException|RequestCore_Exception
*/
public function getLiveChannelStatus($bucket, $channelName, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_OBJECT] = $channelName;
$options[self::OSS_SUB_RESOURCE] = 'live';
$options[self::OSS_COMP] = 'stat';
$response = $this->auth($options);
$result = new GetLiveChannelStatusResult($response);
return $result->getData();
}
/**
* Gets the LiveChannel pushing streaming record
*
* @param string $bucket bucket name
* @param string channelName $channelName
* @param array $options
* @return GetLiveChannelHistory|null
* @throws OssException|RequestCore_Exception
*/
public function getLiveChannelHistory($bucket, $channelName, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_OBJECT] = $channelName;
$options[self::OSS_SUB_RESOURCE] = 'live';
$options[self::OSS_COMP] = 'history';
$response = $this->auth($options);
$result = new GetLiveChannelHistoryResult($response);
return $result->getData();
}
/**
*Gets the live channel list under a bucket.
*
* @param string $bucket bucket name
* @param array $options
* @return LiveChannelListInfo|null
* @throws OssException|RequestCore_Exception
*/
public function listBucketLiveChannels($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_SUB_RESOURCE] = 'live';
$options[self::OSS_QUERY_STRING] = array(
'prefix' => isset($options['prefix']) ? $options['prefix'] : '',
'marker' => isset($options['marker']) ? $options['marker'] : '',
'max-keys' => isset($options['max-keys']) ? $options['max-keys'] : '',
);
$response = $this->auth($options);
$result = new ListLiveChannelResult($response);
$list = $result->getData();
$list->setBucketName($bucket);
return $list;
}
/**
* Creates a play list file for the LiveChannel
*
* @param string $bucket bucket name
* @param string channelName $channelName
* @param string $playlistName The playlist name, must end with ".m3u8".
* @param array $setTime startTime and EndTime in unix time. No more than 1 day.
* @return null
* @throws OssException|RequestCore_Exception
*/
public function postVodPlaylist($bucket, $channelName, $playlistName, $setTime)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_POST;
$options[self::OSS_OBJECT] = $channelName . '/' . $playlistName;
$options[self::OSS_SUB_RESOURCE] = 'vod';
$options[self::OSS_LIVE_CHANNEL_END_TIME] = $setTime['EndTime'];
$options[self::OSS_LIVE_CHANNEL_START_TIME] = $setTime['StartTime'];
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Deletes the Bucket LiveChannel
*
* @param string $bucket bucket name
* @param string channelName $channelName
* @param array $options
* @return null
* @throws OssException|RequestCore_Exception
*/
public function deleteBucketLiveChannel($bucket, $channelName, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
$options[self::OSS_OBJECT] = $channelName;
$options[self::OSS_SUB_RESOURCE] = 'live';
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* Generates the signed pushing streaming url
*
* @param string $bucket bucket name
* @param string channelName $channelName
* @param int timeout timeout value in seconds
* @param array $options
* @return string The signed pushing streaming url
* @throws OssException
*/
public function signRtmpUrl($bucket, $channelName, $timeout = 60, $options = NULL)
{
$this->precheckCommon($bucket, $channelName, $options, false);
$expires = time() + $timeout;
$proto = 'rtmp://';
$hostname = $this->generateHostname($bucket);
$cano_params = '';
$query_items = array();
$params = isset($options['params']) ? $options['params'] : array();
uksort($params, 'strnatcasecmp');
foreach ($params as $key => $value) {
$cano_params = $cano_params . $key . ':' . $value . "\n";
$query_items[] = rawurlencode($key) . '=' . rawurlencode($value);
}
$resource = '/' . $bucket . '/' . $channelName;
$string_to_sign = $expires . "\n" . $cano_params . $resource;
$cred = $this->provider->getCredentials();
$this->checkCredentials($cred);
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $cred->getAccessKeySecret(), true));
$query_items[] = 'OSSAccessKeyId=' . rawurlencode($cred->getAccessKeyId());
$query_items[] = 'Expires=' . rawurlencode($expires);
$query_items[] = 'Signature=' . rawurlencode($signature);
return $proto . $hostname . '/live/' . $channelName . '?' . implode('&', $query_items);
}
/**
* Generates the signed pushing streaming url
*
* @param string $bucket bucket name
* @param string $channelName channel name
* @param int $expiration expiration time of the Url, unix epoch, since 1970.1.1 00.00.00 UTC
* @param array $options
* @return string The signed pushing streaming url
* @throws OssException
*/
public function generatePresignedRtmpUrl($bucket, $channelName, $expiration, $options = NULL)
{
$this->precheckCommon($bucket, $channelName, $options, false);
$proto = 'rtmp://';
$hostname = $this->generateHostname($bucket);
$cano_params = '';
$query_items = array();
$params = isset($options['params']) ? $options['params'] : array();
uksort($params, 'strnatcasecmp');
foreach ($params as $key => $value) {
$cano_params = $cano_params . $key . ':' . $value . "\n";
$query_items[] = rawurlencode($key) . '=' . rawurlencode($value);
}
$resource = '/' . $bucket . '/' . $channelName;
$string_to_sign = $expiration . "\n" . $cano_params . $resource;
$cred = $this->provider->getCredentials();
$this->checkCredentials($cred);
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $cred->getAccessKeySecret(), true));
$query_items[] = 'OSSAccessKeyId=' . rawurlencode($cred->getAccessKeyId());