OssClient.php 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738
  1. <?php
  2. namespace app\common\services\aliyunoss;
  3. use app\common\services\aliyunoss\OSS\Core\MimeTypes;
  4. use app\common\services\aliyunoss\OSS\Core\OssException;
  5. use app\common\services\aliyunoss\OSS\Http\RequestCore;
  6. use app\common\services\aliyunoss\OSS\Http\RequestCore_Exception;
  7. use app\common\services\aliyunoss\OSS\Http\ResponseCore;
  8. use app\common\services\aliyunoss\OSS\Model\CorsConfig;
  9. use app\common\services\aliyunoss\OSS\Model\CnameConfig;
  10. use app\common\services\aliyunoss\OSS\Model\LoggingConfig;
  11. use app\common\services\aliyunoss\OSS\Model\LiveChannelConfig;
  12. use app\common\services\aliyunoss\OSS\Model\LiveChannelInfo;
  13. use app\common\services\aliyunoss\OSS\Model\LiveChannelListInfo;
  14. use app\common\services\aliyunoss\OSS\Model\StorageCapacityConfig;
  15. use app\common\services\aliyunoss\OSS\Result\AclResult;
  16. use app\common\services\aliyunoss\OSS\Result\BodyResult;
  17. use app\common\services\aliyunoss\OSS\Result\GetCorsResult;
  18. use app\common\services\aliyunoss\OSS\Result\GetLifecycleResult;
  19. use app\common\services\aliyunoss\OSS\Result\GetLocationResult;
  20. use app\common\services\aliyunoss\OSS\Result\GetLoggingResult;
  21. use app\common\services\aliyunoss\OSS\Result\GetRefererResult;
  22. use app\common\services\aliyunoss\OSS\Result\GetStorageCapacityResult;
  23. use app\common\services\aliyunoss\OSS\Result\GetWebsiteResult;
  24. use app\common\services\aliyunoss\OSS\Result\GetCnameResult;
  25. use app\common\services\aliyunoss\OSS\Result\HeaderResult;
  26. use app\common\services\aliyunoss\OSS\Result\InitiateMultipartUploadResult;
  27. use app\common\services\aliyunoss\OSS\Result\ListBucketsResult;
  28. use app\common\services\aliyunoss\OSS\Result\ListMultipartUploadResult;
  29. use app\common\services\aliyunoss\OSS\Model\ListMultipartUploadInfo;
  30. use app\common\services\aliyunoss\OSS\Result\ListObjectsResult;
  31. use app\common\services\aliyunoss\OSS\Result\ListPartsResult;
  32. use app\common\services\aliyunoss\OSS\Result\PutSetDeleteResult;
  33. use app\common\services\aliyunoss\OSS\Result\DeleteObjectsResult;
  34. use app\common\services\aliyunoss\OSS\Result\CopyObjectResult;
  35. use app\common\services\aliyunoss\OSS\Result\CallbackResult;
  36. use app\common\services\aliyunoss\OSS\Result\ExistResult;
  37. use app\common\services\aliyunoss\OSS\Result\PutLiveChannelResult;
  38. use app\common\services\aliyunoss\OSS\Result\GetLiveChannelHistoryResult;
  39. use app\common\services\aliyunoss\OSS\Result\GetLiveChannelInfoResult;
  40. use app\common\services\aliyunoss\OSS\Result\GetLiveChannelStatusResult;
  41. use app\common\services\aliyunoss\OSS\Result\ListLiveChannelResult;
  42. use app\common\services\aliyunoss\OSS\Result\AppendResult;
  43. use app\common\services\aliyunoss\OSS\Model\ObjectListInfo;
  44. use app\common\services\aliyunoss\OSS\Result\SymlinkResult;
  45. use app\common\services\aliyunoss\OSS\Result\UploadPartResult;
  46. use app\common\services\aliyunoss\OSS\Model\BucketListInfo;
  47. use app\common\services\aliyunoss\OSS\Model\LifecycleConfig;
  48. use app\common\services\aliyunoss\OSS\Model\RefererConfig;
  49. use app\common\services\aliyunoss\OSS\Model\WebsiteConfig;
  50. use app\common\services\aliyunoss\OSS\Core\OssUtil;
  51. use app\common\services\aliyunoss\OSS\Model\ListPartsInfo;
  52. /**
  53. * Class OssClient
  54. *
  55. * Object Storage Service(OSS)'s client class, which wraps all OSS APIs user could call to talk to OSS.
  56. * Users could do operations on bucket, object, including MultipartUpload or setting ACL via an OSSClient instance.
  57. * For more details, please check out the OSS API document:https://www.alibabacloud.com/help/doc-detail/31947.htm
  58. */
  59. class OssClient
  60. {
  61. /**
  62. * Constructor
  63. *
  64. * There're a few different ways to create an OssClient object:
  65. * 1. Most common one from access Id, access Key and the endpoint: $ossClient = new OssClient($id, $key, $endpoint)
  66. * 2. If the endpoint is the CName (such as www.testoss.com, make sure it's CName binded in the OSS console),
  67. * uses $ossClient = new OssClient($id, $key, $endpoint, true)
  68. * 3. If using Alicloud's security token service (STS), then the AccessKeyId, AccessKeySecret and STS token are all got from STS.
  69. * Use this: $ossClient = new OssClient($id, $key, $endpoint, false, $token)
  70. * 4. If the endpoint is in IP format, you could use this: $ossClient = new OssClient($id, $key, “1.2.3.4:8900”)
  71. *
  72. * @param string $accessKeyId The AccessKeyId from OSS or STS
  73. * @param string $accessKeySecret The AccessKeySecret from OSS or STS
  74. * @param string $endpoint The domain name of the datacenter,For example: oss-cn-hangzhou.aliyuncs.com
  75. * @param boolean $isCName If this is the CName and binded in the bucket.
  76. * @param string $securityToken from STS.
  77. * @param string $requestProxy
  78. * @throws OssException
  79. */
  80. public function __construct($accessKeyId, $accessKeySecret, $endpoint, $isCName = false, $securityToken = NULL, $requestProxy = NULL)
  81. {
  82. $accessKeyId = trim($accessKeyId);
  83. $accessKeySecret = trim($accessKeySecret);
  84. $endpoint = trim(trim($endpoint), "/");
  85. if (empty($accessKeyId)) {
  86. throw new OssException("access key id is empty");
  87. }
  88. if (empty($accessKeySecret)) {
  89. throw new OssException("access key secret is empty");
  90. }
  91. if (empty($endpoint)) {
  92. throw new OssException("endpoint is empty");
  93. }
  94. $this->hostname = $this->checkEndpoint($endpoint, $isCName);
  95. $this->accessKeyId = $accessKeyId;
  96. $this->accessKeySecret = $accessKeySecret;
  97. $this->securityToken = $securityToken;
  98. $this->requestProxy = $requestProxy;
  99. self::checkEnv();
  100. }
  101. /**
  102. * Lists the Bucket [GetService]. Not applicable if the endpoint is CName (because CName must be binded to a specific bucket).
  103. *
  104. * @param array $options
  105. * @throws OssException
  106. * @return BucketListInfo
  107. */
  108. public function listBuckets($options = NULL)
  109. {
  110. if ($this->hostType === self::OSS_HOST_TYPE_CNAME) {
  111. throw new OssException("operation is not permitted with CName host");
  112. }
  113. $this->precheckOptions($options);
  114. $options[self::OSS_BUCKET] = '';
  115. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  116. $options[self::OSS_OBJECT] = '/';
  117. $response = $this->auth($options);
  118. $result = new ListBucketsResult($response);
  119. return $result->getData();
  120. }
  121. /**
  122. * Creates bucket,The ACL of the bucket created by default is OssClient::OSS_ACL_TYPE_PRIVATE
  123. *
  124. * @param string $bucket
  125. * @param string $acl
  126. * @param array $options
  127. * @param string $storageType
  128. * @return null
  129. */
  130. public function createBucket($bucket, $acl = self::OSS_ACL_TYPE_PRIVATE, $options = NULL)
  131. {
  132. $this->precheckCommon($bucket, NULL, $options, false);
  133. $options[self::OSS_BUCKET] = $bucket;
  134. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  135. $options[self::OSS_OBJECT] = '/';
  136. $options[self::OSS_HEADERS] = array(self::OSS_ACL => $acl);
  137. if (isset($options[self::OSS_STORAGE])) {
  138. $this->precheckStorage($options[self::OSS_STORAGE]);
  139. $options[self::OSS_CONTENT] = OssUtil::createBucketXmlBody($options[self::OSS_STORAGE]);
  140. unset($options[self::OSS_STORAGE]);
  141. }
  142. $response = $this->auth($options);
  143. $result = new PutSetDeleteResult($response);
  144. return $result->getData();
  145. }
  146. /**
  147. * Deletes bucket
  148. * The deletion will not succeed if the bucket is not empty (either has objects or parts)
  149. * To delete a bucket, all its objects and parts must be deleted first.
  150. *
  151. * @param string $bucket
  152. * @param array $options
  153. * @return null
  154. */
  155. public function deleteBucket($bucket, $options = NULL)
  156. {
  157. $this->precheckCommon($bucket, NULL, $options, false);
  158. $options[self::OSS_BUCKET] = $bucket;
  159. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  160. $options[self::OSS_OBJECT] = '/';
  161. $response = $this->auth($options);
  162. $result = new PutSetDeleteResult($response);
  163. return $result->getData();
  164. }
  165. /**
  166. * Checks if a bucket exists
  167. *
  168. * @param string $bucket
  169. * @return bool
  170. * @throws OssException
  171. */
  172. public function doesBucketExist($bucket)
  173. {
  174. $this->precheckCommon($bucket, NULL, $options, false);
  175. $options[self::OSS_BUCKET] = $bucket;
  176. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  177. $options[self::OSS_OBJECT] = '/';
  178. $options[self::OSS_SUB_RESOURCE] = 'acl';
  179. $response = $this->auth($options);
  180. $result = new ExistResult($response);
  181. return $result->getData();
  182. }
  183. /**
  184. * Get the data center location information for the bucket
  185. *
  186. * @param string $bucket
  187. * @param array $options
  188. * @throws OssException
  189. * @return string
  190. */
  191. public function getBucketLocation($bucket, $options = NULL)
  192. {
  193. $this->precheckCommon($bucket, NULL, $options, false);
  194. $options[self::OSS_BUCKET] = $bucket;
  195. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  196. $options[self::OSS_OBJECT] = '/';
  197. $options[self::OSS_SUB_RESOURCE] = 'location';
  198. $response = $this->auth($options);
  199. $result = new GetLocationResult($response);
  200. return $result->getData();
  201. }
  202. /**
  203. * Get the Meta information for the Bucket
  204. *
  205. * @param string $bucket
  206. * @param array $options Refer to the SDK documentation
  207. * @return array
  208. */
  209. public function getBucketMeta($bucket, $options = NULL)
  210. {
  211. $this->precheckCommon($bucket, NULL, $options, false);
  212. $options[self::OSS_BUCKET] = $bucket;
  213. $options[self::OSS_METHOD] = self::OSS_HTTP_HEAD;
  214. $options[self::OSS_OBJECT] = '/';
  215. $response = $this->auth($options);
  216. $result = new HeaderResult($response);
  217. return $result->getData();
  218. }
  219. /**
  220. * Gets the bucket ACL
  221. *
  222. * @param string $bucket
  223. * @param array $options
  224. * @throws OssException
  225. * @return string
  226. */
  227. public function getBucketAcl($bucket, $options = NULL)
  228. {
  229. $this->precheckCommon($bucket, NULL, $options, false);
  230. $options[self::OSS_BUCKET] = $bucket;
  231. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  232. $options[self::OSS_OBJECT] = '/';
  233. $options[self::OSS_SUB_RESOURCE] = 'acl';
  234. $response = $this->auth($options);
  235. $result = new AclResult($response);
  236. return $result->getData();
  237. }
  238. /**
  239. * Sets the bucket ACL
  240. *
  241. * @param string $bucket bucket name
  242. * @param string $acl access permissions, valid values are ['private', 'public-read', 'public-read-write']
  243. * @param array $options by default is empty
  244. * @throws OssException
  245. * @return null
  246. */
  247. public function putBucketAcl($bucket, $acl, $options = NULL)
  248. {
  249. $this->precheckCommon($bucket, NULL, $options, false);
  250. $options[self::OSS_BUCKET] = $bucket;
  251. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  252. $options[self::OSS_OBJECT] = '/';
  253. $options[self::OSS_HEADERS] = array(self::OSS_ACL => $acl);
  254. $options[self::OSS_SUB_RESOURCE] = 'acl';
  255. $response = $this->auth($options);
  256. $result = new PutSetDeleteResult($response);
  257. return $result->getData();
  258. }
  259. /**
  260. * Gets object ACL
  261. *
  262. * @param string $bucket
  263. * @param string $object
  264. * @throws OssException
  265. * @return string
  266. */
  267. public function getObjectAcl($bucket, $object)
  268. {
  269. $options = array();
  270. $this->precheckCommon($bucket, $object, $options, true);
  271. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  272. $options[self::OSS_BUCKET] = $bucket;
  273. $options[self::OSS_OBJECT] = $object;
  274. $options[self::OSS_SUB_RESOURCE] = 'acl';
  275. $response = $this->auth($options);
  276. $result = new AclResult($response);
  277. return $result->getData();
  278. }
  279. /**
  280. * Sets the object ACL
  281. *
  282. * @param string $bucket bucket name
  283. * @param string $object object name
  284. * @param string $acl access permissions, valid values are ['default', 'private', 'public-read', 'public-read-write']
  285. * @throws OssException
  286. * @return null
  287. */
  288. public function putObjectAcl($bucket, $object, $acl)
  289. {
  290. $this->precheckCommon($bucket, $object, $options, true);
  291. $options[self::OSS_BUCKET] = $bucket;
  292. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  293. $options[self::OSS_OBJECT] = $object;
  294. $options[self::OSS_HEADERS] = array(self::OSS_OBJECT_ACL => $acl);
  295. $options[self::OSS_SUB_RESOURCE] = 'acl';
  296. $response = $this->auth($options);
  297. $result = new PutSetDeleteResult($response);
  298. return $result->getData();
  299. }
  300. /**
  301. * Gets the bucket logging config
  302. *
  303. * @param string $bucket bucket name
  304. * @param array $options by default is empty
  305. * @throws OssException
  306. * @return LoggingConfig
  307. */
  308. public function getBucketLogging($bucket, $options = NULL)
  309. {
  310. $this->precheckCommon($bucket, NULL, $options, false);
  311. $options[self::OSS_BUCKET] = $bucket;
  312. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  313. $options[self::OSS_OBJECT] = '/';
  314. $options[self::OSS_SUB_RESOURCE] = 'logging';
  315. $response = $this->auth($options);
  316. $result = new GetLoggingResult($response);
  317. return $result->getData();
  318. }
  319. /**
  320. * Sets the bycket logging config. Only owner can call this API.
  321. *
  322. * @param string $bucket bucket name
  323. * @param string $targetBucket The logging file's bucket
  324. * @param string $targetPrefix The logging file's prefix
  325. * @param array $options By default is empty.
  326. * @throws OssException
  327. * @return null
  328. */
  329. public function putBucketLogging($bucket, $targetBucket, $targetPrefix, $options = NULL)
  330. {
  331. $this->precheckCommon($bucket, NULL, $options, false);
  332. $this->precheckBucket($targetBucket, 'targetbucket is not allowed empty');
  333. $options[self::OSS_BUCKET] = $bucket;
  334. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  335. $options[self::OSS_OBJECT] = '/';
  336. $options[self::OSS_SUB_RESOURCE] = 'logging';
  337. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  338. $loggingConfig = new LoggingConfig($targetBucket, $targetPrefix);
  339. $options[self::OSS_CONTENT] = $loggingConfig->serializeToXml();
  340. $response = $this->auth($options);
  341. $result = new PutSetDeleteResult($response);
  342. return $result->getData();
  343. }
  344. /**
  345. * Deletes the bucket logging config
  346. *
  347. * @param string $bucket bucket name
  348. * @param array $options
  349. * @throws OssException
  350. * @return null
  351. */
  352. public function deleteBucketLogging($bucket, $options = NULL)
  353. {
  354. $this->precheckCommon($bucket, NULL, $options, false);
  355. $options[self::OSS_BUCKET] = $bucket;
  356. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  357. $options[self::OSS_OBJECT] = '/';
  358. $options[self::OSS_SUB_RESOURCE] = 'logging';
  359. $response = $this->auth($options);
  360. $result = new PutSetDeleteResult($response);
  361. return $result->getData();
  362. }
  363. /**
  364. * Sets the website config in bucket---that is could make the bucket as a static website once the CName is binded.
  365. *
  366. * @param string $bucket bucket name
  367. * @param WebsiteConfig $websiteConfig
  368. * @param array $options
  369. * @throws OssException
  370. * @return null
  371. */
  372. public function putBucketWebsite($bucket, $websiteConfig, $options = NULL)
  373. {
  374. $this->precheckCommon($bucket, NULL, $options, false);
  375. $options[self::OSS_BUCKET] = $bucket;
  376. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  377. $options[self::OSS_OBJECT] = '/';
  378. $options[self::OSS_SUB_RESOURCE] = 'website';
  379. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  380. $options[self::OSS_CONTENT] = $websiteConfig->serializeToXml();
  381. $response = $this->auth($options);
  382. $result = new PutSetDeleteResult($response);
  383. return $result->getData();
  384. }
  385. /**
  386. * Gets the website config in the bucket
  387. *
  388. * @param string $bucket bucket name
  389. * @param array $options
  390. * @throws OssException
  391. * @return WebsiteConfig
  392. */
  393. public function getBucketWebsite($bucket, $options = NULL)
  394. {
  395. $this->precheckCommon($bucket, NULL, $options, false);
  396. $options[self::OSS_BUCKET] = $bucket;
  397. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  398. $options[self::OSS_OBJECT] = '/';
  399. $options[self::OSS_SUB_RESOURCE] = 'website';
  400. $response = $this->auth($options);
  401. $result = new GetWebsiteResult($response);
  402. return $result->getData();
  403. }
  404. /**
  405. * Deletes the website config in the bucket
  406. *
  407. * @param string $bucket bucket name
  408. * @param array $options
  409. * @throws OssException
  410. * @return null
  411. */
  412. public function deleteBucketWebsite($bucket, $options = NULL)
  413. {
  414. $this->precheckCommon($bucket, NULL, $options, false);
  415. $options[self::OSS_BUCKET] = $bucket;
  416. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  417. $options[self::OSS_OBJECT] = '/';
  418. $options[self::OSS_SUB_RESOURCE] = 'website';
  419. $response = $this->auth($options);
  420. $result = new PutSetDeleteResult($response);
  421. return $result->getData();
  422. }
  423. /**
  424. * Sets the cross-origin-resource-sharing (CORS) rule. It would overwrite the originl one.
  425. *
  426. * @param string $bucket bucket name
  427. * @param CorsConfig $corsConfig CORS config. Check out the details from OSS API document
  428. * @param array $options array
  429. * @throws OssException
  430. * @return null
  431. */
  432. public function putBucketCors($bucket, $corsConfig, $options = NULL)
  433. {
  434. $this->precheckCommon($bucket, NULL, $options, false);
  435. $options[self::OSS_BUCKET] = $bucket;
  436. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  437. $options[self::OSS_OBJECT] = '/';
  438. $options[self::OSS_SUB_RESOURCE] = 'cors';
  439. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  440. $options[self::OSS_CONTENT] = $corsConfig->serializeToXml();
  441. $response = $this->auth($options);
  442. $result = new PutSetDeleteResult($response);
  443. return $result->getData();
  444. }
  445. /**
  446. * Gets the bucket CORS config
  447. *
  448. * @param string $bucket bucket name
  449. * @param array $options
  450. * @throws OssException
  451. * @return CorsConfig
  452. */
  453. public function getBucketCors($bucket, $options = NULL)
  454. {
  455. $this->precheckCommon($bucket, NULL, $options, false);
  456. $options[self::OSS_BUCKET] = $bucket;
  457. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  458. $options[self::OSS_OBJECT] = '/';
  459. $options[self::OSS_SUB_RESOURCE] = 'cors';
  460. $response = $this->auth($options);
  461. $result = new GetCorsResult($response, __FUNCTION__);
  462. return $result->getData();
  463. }
  464. /**
  465. * Deletes the bucket's CORS config and disable the CORS on the bucket.
  466. *
  467. * @param string $bucket bucket name
  468. * @param array $options
  469. * @throws OssException
  470. * @return null
  471. */
  472. public function deleteBucketCors($bucket, $options = NULL)
  473. {
  474. $this->precheckCommon($bucket, NULL, $options, false);
  475. $options[self::OSS_BUCKET] = $bucket;
  476. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  477. $options[self::OSS_OBJECT] = '/';
  478. $options[self::OSS_SUB_RESOURCE] = 'cors';
  479. $response = $this->auth($options);
  480. $result = new PutSetDeleteResult($response);
  481. return $result->getData();
  482. }
  483. /**
  484. * Bind a CName for the bucket
  485. *
  486. * @param string $bucket bucket name
  487. * @param string $cname
  488. * @param array $options
  489. * @throws OssException
  490. * @return null
  491. */
  492. public function addBucketCname($bucket, $cname, $options = NULL)
  493. {
  494. $this->precheckCommon($bucket, NULL, $options, false);
  495. $options[self::OSS_BUCKET] = $bucket;
  496. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  497. $options[self::OSS_OBJECT] = '/';
  498. $options[self::OSS_SUB_RESOURCE] = 'cname';
  499. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  500. $cnameConfig = new CnameConfig();
  501. $cnameConfig->addCname($cname);
  502. $options[self::OSS_CONTENT] = $cnameConfig->serializeToXml();
  503. $options[self::OSS_COMP] = 'add';
  504. $response = $this->auth($options);
  505. $result = new PutSetDeleteResult($response);
  506. return $result->getData();
  507. }
  508. /**
  509. * Gets the binded CName list of the bucket
  510. *
  511. * @param string $bucket bucket name
  512. * @param array $options
  513. * @throws OssException
  514. * @return CnameConfig
  515. */
  516. public function getBucketCname($bucket, $options = NULL)
  517. {
  518. $this->precheckCommon($bucket, NULL, $options, false);
  519. $options[self::OSS_BUCKET] = $bucket;
  520. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  521. $options[self::OSS_OBJECT] = '/';
  522. $options[self::OSS_SUB_RESOURCE] = 'cname';
  523. $response = $this->auth($options);
  524. $result = new GetCnameResult($response);
  525. return $result->getData();
  526. }
  527. /**
  528. * Remove a CName binding from the bucket
  529. *
  530. * @param string $bucket bucket name
  531. * @param CnameConfig $cnameConfig
  532. * @param array $options
  533. * @throws OssException
  534. * @return null
  535. */
  536. public function deleteBucketCname($bucket, $cname, $options = NULL)
  537. {
  538. $this->precheckCommon($bucket, NULL, $options, false);
  539. $options[self::OSS_BUCKET] = $bucket;
  540. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  541. $options[self::OSS_OBJECT] = '/';
  542. $options[self::OSS_SUB_RESOURCE] = 'cname';
  543. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  544. $cnameConfig = new CnameConfig();
  545. $cnameConfig->addCname($cname);
  546. $options[self::OSS_CONTENT] = $cnameConfig->serializeToXml();
  547. $options[self::OSS_COMP] = 'delete';
  548. $response = $this->auth($options);
  549. $result = new PutSetDeleteResult($response);
  550. return $result->getData();
  551. }
  552. /**
  553. * Creates a Live Channel under a bucket
  554. *
  555. * @param string $bucket bucket name
  556. * @param string channelName $channelName
  557. * @param LiveChannelConfig $channelConfig
  558. * @param array $options
  559. * @throws OssException
  560. * @return LiveChannelInfo
  561. */
  562. public function putBucketLiveChannel($bucket, $channelName, $channelConfig, $options = NULL)
  563. {
  564. $this->precheckCommon($bucket, NULL, $options, false);
  565. $options[self::OSS_BUCKET] = $bucket;
  566. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  567. $options[self::OSS_OBJECT] = $channelName;
  568. $options[self::OSS_SUB_RESOURCE] = 'live';
  569. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  570. $options[self::OSS_CONTENT] = $channelConfig->serializeToXml();
  571. $response = $this->auth($options);
  572. $result = new PutLiveChannelResult($response);
  573. $info = $result->getData();
  574. $info->setName($channelName);
  575. $info->setDescription($channelConfig->getDescription());
  576. return $info;
  577. }
  578. /**
  579. * Sets the LiveChannel status
  580. *
  581. * @param string $bucket bucket name
  582. * @param string channelName $channelName
  583. * @param string channelStatus $channelStatus enabled or disabled
  584. * @param array $options
  585. * @throws OssException
  586. * @return null
  587. */
  588. public function putLiveChannelStatus($bucket, $channelName, $channelStatus, $options = NULL)
  589. {
  590. $this->precheckCommon($bucket, NULL, $options, false);
  591. $options[self::OSS_BUCKET] = $bucket;
  592. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  593. $options[self::OSS_OBJECT] = $channelName;
  594. $options[self::OSS_SUB_RESOURCE] = 'live';
  595. $options[self::OSS_LIVE_CHANNEL_STATUS] = $channelStatus;
  596. $response = $this->auth($options);
  597. $result = new PutSetDeleteResult($response);
  598. return $result->getData();
  599. }
  600. /**
  601. * Gets the LiveChannel information by the channel name
  602. *
  603. * @param string $bucket bucket name
  604. * @param string channelName $channelName
  605. * @param array $options
  606. * @throws OssException
  607. * @return GetLiveChannelInfo
  608. */
  609. public function getLiveChannelInfo($bucket, $channelName, $options = NULL)
  610. {
  611. $this->precheckCommon($bucket, NULL, $options, false);
  612. $options[self::OSS_BUCKET] = $bucket;
  613. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  614. $options[self::OSS_OBJECT] = $channelName;
  615. $options[self::OSS_SUB_RESOURCE] = 'live';
  616. $response = $this->auth($options);
  617. $result = new GetLiveChannelInfoResult($response);
  618. return $result->getData();
  619. }
  620. /**
  621. * Gets the status of LiveChannel
  622. *
  623. * @param string $bucket bucket name
  624. * @param string channelName $channelName
  625. * @param array $options
  626. * @throws OssException
  627. * @return GetLiveChannelStatus
  628. */
  629. public function getLiveChannelStatus($bucket, $channelName, $options = NULL)
  630. {
  631. $this->precheckCommon($bucket, NULL, $options, false);
  632. $options[self::OSS_BUCKET] = $bucket;
  633. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  634. $options[self::OSS_OBJECT] = $channelName;
  635. $options[self::OSS_SUB_RESOURCE] = 'live';
  636. $options[self::OSS_COMP] = 'stat';
  637. $response = $this->auth($options);
  638. $result = new GetLiveChannelStatusResult($response);
  639. return $result->getData();
  640. }
  641. /**
  642. * Gets the LiveChannel pushing streaming record
  643. *
  644. * @param string $bucket bucket name
  645. * @param string channelName $channelName
  646. * @param array $options
  647. * @throws OssException
  648. * @return GetLiveChannelHistory
  649. */
  650. public function getLiveChannelHistory($bucket, $channelName, $options = NULL)
  651. {
  652. $this->precheckCommon($bucket, NULL, $options, false);
  653. $options[self::OSS_BUCKET] = $bucket;
  654. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  655. $options[self::OSS_OBJECT] = $channelName;
  656. $options[self::OSS_SUB_RESOURCE] = 'live';
  657. $options[self::OSS_COMP] = 'history';
  658. $response = $this->auth($options);
  659. $result = new GetLiveChannelHistoryResult($response);
  660. return $result->getData();
  661. }
  662. /**
  663. *Gets the live channel list under a bucket.
  664. *
  665. * @param string $bucket bucket name
  666. * @param array $options
  667. * @throws OssException
  668. * @return LiveChannelListInfo
  669. */
  670. public function listBucketLiveChannels($bucket, $options = NULL)
  671. {
  672. $this->precheckCommon($bucket, NULL, $options, false);
  673. $options[self::OSS_BUCKET] = $bucket;
  674. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  675. $options[self::OSS_OBJECT] = '/';
  676. $options[self::OSS_SUB_RESOURCE] = 'live';
  677. $options[self::OSS_QUERY_STRING] = array(
  678. 'prefix' => isset($options['prefix']) ? $options['prefix'] : '',
  679. 'marker' => isset($options['marker']) ? $options['marker'] : '',
  680. 'max-keys' => isset($options['max-keys']) ? $options['max-keys'] : '',
  681. );
  682. $response = $this->auth($options);
  683. $result = new ListLiveChannelResult($response);
  684. $list = $result->getData();
  685. $list->setBucketName($bucket);
  686. return $list;
  687. }
  688. /**
  689. * Creates a play list file for the LiveChannel
  690. *
  691. * @param string $bucket bucket name
  692. * @param string channelName $channelName
  693. * @param string $playlistName The playlist name, must end with ".m3u8".
  694. * @param array $setTime startTime and EndTime in unix time. No more than 1 day.
  695. * @throws OssException
  696. * @return null
  697. */
  698. public function postVodPlaylist($bucket, $channelName, $playlistName, $setTime)
  699. {
  700. $this->precheckCommon($bucket, NULL, $options, false);
  701. $options[self::OSS_BUCKET] = $bucket;
  702. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  703. $options[self::OSS_OBJECT] = $channelName . '/' . $playlistName;
  704. $options[self::OSS_SUB_RESOURCE] = 'vod';
  705. $options[self::OSS_LIVE_CHANNEL_END_TIME] = $setTime['EndTime'];
  706. $options[self::OSS_LIVE_CHANNEL_START_TIME] = $setTime['StartTime'];
  707. $response = $this->auth($options);
  708. $result = new PutSetDeleteResult($response);
  709. return $result->getData();
  710. }
  711. /**
  712. * Deletes the Bucket LiveChannel
  713. *
  714. * @param string $bucket bucket name
  715. * @param string channelName $channelName
  716. * @param array $options
  717. * @throws OssException
  718. * @return null
  719. */
  720. public function deleteBucketLiveChannel($bucket, $channelName, $options = NULL)
  721. {
  722. $this->precheckCommon($bucket, NULL, $options, false);
  723. $options[self::OSS_BUCKET] = $bucket;
  724. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  725. $options[self::OSS_OBJECT] = $channelName;
  726. $options[self::OSS_SUB_RESOURCE] = 'live';
  727. $response = $this->auth($options);
  728. $result = new PutSetDeleteResult($response);
  729. return $result->getData();
  730. }
  731. /**
  732. * Generates the signed pushing streaming url
  733. *
  734. * @param string $bucket bucket name
  735. * @param string channelName $channelName
  736. * @param int timeout timeout value in seconds
  737. * @param array $options
  738. * @throws OssException
  739. * @return The signed pushing streaming url
  740. */
  741. public function signRtmpUrl($bucket, $channelName, $timeout = 60, $options = NULL)
  742. {
  743. $this->precheckCommon($bucket, $channelName, $options, false);
  744. $expires = time() + $timeout;
  745. $proto = 'rtmp://';
  746. $hostname = $this->generateHostname($bucket);
  747. $cano_params = '';
  748. $query_items = array();
  749. $params = isset($options['params']) ? $options['params'] : array();
  750. uksort($params, 'strnatcasecmp');
  751. foreach ($params as $key => $value) {
  752. $cano_params = $cano_params . $key . ':' . $value . "\n";
  753. $query_items[] = rawurlencode($key) . '=' . rawurlencode($value);
  754. }
  755. $resource = '/' . $bucket . '/' . $channelName;
  756. $string_to_sign = $expires . "\n" . $cano_params . $resource;
  757. $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->accessKeySecret, true));
  758. $query_items[] = 'OSSAccessKeyId=' . rawurlencode($this->accessKeyId);
  759. $query_items[] = 'Expires=' . rawurlencode($expires);
  760. $query_items[] = 'Signature=' . rawurlencode($signature);
  761. return $proto . $hostname . '/live/' . $channelName . '?' . implode('&', $query_items);
  762. }
  763. /**
  764. * Precheck the CORS request. Before sending a CORS request, a preflight request (OPTIONS) is sent with the specific origin.
  765. * HTTP METHOD and headers information are sent to OSS as well for evaluating if the CORS request is allowed.
  766. *
  767. * Note: OSS could enable the CORS on the bucket by calling putBucketCors. Once CORS is enabled, the OSS could evaluate accordingto the preflight request.
  768. *
  769. * @param string $bucket bucket name
  770. * @param string $object object name
  771. * @param string $origin the origin of the request
  772. * @param string $request_method The actual HTTP method which will be used in CORS request
  773. * @param string $request_headers The actual HTTP headers which will be used in CORS request
  774. * @param array $options
  775. * @return array
  776. * @throws OssException
  777. * @link http://help.aliyun.com/document_detail/oss/api-reference/cors/OptionObject.html
  778. */
  779. public function optionsObject($bucket, $object, $origin, $request_method, $request_headers, $options = NULL)
  780. {
  781. $this->precheckCommon($bucket, NULL, $options, false);
  782. $options[self::OSS_BUCKET] = $bucket;
  783. $options[self::OSS_METHOD] = self::OSS_HTTP_OPTIONS;
  784. $options[self::OSS_OBJECT] = $object;
  785. $options[self::OSS_HEADERS] = array(
  786. self::OSS_OPTIONS_ORIGIN => $origin,
  787. self::OSS_OPTIONS_REQUEST_HEADERS => $request_headers,
  788. self::OSS_OPTIONS_REQUEST_METHOD => $request_method
  789. );
  790. $response = $this->auth($options);
  791. $result = new HeaderResult($response);
  792. return $result->getData();
  793. }
  794. /**
  795. * Sets the bucket's lifecycle config
  796. *
  797. * @param string $bucket bucket name
  798. * @param LifecycleConfig $lifecycleConfig LifecycleConfig instance
  799. * @param array $options
  800. * @throws OssException
  801. * @return null
  802. */
  803. public function putBucketLifecycle($bucket, $lifecycleConfig, $options = NULL)
  804. {
  805. $this->precheckCommon($bucket, NULL, $options, false);
  806. $options[self::OSS_BUCKET] = $bucket;
  807. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  808. $options[self::OSS_OBJECT] = '/';
  809. $options[self::OSS_SUB_RESOURCE] = 'lifecycle';
  810. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  811. $options[self::OSS_CONTENT] = $lifecycleConfig->serializeToXml();
  812. $response = $this->auth($options);
  813. $result = new PutSetDeleteResult($response);
  814. return $result->getData();
  815. }
  816. /**
  817. * Gets bucket's lifecycle config
  818. *
  819. * @param string $bucket bucket name
  820. * @param array $options
  821. * @throws OssException
  822. * @return LifecycleConfig
  823. */
  824. public function getBucketLifecycle($bucket, $options = NULL)
  825. {
  826. $this->precheckCommon($bucket, NULL, $options, false);
  827. $options[self::OSS_BUCKET] = $bucket;
  828. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  829. $options[self::OSS_OBJECT] = '/';
  830. $options[self::OSS_SUB_RESOURCE] = 'lifecycle';
  831. $response = $this->auth($options);
  832. $result = new GetLifecycleResult($response);
  833. return $result->getData();
  834. }
  835. /**
  836. * Deletes the bucket's lifecycle config
  837. *
  838. * @param string $bucket bucket name
  839. * @param array $options
  840. * @throws OssException
  841. * @return null
  842. */
  843. public function deleteBucketLifecycle($bucket, $options = NULL)
  844. {
  845. $this->precheckCommon($bucket, NULL, $options, false);
  846. $options[self::OSS_BUCKET] = $bucket;
  847. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  848. $options[self::OSS_OBJECT] = '/';
  849. $options[self::OSS_SUB_RESOURCE] = 'lifecycle';
  850. $response = $this->auth($options);
  851. $result = new PutSetDeleteResult($response);
  852. return $result->getData();
  853. }
  854. /**
  855. * Sets a bucket's referer, which has a whitelist of referrer and specifies if empty referer is allowed.
  856. * Checks out API document for more details about "Bucket Referer"
  857. *
  858. * @param string $bucket bucket name
  859. * @param RefererConfig $refererConfig
  860. * @param array $options
  861. * @return ResponseCore
  862. * @throws null
  863. */
  864. public function putBucketReferer($bucket, $refererConfig, $options = NULL)
  865. {
  866. $this->precheckCommon($bucket, NULL, $options, false);
  867. $options[self::OSS_BUCKET] = $bucket;
  868. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  869. $options[self::OSS_OBJECT] = '/';
  870. $options[self::OSS_SUB_RESOURCE] = 'referer';
  871. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  872. $options[self::OSS_CONTENT] = $refererConfig->serializeToXml();
  873. $response = $this->auth($options);
  874. $result = new PutSetDeleteResult($response);
  875. return $result->getData();
  876. }
  877. /**
  878. * Gets the bucket's Referer
  879. * Checks out API document for more details about "Bucket Referer"
  880. *
  881. * @param string $bucket bucket name
  882. * @param array $options
  883. * @throws OssException
  884. * @return RefererConfig
  885. */
  886. public function getBucketReferer($bucket, $options = NULL)
  887. {
  888. $this->precheckCommon($bucket, NULL, $options, false);
  889. $options[self::OSS_BUCKET] = $bucket;
  890. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  891. $options[self::OSS_OBJECT] = '/';
  892. $options[self::OSS_SUB_RESOURCE] = 'referer';
  893. $response = $this->auth($options);
  894. $result = new GetRefererResult($response);
  895. return $result->getData();
  896. }
  897. /**
  898. * Set the size of the bucket,the unit is GB
  899. * When the capacity of the bucket is bigger than the set, it's forbidden to continue writing
  900. *
  901. * @param string $bucket bucket name
  902. * @param int $storageCapacity
  903. * @param array $options
  904. * @return ResponseCore
  905. * @throws null
  906. */
  907. public function putBucketStorageCapacity($bucket, $storageCapacity, $options = NULL)
  908. {
  909. $this->precheckCommon($bucket, NULL, $options, false);
  910. $options[self::OSS_BUCKET] = $bucket;
  911. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  912. $options[self::OSS_OBJECT] = '/';
  913. $options[self::OSS_SUB_RESOURCE] = 'qos';
  914. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  915. $storageCapacityConfig = new StorageCapacityConfig($storageCapacity);
  916. $options[self::OSS_CONTENT] = $storageCapacityConfig->serializeToXml();
  917. $response = $this->auth($options);
  918. $result = new PutSetDeleteResult($response);
  919. return $result->getData();
  920. }
  921. /**
  922. * Get the capacity of the bucket, the unit is GB
  923. *
  924. * @param string $bucket bucket name
  925. * @param array $options
  926. * @throws OssException
  927. * @return int
  928. */
  929. public function getBucketStorageCapacity($bucket, $options = NULL)
  930. {
  931. $this->precheckCommon($bucket, NULL, $options, false);
  932. $options[self::OSS_BUCKET] = $bucket;
  933. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  934. $options[self::OSS_OBJECT] = '/';
  935. $options[self::OSS_SUB_RESOURCE] = 'qos';
  936. $response = $this->auth($options);
  937. $result = new GetStorageCapacityResult($response);
  938. return $result->getData();
  939. }
  940. /**
  941. * Lists the bucket's object list (in ObjectListInfo)
  942. *
  943. * @param string $bucket
  944. * @param array $options are defined below:
  945. * $options = array(
  946. * 'max-keys' => specifies max object count to return. By default is 100 and max value could be 1000.
  947. * 'prefix' => specifies the key prefix the returned objects must have. Note that the returned keys still contain the prefix.
  948. * 'delimiter' => The delimiter of object name for grouping object. When it's specified, listObjects will differeniate the object and folder. And it will return subfolder's objects.
  949. * 'marker' => The key of returned object must be greater than the 'marker'.
  950. *)
  951. * Prefix and marker are for filtering and paging. Their length must be less than 256 bytes
  952. * @throws OssException
  953. * @return ObjectListInfo
  954. */
  955. public function listObjects($bucket, $options = NULL)
  956. {
  957. $this->precheckCommon($bucket, NULL, $options, false);
  958. $options[self::OSS_BUCKET] = $bucket;
  959. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  960. $options[self::OSS_OBJECT] = '/';
  961. $options[self::OSS_HEADERS] = array(
  962. self::OSS_DELIMITER => isset($options[self::OSS_DELIMITER]) ? $options[self::OSS_DELIMITER] : '/',
  963. self::OSS_PREFIX => isset($options[self::OSS_PREFIX]) ? $options[self::OSS_PREFIX] : '',
  964. self::OSS_MAX_KEYS => isset($options[self::OSS_MAX_KEYS]) ? $options[self::OSS_MAX_KEYS] : self::OSS_MAX_KEYS_VALUE,
  965. self::OSS_MARKER => isset($options[self::OSS_MARKER]) ? $options[self::OSS_MARKER] : '',
  966. );
  967. $query = isset($options[self::OSS_QUERY_STRING]) ? $options[self::OSS_QUERY_STRING] : array();
  968. $options[self::OSS_QUERY_STRING] = array_merge(
  969. $query,
  970. array(self::OSS_ENCODING_TYPE => self::OSS_ENCODING_TYPE_URL)
  971. );
  972. $response = $this->auth($options);
  973. $result = new ListObjectsResult($response);
  974. return $result->getData();
  975. }
  976. /**
  977. * Creates a virtual 'folder' in OSS. The name should not end with '/' because the method will append the name with a '/' anyway.
  978. *
  979. * Internal use only.
  980. *
  981. * @param string $bucket bucket name
  982. * @param string $object object name
  983. * @param array $options
  984. * @return null
  985. */
  986. public function createObjectDir($bucket, $object, $options = NULL)
  987. {
  988. $this->precheckCommon($bucket, $object, $options);
  989. $options[self::OSS_BUCKET] = $bucket;
  990. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  991. $options[self::OSS_OBJECT] = $object . '/';
  992. $options[self::OSS_CONTENT_LENGTH] = array(self::OSS_CONTENT_LENGTH => 0);
  993. $response = $this->auth($options);
  994. $result = new PutSetDeleteResult($response);
  995. return $result->getData();
  996. }
  997. /**
  998. * Uploads the $content object to OSS.
  999. *
  1000. * @param string $bucket bucket name
  1001. * @param string $object objcet name
  1002. * @param string $content The content object
  1003. * @param array $options
  1004. * @return null
  1005. */
  1006. public function putObject($bucket, $object, $content, $options = NULL)
  1007. {
  1008. $this->precheckCommon($bucket, $object, $options);
  1009. $options[self::OSS_CONTENT] = $content;
  1010. $options[self::OSS_BUCKET] = $bucket;
  1011. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1012. $options[self::OSS_OBJECT] = $object;
  1013. if (!isset($options[self::OSS_LENGTH])) {
  1014. $options[self::OSS_CONTENT_LENGTH] = strlen($options[self::OSS_CONTENT]);
  1015. } else {
  1016. $options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH];
  1017. }
  1018. $is_check_md5 = $this->isCheckMD5($options);
  1019. if ($is_check_md5) {
  1020. $content_md5 = base64_encode(md5($content, true));
  1021. $options[self::OSS_CONTENT_MD5] = $content_md5;
  1022. }
  1023. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  1024. $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object);
  1025. }
  1026. $response = $this->auth($options);
  1027. if (isset($options[self::OSS_CALLBACK]) && !empty($options[self::OSS_CALLBACK])) {
  1028. $result = new CallbackResult($response);
  1029. } else {
  1030. $result = new PutSetDeleteResult($response);
  1031. }
  1032. return $result->getData();
  1033. }
  1034. /**
  1035. * creates symlink
  1036. * @param string $bucket bucket name
  1037. * @param string $symlink symlink name
  1038. * @param string $targetObject targetObject name
  1039. * @param array $options
  1040. * @return null
  1041. */
  1042. public function putSymlink($bucket, $symlink ,$targetObject, $options = NULL)
  1043. {
  1044. $this->precheckCommon($bucket, $symlink, $options);
  1045. $options[self::OSS_BUCKET] = $bucket;
  1046. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1047. $options[self::OSS_OBJECT] = $symlink;
  1048. $options[self::OSS_SUB_RESOURCE] = self::OSS_SYMLINK;
  1049. $options[self::OSS_HEADERS][self::OSS_SYMLINK_TARGET] = rawurlencode($targetObject);
  1050. $response = $this->auth($options);
  1051. $result = new PutSetDeleteResult($response);
  1052. return $result->getData();
  1053. }
  1054. /**
  1055. * gets symlink
  1056. *@param string $bucket bucket name
  1057. * @param string $symlink symlink name
  1058. * @return null
  1059. */
  1060. public function getSymlink($bucket, $symlink)
  1061. {
  1062. $this->precheckCommon($bucket, $symlink, $options);
  1063. $options[self::OSS_BUCKET] = $bucket;
  1064. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1065. $options[self::OSS_OBJECT] = $symlink;
  1066. $options[self::OSS_SUB_RESOURCE] = self::OSS_SYMLINK;
  1067. $response = $this->auth($options);
  1068. $result = new SymlinkResult($response);
  1069. return $result->getData();
  1070. }
  1071. /**
  1072. * Uploads a local file
  1073. *
  1074. * @param string $bucket bucket name
  1075. * @param string $object object name
  1076. * @param string $file local file path
  1077. * @param array $options
  1078. * @return null
  1079. * @throws OssException
  1080. */
  1081. public function uploadFile($bucket, $object, $file, $options = NULL)
  1082. {
  1083. $this->precheckCommon($bucket, $object, $options);
  1084. OssUtil::throwOssExceptionWithMessageIfEmpty($file, "file path is invalid");
  1085. $file = OssUtil::encodePath($file);
  1086. if (!file_exists($file)) {
  1087. throw new OssException($file . " file does not exist");
  1088. }
  1089. $options[self::OSS_FILE_UPLOAD] = $file;
  1090. $file_size = filesize($options[self::OSS_FILE_UPLOAD]);
  1091. $is_check_md5 = $this->isCheckMD5($options);
  1092. if ($is_check_md5) {
  1093. $content_md5 = base64_encode(md5_file($options[self::OSS_FILE_UPLOAD], true));
  1094. $options[self::OSS_CONTENT_MD5] = $content_md5;
  1095. }
  1096. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  1097. $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object, $file);
  1098. }
  1099. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1100. $options[self::OSS_BUCKET] = $bucket;
  1101. $options[self::OSS_OBJECT] = $object;
  1102. $options[self::OSS_CONTENT_LENGTH] = $file_size;
  1103. $response = $this->auth($options);
  1104. $result = new PutSetDeleteResult($response);
  1105. return $result->getData();
  1106. }
  1107. /**
  1108. * Append the object with the content at the specified position.
  1109. * The specified position is typically the lengh of the current file.
  1110. * @param string $bucket bucket name
  1111. * @param string $object objcet name
  1112. * @param string $content content to append
  1113. * @param array $options
  1114. * @return int next append position
  1115. * @throws OssException
  1116. */
  1117. public function appendObject($bucket, $object, $content, $position, $options = NULL)
  1118. {
  1119. $this->precheckCommon($bucket, $object, $options);
  1120. $options[self::OSS_CONTENT] = $content;
  1121. $options[self::OSS_BUCKET] = $bucket;
  1122. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1123. $options[self::OSS_OBJECT] = $object;
  1124. $options[self::OSS_SUB_RESOURCE] = 'append';
  1125. $options[self::OSS_POSITION] = strval($position);
  1126. if (!isset($options[self::OSS_LENGTH])) {
  1127. $options[self::OSS_CONTENT_LENGTH] = strlen($options[self::OSS_CONTENT]);
  1128. } else {
  1129. $options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH];
  1130. }
  1131. $is_check_md5 = $this->isCheckMD5($options);
  1132. if ($is_check_md5) {
  1133. $content_md5 = base64_encode(md5($content, true));
  1134. $options[self::OSS_CONTENT_MD5] = $content_md5;
  1135. }
  1136. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  1137. $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object);
  1138. }
  1139. $response = $this->auth($options);
  1140. $result = new AppendResult($response);
  1141. return $result->getData();
  1142. }
  1143. /**
  1144. * Append the object with a local file
  1145. *
  1146. * @param string $bucket bucket name
  1147. * @param string $object object name
  1148. * @param string $file The local file path to append with
  1149. * @param array $options
  1150. * @return int next append position
  1151. * @throws OssException
  1152. */
  1153. public function appendFile($bucket, $object, $file, $position, $options = NULL)
  1154. {
  1155. $this->precheckCommon($bucket, $object, $options);
  1156. OssUtil::throwOssExceptionWithMessageIfEmpty($file, "file path is invalid");
  1157. $file = OssUtil::encodePath($file);
  1158. if (!file_exists($file)) {
  1159. throw new OssException($file . " file does not exist");
  1160. }
  1161. $options[self::OSS_FILE_UPLOAD] = $file;
  1162. $file_size = filesize($options[self::OSS_FILE_UPLOAD]);
  1163. $is_check_md5 = $this->isCheckMD5($options);
  1164. if ($is_check_md5) {
  1165. $content_md5 = base64_encode(md5_file($options[self::OSS_FILE_UPLOAD], true));
  1166. $options[self::OSS_CONTENT_MD5] = $content_md5;
  1167. }
  1168. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  1169. $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object, $file);
  1170. }
  1171. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1172. $options[self::OSS_BUCKET] = $bucket;
  1173. $options[self::OSS_OBJECT] = $object;
  1174. $options[self::OSS_CONTENT_LENGTH] = $file_size;
  1175. $options[self::OSS_SUB_RESOURCE] = 'append';
  1176. $options[self::OSS_POSITION] = strval($position);
  1177. $response = $this->auth($options);
  1178. $result = new AppendResult($response);
  1179. return $result->getData();
  1180. }
  1181. /**
  1182. * Copy from an existing OSS object to another OSS object. If the target object exists already, it will be overwritten.
  1183. *
  1184. * @param string $fromBucket Source bucket name
  1185. * @param string $fromObject Source object name
  1186. * @param string $toBucket Target bucket name
  1187. * @param string $toObject Target object name
  1188. * @param array $options
  1189. * @return null
  1190. * @throws OssException
  1191. */
  1192. public function copyObject($fromBucket, $fromObject, $toBucket, $toObject, $options = NULL)
  1193. {
  1194. $this->precheckCommon($fromBucket, $fromObject, $options);
  1195. $this->precheckCommon($toBucket, $toObject, $options);
  1196. $options[self::OSS_BUCKET] = $toBucket;
  1197. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1198. $options[self::OSS_OBJECT] = $toObject;
  1199. if (isset($options[self::OSS_HEADERS])) {
  1200. $options[self::OSS_HEADERS][self::OSS_OBJECT_COPY_SOURCE] = '/' . $fromBucket . '/' . $fromObject;
  1201. } else {
  1202. $options[self::OSS_HEADERS] = array(self::OSS_OBJECT_COPY_SOURCE => '/' . $fromBucket . '/' . $fromObject);
  1203. }
  1204. $response = $this->auth($options);
  1205. $result = new CopyObjectResult($response);
  1206. return $result->getData();
  1207. }
  1208. /**
  1209. * Gets Object metadata
  1210. *
  1211. * @param string $bucket bucket name
  1212. * @param string $object object name
  1213. * @param string $options Checks out the SDK document for the detail
  1214. * @return array
  1215. */
  1216. public function getObjectMeta($bucket, $object, $options = NULL)
  1217. {
  1218. $this->precheckCommon($bucket, $object, $options);
  1219. $options[self::OSS_BUCKET] = $bucket;
  1220. $options[self::OSS_METHOD] = self::OSS_HTTP_HEAD;
  1221. $options[self::OSS_OBJECT] = $object;
  1222. $response = $this->auth($options);
  1223. $result = new HeaderResult($response);
  1224. return $result->getData();
  1225. }
  1226. /**
  1227. * Deletes a object
  1228. *
  1229. * @param string $bucket bucket name
  1230. * @param string $object object name
  1231. * @param array $options
  1232. * @return null
  1233. */
  1234. public function deleteObject($bucket, $object, $options = NULL)
  1235. {
  1236. $this->precheckCommon($bucket, $object, $options);
  1237. $options[self::OSS_BUCKET] = $bucket;
  1238. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  1239. $options[self::OSS_OBJECT] = $object;
  1240. $response = $this->auth($options);
  1241. $result = new PutSetDeleteResult($response);
  1242. return $result->getData();
  1243. }
  1244. /**
  1245. * Deletes multiple objects in a bucket
  1246. *
  1247. * @param string $bucket bucket name
  1248. * @param array $objects object list
  1249. * @param array $options
  1250. * @return ResponseCore
  1251. * @throws null
  1252. */
  1253. public function deleteObjects($bucket, $objects, $options = null)
  1254. {
  1255. $this->precheckCommon($bucket, NULL, $options, false);
  1256. if (!is_array($objects) || !$objects) {
  1257. throw new OssException('objects must be array');
  1258. }
  1259. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1260. $options[self::OSS_BUCKET] = $bucket;
  1261. $options[self::OSS_OBJECT] = '/';
  1262. $options[self::OSS_SUB_RESOURCE] = 'delete';
  1263. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  1264. $quiet = 'false';
  1265. if (isset($options['quiet'])) {
  1266. if (is_bool($options['quiet'])) { //Boolean
  1267. $quiet = $options['quiet'] ? 'true' : 'false';
  1268. } elseif (is_string($options['quiet'])) { // string
  1269. $quiet = ($options['quiet'] === 'true') ? 'true' : 'false';
  1270. }
  1271. }
  1272. $xmlBody = OssUtil::createDeleteObjectsXmlBody($objects, $quiet);
  1273. $options[self::OSS_CONTENT] = $xmlBody;
  1274. $response = $this->auth($options);
  1275. $result = new DeleteObjectsResult($response);
  1276. return $result->getData();
  1277. }
  1278. /**
  1279. * Gets Object content
  1280. *
  1281. * @param string $bucket bucket name
  1282. * @param string $object object name
  1283. * @param array $options It must contain ALIOSS::OSS_FILE_DOWNLOAD. And ALIOSS::OSS_RANGE is optional and empty means to download the whole file.
  1284. * @return string
  1285. */
  1286. public function getObject($bucket, $object, $options = NULL)
  1287. {
  1288. $this->precheckCommon($bucket, $object, $options);
  1289. $options[self::OSS_BUCKET] = $bucket;
  1290. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1291. $options[self::OSS_OBJECT] = $object;
  1292. if (isset($options[self::OSS_LAST_MODIFIED])) {
  1293. $options[self::OSS_HEADERS][self::OSS_IF_MODIFIED_SINCE] = $options[self::OSS_LAST_MODIFIED];
  1294. unset($options[self::OSS_LAST_MODIFIED]);
  1295. }
  1296. if (isset($options[self::OSS_ETAG])) {
  1297. $options[self::OSS_HEADERS][self::OSS_IF_NONE_MATCH] = $options[self::OSS_ETAG];
  1298. unset($options[self::OSS_ETAG]);
  1299. }
  1300. if (isset($options[self::OSS_RANGE])) {
  1301. $range = $options[self::OSS_RANGE];
  1302. $options[self::OSS_HEADERS][self::OSS_RANGE] = "bytes=$range";
  1303. unset($options[self::OSS_RANGE]);
  1304. }
  1305. $response = $this->auth($options);
  1306. $result = new BodyResult($response);
  1307. return $result->getData();
  1308. }
  1309. /**
  1310. * Checks if the object exists
  1311. * It's implemented by getObjectMeta().
  1312. *
  1313. * @param string $bucket bucket name
  1314. * @param string $object object name
  1315. * @param array $options
  1316. * @return bool True:object exists; False:object does not exist
  1317. */
  1318. public function doesObjectExist($bucket, $object, $options = NULL)
  1319. {
  1320. $this->precheckCommon($bucket, $object, $options);
  1321. $options[self::OSS_BUCKET] = $bucket;
  1322. $options[self::OSS_METHOD] = self::OSS_HTTP_HEAD;
  1323. $options[self::OSS_OBJECT] = $object;
  1324. $response = $this->auth($options);
  1325. $result = new ExistResult($response);
  1326. return $result->getData();
  1327. }
  1328. /**
  1329. * Object reading for Archive type
  1330. * Use Restore to enable the server to perform the thawing task
  1331. *
  1332. * @param string $bucket bucket name
  1333. * @param string $object object name
  1334. * @return null
  1335. * @throws OssException
  1336. */
  1337. public function restoreObject($bucket, $object, $options = NULL)
  1338. {
  1339. $this->precheckCommon($bucket, $object, $options);
  1340. $options[self::OSS_BUCKET] = $bucket;
  1341. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1342. $options[self::OSS_OBJECT] = $object;
  1343. $options[self::OSS_SUB_RESOURCE] = self::OSS_RESTORE;
  1344. $response = $this->auth($options);
  1345. $result = new PutSetDeleteResult($response);
  1346. return $result->getData();
  1347. }
  1348. /**
  1349. * Gets the part size according to the preferred part size.
  1350. * If the specified part size is too small or too big, it will return a min part or max part size instead.
  1351. * Otherwise returns the specified part size.
  1352. * @param int $partSize
  1353. * @return int
  1354. */
  1355. private function computePartSize($partSize)
  1356. {
  1357. $partSize = (integer)$partSize;
  1358. if ($partSize <= self::OSS_MIN_PART_SIZE) {
  1359. $partSize = self::OSS_MIN_PART_SIZE;
  1360. } elseif ($partSize > self::OSS_MAX_PART_SIZE) {
  1361. $partSize = self::OSS_MAX_PART_SIZE;
  1362. }
  1363. return $partSize;
  1364. }
  1365. /**
  1366. * Computes the parts count, size and start position according to the file size and the part size.
  1367. * It must be only called by upload_Part().
  1368. *
  1369. * @param integer $file_size File size
  1370. * @param integer $partSize part大小,part size. Default is 5MB
  1371. * @return array An array contains key-value pairs--the key is `seekTo`and value is `length`.
  1372. */
  1373. public function generateMultiuploadParts($file_size, $partSize = 5242880)
  1374. {
  1375. $i = 0;
  1376. $size_count = $file_size;
  1377. $values = array();
  1378. $partSize = $this->computePartSize($partSize);
  1379. while ($size_count > 0) {
  1380. $size_count -= $partSize;
  1381. $values[] = array(
  1382. self::OSS_SEEK_TO => ($partSize * $i),
  1383. self::OSS_LENGTH => (($size_count > 0) ? $partSize : ($size_count + $partSize)),
  1384. );
  1385. $i++;
  1386. }
  1387. return $values;
  1388. }
  1389. /**
  1390. * Initialize a multi-part upload
  1391. *
  1392. * @param string $bucket bucket name
  1393. * @param string $object object name
  1394. * @param array $options Key-Value array
  1395. * @throws OssException
  1396. * @return string returns uploadid
  1397. */
  1398. public function initiateMultipartUpload($bucket, $object, $options = NULL)
  1399. {
  1400. $this->precheckCommon($bucket, $object, $options);
  1401. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1402. $options[self::OSS_BUCKET] = $bucket;
  1403. $options[self::OSS_OBJECT] = $object;
  1404. $options[self::OSS_SUB_RESOURCE] = 'uploads';
  1405. $options[self::OSS_CONTENT] = '';
  1406. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  1407. $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object);
  1408. }
  1409. if (!isset($options[self::OSS_HEADERS])) {
  1410. $options[self::OSS_HEADERS] = array();
  1411. }
  1412. $response = $this->auth($options);
  1413. $result = new InitiateMultipartUploadResult($response);
  1414. return $result->getData();
  1415. }
  1416. /**
  1417. * Upload a part in a multiparts upload.
  1418. *
  1419. * @param string $bucket bucket name
  1420. * @param string $object object name
  1421. * @param string $uploadId
  1422. * @param array $options Key-Value array
  1423. * @return string eTag
  1424. * @throws OssException
  1425. */
  1426. public function uploadPart($bucket, $object, $uploadId, $options = null)
  1427. {
  1428. $this->precheckCommon($bucket, $object, $options);
  1429. $this->precheckParam($options, self::OSS_FILE_UPLOAD, __FUNCTION__);
  1430. $this->precheckParam($options, self::OSS_PART_NUM, __FUNCTION__);
  1431. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1432. $options[self::OSS_BUCKET] = $bucket;
  1433. $options[self::OSS_OBJECT] = $object;
  1434. $options[self::OSS_UPLOAD_ID] = $uploadId;
  1435. if (isset($options[self::OSS_LENGTH])) {
  1436. $options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH];
  1437. }
  1438. $response = $this->auth($options);
  1439. $result = new UploadPartResult($response);
  1440. return $result->getData();
  1441. }
  1442. /**
  1443. * Gets the uploaded parts.
  1444. *
  1445. * @param string $bucket bucket name
  1446. * @param string $object object name
  1447. * @param string $uploadId uploadId
  1448. * @param array $options Key-Value array
  1449. * @return ListPartsInfo
  1450. * @throws OssException
  1451. */
  1452. public function listParts($bucket, $object, $uploadId, $options = null)
  1453. {
  1454. $this->precheckCommon($bucket, $object, $options);
  1455. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1456. $options[self::OSS_BUCKET] = $bucket;
  1457. $options[self::OSS_OBJECT] = $object;
  1458. $options[self::OSS_UPLOAD_ID] = $uploadId;
  1459. $options[self::OSS_QUERY_STRING] = array();
  1460. foreach (array('max-parts', 'part-number-marker') as $param) {
  1461. if (isset($options[$param])) {
  1462. $options[self::OSS_QUERY_STRING][$param] = $options[$param];
  1463. unset($options[$param]);
  1464. }
  1465. }
  1466. $response = $this->auth($options);
  1467. $result = new ListPartsResult($response);
  1468. return $result->getData();
  1469. }
  1470. /**
  1471. * Abort a multiparts upload
  1472. *
  1473. * @param string $bucket bucket name
  1474. * @param string $object object name
  1475. * @param string $uploadId uploadId
  1476. * @param array $options Key-Value name
  1477. * @return null
  1478. * @throws OssException
  1479. */
  1480. public function abortMultipartUpload($bucket, $object, $uploadId, $options = NULL)
  1481. {
  1482. $this->precheckCommon($bucket, $object, $options);
  1483. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  1484. $options[self::OSS_BUCKET] = $bucket;
  1485. $options[self::OSS_OBJECT] = $object;
  1486. $options[self::OSS_UPLOAD_ID] = $uploadId;
  1487. $response = $this->auth($options);
  1488. $result = new PutSetDeleteResult($response);
  1489. return $result->getData();
  1490. }
  1491. /**
  1492. * Completes a multiparts upload, after all parts are uploaded.
  1493. *
  1494. * @param string $bucket bucket name
  1495. * @param string $object object name
  1496. * @param string $uploadId uploadId
  1497. * @param array $listParts array( array("PartNumber"=> int, "ETag"=>string))
  1498. * @param array $options Key-Value array
  1499. * @throws OssException
  1500. * @return null
  1501. */
  1502. public function completeMultipartUpload($bucket, $object, $uploadId, $listParts, $options = NULL)
  1503. {
  1504. $this->precheckCommon($bucket, $object, $options);
  1505. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1506. $options[self::OSS_BUCKET] = $bucket;
  1507. $options[self::OSS_OBJECT] = $object;
  1508. $options[self::OSS_UPLOAD_ID] = $uploadId;
  1509. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  1510. if (!is_array($listParts)) {
  1511. throw new OssException("listParts must be array type");
  1512. }
  1513. $options[self::OSS_CONTENT] = OssUtil::createCompleteMultipartUploadXmlBody($listParts);
  1514. $response = $this->auth($options);
  1515. if (isset($options[self::OSS_CALLBACK]) && !empty($options[self::OSS_CALLBACK])) {
  1516. $result = new CallbackResult($response);
  1517. } else {
  1518. $result = new PutSetDeleteResult($response);
  1519. }
  1520. return $result->getData();
  1521. }
  1522. /**
  1523. * Lists all ongoing multipart upload events, which means all initialized but not completed or aborted multipart uploads.
  1524. *
  1525. * @param string $bucket bucket
  1526. * @param array $options key-value array--expected keys are 'delimiter', 'key-marker', 'max-uploads', 'prefix', 'upload-id-marker'
  1527. * @throws OssException
  1528. * @return ListMultipartUploadInfo
  1529. */
  1530. public function listMultipartUploads($bucket, $options = null)
  1531. {
  1532. $this->precheckCommon($bucket, NULL, $options, false);
  1533. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1534. $options[self::OSS_BUCKET] = $bucket;
  1535. $options[self::OSS_OBJECT] = '/';
  1536. $options[self::OSS_SUB_RESOURCE] = 'uploads';
  1537. foreach (array('delimiter', 'key-marker', 'max-uploads', 'prefix', 'upload-id-marker') as $param) {
  1538. if (isset($options[$param])) {
  1539. $options[self::OSS_QUERY_STRING][$param] = $options[$param];
  1540. unset($options[$param]);
  1541. }
  1542. }
  1543. $query = isset($options[self::OSS_QUERY_STRING]) ? $options[self::OSS_QUERY_STRING] : array();
  1544. $options[self::OSS_QUERY_STRING] = array_merge(
  1545. $query,
  1546. array(self::OSS_ENCODING_TYPE => self::OSS_ENCODING_TYPE_URL)
  1547. );
  1548. $response = $this->auth($options);
  1549. $result = new ListMultipartUploadResult($response);
  1550. return $result->getData();
  1551. }
  1552. /**
  1553. * Copy an existing file as a part
  1554. *
  1555. * @param string $fromBucket source bucket name
  1556. * @param string $fromObject source object name
  1557. * @param string $toBucket target bucket name
  1558. * @param string $toObject target object name
  1559. * @param int $partNumber Part number
  1560. * @param string $uploadId Upload Id
  1561. * @param array $options Key-Value array---it should have 'start' or 'end' key to specify the range of the source object to copy. If it's not specifed, the whole object is copied.
  1562. * @return null
  1563. * @throws OssException
  1564. */
  1565. public function uploadPartCopy($fromBucket, $fromObject, $toBucket, $toObject, $partNumber, $uploadId, $options = NULL)
  1566. {
  1567. $this->precheckCommon($fromBucket, $fromObject, $options);
  1568. $this->precheckCommon($toBucket, $toObject, $options);
  1569. //If $options['isFullCopy'] is not set, copy from the beginning
  1570. $start_range = "0";
  1571. if (isset($options['start'])) {
  1572. $start_range = $options['start'];
  1573. }
  1574. $end_range = "";
  1575. if (isset($options['end'])) {
  1576. $end_range = $options['end'];
  1577. }
  1578. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1579. $options[self::OSS_BUCKET] = $toBucket;
  1580. $options[self::OSS_OBJECT] = $toObject;
  1581. $options[self::OSS_PART_NUM] = $partNumber;
  1582. $options[self::OSS_UPLOAD_ID] = $uploadId;
  1583. if (!isset($options[self::OSS_HEADERS])) {
  1584. $options[self::OSS_HEADERS] = array();
  1585. }
  1586. $options[self::OSS_HEADERS][self::OSS_OBJECT_COPY_SOURCE] = '/' . $fromBucket . '/' . $fromObject;
  1587. $options[self::OSS_HEADERS][self::OSS_OBJECT_COPY_SOURCE_RANGE] = "bytes=" . $start_range . "-" . $end_range;
  1588. $response = $this->auth($options);
  1589. $result = new UploadPartResult($response);
  1590. return $result->getData();
  1591. }
  1592. /**
  1593. * A higher level API for uploading a file with multipart upload. It consists of initialization, parts upload and completion.
  1594. *
  1595. * @param string $bucket bucket name
  1596. * @param string $object object name
  1597. * @param string $file The local file to upload
  1598. * @param array $options Key-Value array
  1599. * @return null
  1600. * @throws OssException
  1601. */
  1602. public function multiuploadFile($bucket, $object, $file, $options = null)
  1603. {
  1604. $this->precheckCommon($bucket, $object, $options);
  1605. if (isset($options[self::OSS_LENGTH])) {
  1606. $options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH];
  1607. unset($options[self::OSS_LENGTH]);
  1608. }
  1609. if (empty($file)) {
  1610. throw new OssException("parameter invalid, file is empty");
  1611. }
  1612. $uploadFile = OssUtil::encodePath($file);
  1613. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  1614. $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object, $uploadFile);
  1615. }
  1616. $upload_position = isset($options[self::OSS_SEEK_TO]) ? (integer)$options[self::OSS_SEEK_TO] : 0;
  1617. if (isset($options[self::OSS_CONTENT_LENGTH])) {
  1618. $upload_file_size = (integer)$options[self::OSS_CONTENT_LENGTH];
  1619. } else {
  1620. $upload_file_size = filesize($uploadFile);
  1621. if ($upload_file_size !== false) {
  1622. $upload_file_size -= $upload_position;
  1623. }
  1624. }
  1625. if ($upload_position === false || !isset($upload_file_size) || $upload_file_size === false || $upload_file_size < 0) {
  1626. throw new OssException('The size of `fileUpload` cannot be determined in ' . __FUNCTION__ . '().');
  1627. }
  1628. // Computes the part size and assign it to options.
  1629. if (isset($options[self::OSS_PART_SIZE])) {
  1630. $options[self::OSS_PART_SIZE] = $this->computePartSize($options[self::OSS_PART_SIZE]);
  1631. } else {
  1632. $options[self::OSS_PART_SIZE] = self::OSS_MID_PART_SIZE;
  1633. }
  1634. $is_check_md5 = $this->isCheckMD5($options);
  1635. // if the file size is less than part size, use simple file upload.
  1636. if ($upload_file_size < $options[self::OSS_PART_SIZE] && !isset($options[self::OSS_UPLOAD_ID])) {
  1637. return $this->uploadFile($bucket, $object, $uploadFile, $options);
  1638. }
  1639. // Using multipart upload, initialize if no OSS_UPLOAD_ID is specified in options.
  1640. if (isset($options[self::OSS_UPLOAD_ID])) {
  1641. $uploadId = $options[self::OSS_UPLOAD_ID];
  1642. } else {
  1643. // initialize
  1644. $uploadId = $this->initiateMultipartUpload($bucket, $object, $options);
  1645. }
  1646. // generates the parts information and upload them one by one
  1647. $pieces = $this->generateMultiuploadParts($upload_file_size, (integer)$options[self::OSS_PART_SIZE]);
  1648. $response_upload_part = array();
  1649. foreach ($pieces as $i => $piece) {
  1650. $from_pos = $upload_position + (integer)$piece[self::OSS_SEEK_TO];
  1651. $to_pos = (integer)$piece[self::OSS_LENGTH] + $from_pos - 1;
  1652. $up_options = array(
  1653. self::OSS_FILE_UPLOAD => $uploadFile,
  1654. self::OSS_PART_NUM => ($i + 1),
  1655. self::OSS_SEEK_TO => $from_pos,
  1656. self::OSS_LENGTH => $to_pos - $from_pos + 1,
  1657. self::OSS_CHECK_MD5 => $is_check_md5,
  1658. );
  1659. if ($is_check_md5) {
  1660. $content_md5 = OssUtil::getMd5SumForFile($uploadFile, $from_pos, $to_pos);
  1661. $up_options[self::OSS_CONTENT_MD5] = $content_md5;
  1662. }
  1663. $response_upload_part[] = $this->uploadPart($bucket, $object, $uploadId, $up_options);
  1664. }
  1665. $uploadParts = array();
  1666. foreach ($response_upload_part as $i => $etag) {
  1667. $uploadParts[] = array(
  1668. 'PartNumber' => ($i + 1),
  1669. 'ETag' => $etag,
  1670. );
  1671. }
  1672. return $this->completeMultipartUpload($bucket, $object, $uploadId, $uploadParts);
  1673. }
  1674. /**
  1675. * Uploads the local directory to the specified bucket into specified folder (prefix)
  1676. *
  1677. * @param string $bucket bucket name
  1678. * @param string $prefix The object key prefix. Typically it's folder name. The name should not end with '/' as the API appends it automatically.
  1679. * @param string $localDirectory The local directory to upload
  1680. * @param string $exclude To excluded directories
  1681. * @param bool $recursive Recursive flag. True: Recursively upload all datas under the local directory; False: only upload first layer's files.
  1682. * @param bool $checkMd5
  1683. * @return array Returns two list: array("succeededList" => array("object"), "failedList" => array("object"=>"errorMessage"))
  1684. * @throws OssException
  1685. */
  1686. public function uploadDir($bucket, $prefix, $localDirectory, $exclude = '.|..|.svn|.git', $recursive = false, $checkMd5 = true)
  1687. {
  1688. $retArray = array("succeededList" => array(), "failedList" => array());
  1689. if (empty($bucket)) throw new OssException("parameter error, bucket is empty");
  1690. if (!is_string($prefix)) throw new OssException("parameter error, prefix is not string");
  1691. if (empty($localDirectory)) throw new OssException("parameter error, localDirectory is empty");
  1692. $directory = $localDirectory;
  1693. $directory = OssUtil::encodePath($directory);
  1694. //If it's not the local directory, throw OSSException.
  1695. if (!is_dir($directory)) {
  1696. throw new OssException('parameter error: ' . $directory . ' is not a directory, please check it');
  1697. }
  1698. //read directory
  1699. $file_list_array = OssUtil::readDir($directory, $exclude, $recursive);
  1700. if (!$file_list_array) {
  1701. throw new OssException($directory . ' is empty...');
  1702. }
  1703. foreach ($file_list_array as $k => $item) {
  1704. if (is_dir($item['path'])) {
  1705. continue;
  1706. }
  1707. $options = array(
  1708. self::OSS_PART_SIZE => self::OSS_MIN_PART_SIZE,
  1709. self::OSS_CHECK_MD5 => $checkMd5,
  1710. );
  1711. $realObject = (!empty($prefix) ? $prefix . '/' : '') . $item['file'];
  1712. try {
  1713. $this->multiuploadFile($bucket, $realObject, $item['path'], $options);
  1714. $retArray["succeededList"][] = $realObject;
  1715. } catch (OssException $e) {
  1716. $retArray["failedList"][$realObject] = $e->getMessage();
  1717. }
  1718. }
  1719. return $retArray;
  1720. }
  1721. /**
  1722. * Sign URL with specified expiration time in seconds (timeout) and HTTP method.
  1723. * The signed URL could be used to access the object directly.
  1724. *
  1725. * @param string $bucket
  1726. * @param string $object
  1727. * @param int $timeout expiration time in seconds.
  1728. * @param string $method
  1729. * @param array $options Key-Value array
  1730. * @return string
  1731. * @throws OssException
  1732. */
  1733. public function signUrl($bucket, $object, $timeout = 60, $method = self::OSS_HTTP_GET, $options = NULL)
  1734. {
  1735. $this->precheckCommon($bucket, $object, $options);
  1736. //method
  1737. if (self::OSS_HTTP_GET !== $method && self::OSS_HTTP_PUT !== $method) {
  1738. throw new OssException("method is invalid");
  1739. }
  1740. $options[self::OSS_BUCKET] = $bucket;
  1741. $options[self::OSS_OBJECT] = $object;
  1742. $options[self::OSS_METHOD] = $method;
  1743. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  1744. $options[self::OSS_CONTENT_TYPE] = '';
  1745. }
  1746. $timeout = time() + $timeout;
  1747. $options[self::OSS_PREAUTH] = $timeout;
  1748. $options[self::OSS_DATE] = $timeout;
  1749. $this->setSignStsInUrl(true);
  1750. return $this->auth($options);
  1751. }
  1752. /**
  1753. * validates options. Create a empty array if it's NULL.
  1754. *
  1755. * @param array $options
  1756. * @throws OssException
  1757. */
  1758. private function precheckOptions(&$options)
  1759. {
  1760. OssUtil::validateOptions($options);
  1761. if (!$options) {
  1762. $options = array();
  1763. }
  1764. }
  1765. /**
  1766. * Validates bucket parameter
  1767. *
  1768. * @param string $bucket
  1769. * @param string $errMsg
  1770. * @throws OssException
  1771. */
  1772. private function precheckBucket($bucket, $errMsg = 'bucket is not allowed empty')
  1773. {
  1774. OssUtil::throwOssExceptionWithMessageIfEmpty($bucket, $errMsg);
  1775. }
  1776. /**
  1777. * validates object parameter
  1778. *
  1779. * @param string $object
  1780. * @throws OssException
  1781. */
  1782. private function precheckObject($object)
  1783. {
  1784. OssUtil::throwOssExceptionWithMessageIfEmpty($object, "object name is empty");
  1785. }
  1786. /**
  1787. * 校验option restore
  1788. *
  1789. * @param string $restore
  1790. * @throws OssException
  1791. */
  1792. private function precheckStorage($storage)
  1793. {
  1794. if (is_string($storage)) {
  1795. switch ($storage) {
  1796. case self::OSS_STORAGE_ARCHIVE:
  1797. return;
  1798. case self::OSS_STORAGE_IA:
  1799. return;
  1800. case self::OSS_STORAGE_STANDARD:
  1801. return;
  1802. default:
  1803. break;
  1804. }
  1805. }
  1806. throw new OssException('storage name is invalid');
  1807. }
  1808. /**
  1809. * Validates bucket,options parameters and optionally validate object parameter.
  1810. *
  1811. * @param string $bucket
  1812. * @param string $object
  1813. * @param array $options
  1814. * @param bool $isCheckObject
  1815. */
  1816. private function precheckCommon($bucket, $object, &$options, $isCheckObject = true)
  1817. {
  1818. if ($isCheckObject) {
  1819. $this->precheckObject($object);
  1820. }
  1821. $this->precheckOptions($options);
  1822. $this->precheckBucket($bucket);
  1823. }
  1824. /**
  1825. * checks parameters
  1826. *
  1827. * @param array $options
  1828. * @param string $param
  1829. * @param string $funcName
  1830. * @throws OssException
  1831. */
  1832. private function precheckParam($options, $param, $funcName)
  1833. {
  1834. if (!isset($options[$param])) {
  1835. throw new OssException('The `' . $param . '` options is required in ' . $funcName . '().');
  1836. }
  1837. }
  1838. /**
  1839. * Checks md5
  1840. *
  1841. * @param array $options
  1842. * @return bool|null
  1843. */
  1844. private function isCheckMD5($options)
  1845. {
  1846. return $this->getValue($options, self::OSS_CHECK_MD5, false, true, true);
  1847. }
  1848. /**
  1849. * Gets value of the specified key from the options
  1850. *
  1851. * @param array $options
  1852. * @param string $key
  1853. * @param string $default
  1854. * @param bool $isCheckEmpty
  1855. * @param bool $isCheckBool
  1856. * @return bool|null
  1857. */
  1858. private function getValue($options, $key, $default = NULL, $isCheckEmpty = false, $isCheckBool = false)
  1859. {
  1860. $value = $default;
  1861. if (isset($options[$key])) {
  1862. if ($isCheckEmpty) {
  1863. if (!empty($options[$key])) {
  1864. $value = $options[$key];
  1865. }
  1866. } else {
  1867. $value = $options[$key];
  1868. }
  1869. unset($options[$key]);
  1870. }
  1871. if ($isCheckBool) {
  1872. if ($value !== true && $value !== false) {
  1873. $value = false;
  1874. }
  1875. }
  1876. return $value;
  1877. }
  1878. /**
  1879. * Gets mimetype
  1880. *
  1881. * @param string $object
  1882. * @return string
  1883. */
  1884. private function getMimeType($object, $file = null)
  1885. {
  1886. if (!is_null($file)) {
  1887. $type = MimeTypes::getMimetype($file);
  1888. if (!is_null($type)) {
  1889. return $type;
  1890. }
  1891. }
  1892. $type = MimeTypes::getMimetype($object);
  1893. if (!is_null($type)) {
  1894. return $type;
  1895. }
  1896. return self::DEFAULT_CONTENT_TYPE;
  1897. }
  1898. /**
  1899. * Validates and executes the request according to OSS API protocol.
  1900. *
  1901. * @param array $options
  1902. * @return ResponseCore
  1903. * @throws OssException
  1904. * @throws RequestCore_Exception
  1905. */
  1906. private function auth($options)
  1907. {
  1908. OssUtil::validateOptions($options);
  1909. //Validates bucket, not required for list_bucket
  1910. $this->authPrecheckBucket($options);
  1911. //Validates object
  1912. $this->authPrecheckObject($options);
  1913. //object name encoding must be UTF-8
  1914. $this->authPrecheckObjectEncoding($options);
  1915. //Validates ACL
  1916. $this->authPrecheckAcl($options);
  1917. // Should https or http be used?
  1918. $scheme = $this->useSSL ? 'https://' : 'http://';
  1919. // gets the host name. If the host name is public domain or private domain, form a third level domain by prefixing the bucket name on the domain name.
  1920. $hostname = $this->generateHostname($options[self::OSS_BUCKET]);
  1921. $string_to_sign = '';
  1922. $headers = $this->generateHeaders($options, $hostname);
  1923. $signable_query_string_params = $this->generateSignableQueryStringParam($options);
  1924. $signable_query_string = OssUtil::toQueryString($signable_query_string_params);
  1925. $resource_uri = $this->generateResourceUri($options);
  1926. //Generates the URL (add query parameters)
  1927. $conjunction = '?';
  1928. $non_signable_resource = '';
  1929. if (isset($options[self::OSS_SUB_RESOURCE])) {
  1930. $conjunction = '&';
  1931. }
  1932. if ($signable_query_string !== '') {
  1933. $signable_query_string = $conjunction . $signable_query_string;
  1934. $conjunction = '&';
  1935. }
  1936. $query_string = $this->generateQueryString($options);
  1937. if ($query_string !== '') {
  1938. $non_signable_resource .= $conjunction . $query_string;
  1939. $conjunction = '&';
  1940. }
  1941. $this->requestUrl = $scheme . $hostname . $resource_uri . $signable_query_string . $non_signable_resource;
  1942. //Creates the request
  1943. $request = new RequestCore($this->requestUrl, $this->requestProxy);
  1944. $request->set_useragent($this->generateUserAgent());
  1945. // Streaming uploads
  1946. if (isset($options[self::OSS_FILE_UPLOAD])) {
  1947. if (is_resource($options[self::OSS_FILE_UPLOAD])) {
  1948. $length = null;
  1949. if (isset($options[self::OSS_CONTENT_LENGTH])) {
  1950. $length = $options[self::OSS_CONTENT_LENGTH];
  1951. } elseif (isset($options[self::OSS_SEEK_TO])) {
  1952. $stats = fstat($options[self::OSS_FILE_UPLOAD]);
  1953. if ($stats && $stats[self::OSS_SIZE] >= 0) {
  1954. $length = $stats[self::OSS_SIZE] - (integer)$options[self::OSS_SEEK_TO];
  1955. }
  1956. }
  1957. $request->set_read_stream($options[self::OSS_FILE_UPLOAD], $length);
  1958. } else {
  1959. $request->set_read_file($options[self::OSS_FILE_UPLOAD]);
  1960. $length = $request->read_stream_size;
  1961. if (isset($options[self::OSS_CONTENT_LENGTH])) {
  1962. $length = $options[self::OSS_CONTENT_LENGTH];
  1963. } elseif (isset($options[self::OSS_SEEK_TO]) && isset($length)) {
  1964. $length -= (integer)$options[self::OSS_SEEK_TO];
  1965. }
  1966. $request->set_read_stream_size($length);
  1967. }
  1968. }
  1969. if (isset($options[self::OSS_SEEK_TO])) {
  1970. $request->set_seek_position((integer)$options[self::OSS_SEEK_TO]);
  1971. }
  1972. if (isset($options[self::OSS_FILE_DOWNLOAD])) {
  1973. if (is_resource($options[self::OSS_FILE_DOWNLOAD])) {
  1974. $request->set_write_stream($options[self::OSS_FILE_DOWNLOAD]);
  1975. } else {
  1976. $request->set_write_file($options[self::OSS_FILE_DOWNLOAD]);
  1977. }
  1978. }
  1979. if (isset($options[self::OSS_METHOD])) {
  1980. $request->set_method($options[self::OSS_METHOD]);
  1981. $string_to_sign .= $options[self::OSS_METHOD] . "\n";
  1982. }
  1983. if (isset($options[self::OSS_CONTENT])) {
  1984. $request->set_body($options[self::OSS_CONTENT]);
  1985. if ($headers[self::OSS_CONTENT_TYPE] === 'application/x-www-form-urlencoded') {
  1986. $headers[self::OSS_CONTENT_TYPE] = 'application/octet-stream';
  1987. }
  1988. $headers[self::OSS_CONTENT_LENGTH] = strlen($options[self::OSS_CONTENT]);
  1989. $headers[self::OSS_CONTENT_MD5] = base64_encode(md5($options[self::OSS_CONTENT], true));
  1990. }
  1991. if (isset($options[self::OSS_CALLBACK])) {
  1992. $headers[self::OSS_CALLBACK] = base64_encode($options[self::OSS_CALLBACK]);
  1993. }
  1994. if (isset($options[self::OSS_CALLBACK_VAR])) {
  1995. $headers[self::OSS_CALLBACK_VAR] = base64_encode($options[self::OSS_CALLBACK_VAR]);
  1996. }
  1997. if (!isset($headers[self::OSS_ACCEPT_ENCODING])) {
  1998. $headers[self::OSS_ACCEPT_ENCODING] = '';
  1999. }
  2000. uksort($headers, 'strnatcasecmp');
  2001. foreach ($headers as $header_key => $header_value) {
  2002. $header_value = str_replace(array("\r", "\n"), '', $header_value);
  2003. if ($header_value !== '' || $header_key === self::OSS_ACCEPT_ENCODING) {
  2004. $request->add_header($header_key, $header_value);
  2005. }
  2006. if (
  2007. strtolower($header_key) === 'content-md5' ||
  2008. strtolower($header_key) === 'content-type' ||
  2009. strtolower($header_key) === 'date' ||
  2010. (isset($options['self::OSS_PREAUTH']) && (integer)$options['self::OSS_PREAUTH'] > 0)
  2011. ) {
  2012. $string_to_sign .= $header_value . "\n";
  2013. } elseif (substr(strtolower($header_key), 0, 6) === self::OSS_DEFAULT_PREFIX) {
  2014. $string_to_sign .= strtolower($header_key) . ':' . $header_value . "\n";
  2015. }
  2016. }
  2017. // Generates the signable_resource
  2018. $signable_resource = $this->generateSignableResource($options);
  2019. $string_to_sign .= rawurldecode($signable_resource) . urldecode($signable_query_string);
  2020. // Sort the strings to be signed.
  2021. $string_to_sign_ordered = $this->stringToSignSorted($string_to_sign);
  2022. $signature = base64_encode(hash_hmac('sha1', $string_to_sign_ordered, $this->accessKeySecret, true));
  2023. $request->add_header('Authorization', 'OSS ' . $this->accessKeyId . ':' . $signature);
  2024. if (isset($options[self::OSS_PREAUTH]) && (integer)$options[self::OSS_PREAUTH] > 0) {
  2025. $signed_url = $this->requestUrl . $conjunction . self::OSS_URL_ACCESS_KEY_ID . '=' . rawurlencode($this->accessKeyId) . '&' . self::OSS_URL_EXPIRES . '=' . $options[self::OSS_PREAUTH] . '&' . self::OSS_URL_SIGNATURE . '=' . rawurlencode($signature);
  2026. return $signed_url;
  2027. } elseif (isset($options[self::OSS_PREAUTH])) {
  2028. return $this->requestUrl;
  2029. }
  2030. if ($this->timeout !== 0) {
  2031. $request->timeout = $this->timeout;
  2032. }
  2033. if ($this->connectTimeout !== 0) {
  2034. $request->connect_timeout = $this->connectTimeout;
  2035. }
  2036. try {
  2037. $request->send_request();
  2038. } catch (RequestCore_Exception $e) {
  2039. throw(new OssException('RequestCoreException: ' . $e->getMessage()));
  2040. }
  2041. $response_header = $request->get_response_header();
  2042. $response_header['oss-request-url'] = $this->requestUrl;
  2043. $response_header['oss-redirects'] = $this->redirects;
  2044. $response_header['oss-stringtosign'] = $string_to_sign;
  2045. $response_header['oss-requestheaders'] = $request->request_headers;
  2046. $data = new ResponseCore($response_header, $request->get_response_body(), $request->get_response_code());
  2047. //retry if OSS Internal Error
  2048. if ((integer)$request->get_response_code() === 500) {
  2049. if ($this->redirects <= $this->maxRetries) {
  2050. //Sets the sleep time betwen each retry.
  2051. $delay = (integer)(pow(4, $this->redirects) * 100000);
  2052. usleep($delay);
  2053. $this->redirects++;
  2054. $data = $this->auth($options);
  2055. }
  2056. }
  2057. $this->redirects = 0;
  2058. return $data;
  2059. }
  2060. /**
  2061. * Sets the max retry count
  2062. *
  2063. * @param int $maxRetries
  2064. * @return void
  2065. */
  2066. public function setMaxTries($maxRetries = 3)
  2067. {
  2068. $this->maxRetries = $maxRetries;
  2069. }
  2070. /**
  2071. * Gets the max retry count
  2072. *
  2073. * @return int
  2074. */
  2075. public function getMaxRetries()
  2076. {
  2077. return $this->maxRetries;
  2078. }
  2079. /**
  2080. * Enaable/disable STS in the URL. This is to determine the $sts value passed from constructor take effect or not.
  2081. *
  2082. * @param boolean $enable
  2083. */
  2084. public function setSignStsInUrl($enable)
  2085. {
  2086. $this->enableStsInUrl = $enable;
  2087. }
  2088. /**
  2089. * @return boolean
  2090. */
  2091. public function isUseSSL()
  2092. {
  2093. return $this->useSSL;
  2094. }
  2095. /**
  2096. * @param boolean $useSSL
  2097. */
  2098. public function setUseSSL($useSSL)
  2099. {
  2100. $this->useSSL = $useSSL;
  2101. }
  2102. /**
  2103. * Validates bucket name--throw OssException if it's invalid
  2104. *
  2105. * @param $options
  2106. * @throws OssException
  2107. */
  2108. private function authPrecheckBucket($options)
  2109. {
  2110. if (!(('/' == $options[self::OSS_OBJECT]) && ('' == $options[self::OSS_BUCKET]) && ('GET' == $options[self::OSS_METHOD])) && !OssUtil::validateBucket($options[self::OSS_BUCKET])) {
  2111. throw new OssException('"' . $options[self::OSS_BUCKET] . '"' . 'bucket name is invalid');
  2112. }
  2113. }
  2114. /**
  2115. *
  2116. * Validates the object name--throw OssException if it's invalid.
  2117. *
  2118. * @param $options
  2119. * @throws OssException
  2120. */
  2121. private function authPrecheckObject($options)
  2122. {
  2123. if (isset($options[self::OSS_OBJECT]) && $options[self::OSS_OBJECT] === '/') {
  2124. return;
  2125. }
  2126. if (isset($options[self::OSS_OBJECT]) && !OssUtil::validateObject($options[self::OSS_OBJECT])) {
  2127. throw new OssException('"' . $options[self::OSS_OBJECT] . '"' . ' object name is invalid');
  2128. }
  2129. }
  2130. /**
  2131. * Checks the object's encoding. Convert it to UTF8 if it's in GBK or GB2312
  2132. *
  2133. * @param mixed $options parameter
  2134. */
  2135. private function authPrecheckObjectEncoding(&$options)
  2136. {
  2137. $tmp_object = $options[self::OSS_OBJECT];
  2138. try {
  2139. if (OssUtil::isGb2312($options[self::OSS_OBJECT])) {
  2140. $options[self::OSS_OBJECT] = iconv('GB2312', "UTF-8//IGNORE", $options[self::OSS_OBJECT]);
  2141. } elseif (OssUtil::checkChar($options[self::OSS_OBJECT], true)) {
  2142. $options[self::OSS_OBJECT] = iconv('GBK', "UTF-8//IGNORE", $options[self::OSS_OBJECT]);
  2143. }
  2144. } catch (\Exception $e) {
  2145. try {
  2146. $tmp_object = iconv(mb_detect_encoding($tmp_object), "UTF-8", $tmp_object);
  2147. } catch (\Exception $e) {
  2148. }
  2149. }
  2150. $options[self::OSS_OBJECT] = $tmp_object;
  2151. }
  2152. /**
  2153. * Checks if the ACL is one of the 3 predefined one. Throw OSSException if not.
  2154. *
  2155. * @param $options
  2156. * @throws OssException
  2157. */
  2158. private function authPrecheckAcl($options)
  2159. {
  2160. if (isset($options[self::OSS_HEADERS][self::OSS_ACL]) && !empty($options[self::OSS_HEADERS][self::OSS_ACL])) {
  2161. if (!in_array(strtolower($options[self::OSS_HEADERS][self::OSS_ACL]), self::$OSS_ACL_TYPES)) {
  2162. throw new OssException($options[self::OSS_HEADERS][self::OSS_ACL] . ':' . 'acl is invalid(private,public-read,public-read-write)');
  2163. }
  2164. }
  2165. }
  2166. /**
  2167. * Gets the host name for the current request.
  2168. * It could be either a third level domain (prefixed by bucket name) or second level domain if it's CName or IP
  2169. *
  2170. * @param $bucket
  2171. * @return string The host name without the protocol scheem (e.g. https://)
  2172. */
  2173. private function generateHostname($bucket)
  2174. {
  2175. if ($this->hostType === self::OSS_HOST_TYPE_IP) {
  2176. $hostname = $this->hostname;
  2177. } elseif ($this->hostType === self::OSS_HOST_TYPE_CNAME) {
  2178. $hostname = $this->hostname;
  2179. } else {
  2180. // Private domain or public domain
  2181. $hostname = ($bucket == '') ? $this->hostname : ($bucket . '.') . $this->hostname;
  2182. }
  2183. return $hostname;
  2184. }
  2185. /**
  2186. * Gets the resource Uri in the current request
  2187. *
  2188. * @param $options
  2189. * @return string return the resource uri.
  2190. */
  2191. private function generateResourceUri($options)
  2192. {
  2193. $resource_uri = "";
  2194. // resource_uri + bucket
  2195. if (isset($options[self::OSS_BUCKET]) && '' !== $options[self::OSS_BUCKET]) {
  2196. if ($this->hostType === self::OSS_HOST_TYPE_IP) {
  2197. $resource_uri = '/' . $options[self::OSS_BUCKET];
  2198. }
  2199. }
  2200. // resource_uri + object
  2201. if (isset($options[self::OSS_OBJECT]) && '/' !== $options[self::OSS_OBJECT]) {
  2202. $resource_uri .= '/' . str_replace(array('%2F', '%25'), array('/', '%'), rawurlencode($options[self::OSS_OBJECT]));
  2203. }
  2204. // resource_uri + sub_resource
  2205. $conjunction = '?';
  2206. if (isset($options[self::OSS_SUB_RESOURCE])) {
  2207. $resource_uri .= $conjunction . $options[self::OSS_SUB_RESOURCE];
  2208. }
  2209. return $resource_uri;
  2210. }
  2211. /**
  2212. * Generates the signalbe query string parameters in array type
  2213. *
  2214. * @param array $options
  2215. * @return array
  2216. */
  2217. private function generateSignableQueryStringParam($options)
  2218. {
  2219. $signableQueryStringParams = array();
  2220. $signableList = array(
  2221. self::OSS_PART_NUM,
  2222. 'response-content-type',
  2223. 'response-content-language',
  2224. 'response-cache-control',
  2225. 'response-content-encoding',
  2226. 'response-expires',
  2227. 'response-content-disposition',
  2228. self::OSS_UPLOAD_ID,
  2229. self::OSS_COMP,
  2230. self::OSS_LIVE_CHANNEL_STATUS,
  2231. self::OSS_LIVE_CHANNEL_START_TIME,
  2232. self::OSS_LIVE_CHANNEL_END_TIME,
  2233. self::OSS_PROCESS,
  2234. self::OSS_POSITION,
  2235. self::OSS_SYMLINK,
  2236. self::OSS_RESTORE,
  2237. );
  2238. foreach ($signableList as $item) {
  2239. if (isset($options[$item])) {
  2240. $signableQueryStringParams[$item] = $options[$item];
  2241. }
  2242. }
  2243. if ($this->enableStsInUrl && (!is_null($this->securityToken))) {
  2244. $signableQueryStringParams["security-token"] = $this->securityToken;
  2245. }
  2246. return $signableQueryStringParams;
  2247. }
  2248. /**
  2249. * Generates the resource uri for signing
  2250. *
  2251. * @param mixed $options
  2252. * @return string
  2253. */
  2254. private function generateSignableResource($options)
  2255. {
  2256. $signableResource = "";
  2257. $signableResource .= '/';
  2258. if (isset($options[self::OSS_BUCKET]) && '' !== $options[self::OSS_BUCKET]) {
  2259. $signableResource .= $options[self::OSS_BUCKET];
  2260. // if there's no object in options, adding a '/' if the host type is not IP.\
  2261. if ($options[self::OSS_OBJECT] == '/') {
  2262. if ($this->hostType !== self::OSS_HOST_TYPE_IP) {
  2263. $signableResource .= "/";
  2264. }
  2265. }
  2266. }
  2267. //signable_resource + object
  2268. if (isset($options[self::OSS_OBJECT]) && '/' !== $options[self::OSS_OBJECT]) {
  2269. $signableResource .= '/' . str_replace(array('%2F', '%25'), array('/', '%'), rawurlencode($options[self::OSS_OBJECT]));
  2270. }
  2271. if (isset($options[self::OSS_SUB_RESOURCE])) {
  2272. $signableResource .= '?' . $options[self::OSS_SUB_RESOURCE];
  2273. }
  2274. return $signableResource;
  2275. }
  2276. /**
  2277. * generates query string
  2278. *
  2279. * @param mixed $options
  2280. * @return string
  2281. */
  2282. private function generateQueryString($options)
  2283. {
  2284. //query parameters
  2285. $queryStringParams = array();
  2286. if (isset($options[self::OSS_QUERY_STRING])) {
  2287. $queryStringParams = array_merge($queryStringParams, $options[self::OSS_QUERY_STRING]);
  2288. }
  2289. return OssUtil::toQueryString($queryStringParams);
  2290. }
  2291. private function stringToSignSorted($string_to_sign)
  2292. {
  2293. $queryStringSorted = '';
  2294. $explodeResult = explode('?', $string_to_sign);
  2295. $index = count($explodeResult);
  2296. if ($index === 1)
  2297. return $string_to_sign;
  2298. $queryStringParams = explode('&', $explodeResult[$index - 1]);
  2299. sort($queryStringParams);
  2300. foreach($queryStringParams as $params)
  2301. {
  2302. $queryStringSorted .= $params . '&';
  2303. }
  2304. $queryStringSorted = substr($queryStringSorted, 0, -1);
  2305. return $explodeResult[0] . '?' . $queryStringSorted;
  2306. }
  2307. /**
  2308. * Initialize headers
  2309. *
  2310. * @param mixed $options
  2311. * @param string $hostname hostname
  2312. * @return array
  2313. */
  2314. private function generateHeaders($options, $hostname)
  2315. {
  2316. $headers = array(
  2317. self::OSS_CONTENT_MD5 => '',
  2318. self::OSS_CONTENT_TYPE => isset($options[self::OSS_CONTENT_TYPE]) ? $options[self::OSS_CONTENT_TYPE] : self::DEFAULT_CONTENT_TYPE,
  2319. self::OSS_DATE => isset($options[self::OSS_DATE]) ? $options[self::OSS_DATE] : gmdate('D, d M Y H:i:s \G\M\T'),
  2320. self::OSS_HOST => $hostname,
  2321. );
  2322. if (isset($options[self::OSS_CONTENT_MD5])) {
  2323. $headers[self::OSS_CONTENT_MD5] = $options[self::OSS_CONTENT_MD5];
  2324. }
  2325. //Add stsSecurityToken
  2326. if ((!is_null($this->securityToken)) && (!$this->enableStsInUrl)) {
  2327. $headers[self::OSS_SECURITY_TOKEN] = $this->securityToken;
  2328. }
  2329. //Merge HTTP headers
  2330. if (isset($options[self::OSS_HEADERS])) {
  2331. $headers = array_merge($headers, $options[self::OSS_HEADERS]);
  2332. }
  2333. return $headers;
  2334. }
  2335. /**
  2336. * Generates UserAgent
  2337. *
  2338. * @return string
  2339. */
  2340. private function generateUserAgent()
  2341. {
  2342. return self::OSS_NAME . "/" . self::OSS_VERSION . " (" . php_uname('s') . "/" . php_uname('r') . "/" . php_uname('m') . ";" . PHP_VERSION . ")";
  2343. }
  2344. /**
  2345. * Checks endpoint type and returns the endpoint without the protocol schema.
  2346. * Figures out the domain's type (ip, cname or private/public domain).
  2347. *
  2348. * @param string $endpoint
  2349. * @param boolean $isCName
  2350. * @return string The domain name without the protocol schema.
  2351. */
  2352. private function checkEndpoint($endpoint, $isCName)
  2353. {
  2354. $ret_endpoint = null;
  2355. if (strpos($endpoint, 'http://') === 0) {
  2356. $ret_endpoint = substr($endpoint, strlen('http://'));
  2357. } elseif (strpos($endpoint, 'https://') === 0) {
  2358. $ret_endpoint = substr($endpoint, strlen('https://'));
  2359. $this->useSSL = true;
  2360. } else {
  2361. $ret_endpoint = $endpoint;
  2362. }
  2363. if ($isCName) {
  2364. $this->hostType = self::OSS_HOST_TYPE_CNAME;
  2365. } elseif (OssUtil::isIPFormat($ret_endpoint)) {
  2366. $this->hostType = self::OSS_HOST_TYPE_IP;
  2367. } else {
  2368. $this->hostType = self::OSS_HOST_TYPE_NORMAL;
  2369. }
  2370. return $ret_endpoint;
  2371. }
  2372. /**
  2373. * Check if all dependent extensions are installed correctly.
  2374. * For now only "curl" is needed.
  2375. * @throws OssException
  2376. */
  2377. public static function checkEnv()
  2378. {
  2379. if (function_exists('get_loaded_extensions')) {
  2380. //Test curl extension
  2381. $enabled_extension = array("curl");
  2382. $extensions = get_loaded_extensions();
  2383. if ($extensions) {
  2384. foreach ($enabled_extension as $item) {
  2385. if (!in_array($item, $extensions)) {
  2386. throw new OssException("Extension {" . $item . "} is not installed or not enabled, please check your php env.");
  2387. }
  2388. }
  2389. } else {
  2390. throw new OssException("function get_loaded_extensions not found.");
  2391. }
  2392. } else {
  2393. throw new OssException('Function get_loaded_extensions has been disabled, please check php config.');
  2394. }
  2395. }
  2396. /**
  2397. * Sets the http's timeout (in seconds)
  2398. *
  2399. * @param int $timeout
  2400. */
  2401. public function setTimeout($timeout)
  2402. {
  2403. $this->timeout = $timeout;
  2404. }
  2405. /**
  2406. * Sets the http's connection timeout (in seconds)
  2407. *
  2408. * @param int $connectTimeout
  2409. */
  2410. public function setConnectTimeout($connectTimeout)
  2411. {
  2412. $this->connectTimeout = $connectTimeout;
  2413. }
  2414. // Constants for Life cycle
  2415. const OSS_LIFECYCLE_EXPIRATION = "Expiration";
  2416. const OSS_LIFECYCLE_TIMING_DAYS = "Days";
  2417. const OSS_LIFECYCLE_TIMING_DATE = "Date";
  2418. //OSS Internal constants
  2419. const OSS_BUCKET = 'bucket';
  2420. const OSS_OBJECT = 'object';
  2421. const OSS_HEADERS = OssUtil::OSS_HEADERS;
  2422. const OSS_METHOD = 'method';
  2423. const OSS_QUERY = 'query';
  2424. const OSS_BASENAME = 'basename';
  2425. const OSS_MAX_KEYS = 'max-keys';
  2426. const OSS_UPLOAD_ID = 'uploadId';
  2427. const OSS_PART_NUM = 'partNumber';
  2428. const OSS_COMP = 'comp';
  2429. const OSS_LIVE_CHANNEL_STATUS = 'status';
  2430. const OSS_LIVE_CHANNEL_START_TIME = 'startTime';
  2431. const OSS_LIVE_CHANNEL_END_TIME = 'endTime';
  2432. const OSS_POSITION = 'position';
  2433. const OSS_MAX_KEYS_VALUE = 100;
  2434. const OSS_MAX_OBJECT_GROUP_VALUE = OssUtil::OSS_MAX_OBJECT_GROUP_VALUE;
  2435. const OSS_MAX_PART_SIZE = OssUtil::OSS_MAX_PART_SIZE;
  2436. const OSS_MID_PART_SIZE = OssUtil::OSS_MID_PART_SIZE;
  2437. const OSS_MIN_PART_SIZE = OssUtil::OSS_MIN_PART_SIZE;
  2438. const OSS_FILE_SLICE_SIZE = 8192;
  2439. const OSS_PREFIX = 'prefix';
  2440. const OSS_DELIMITER = 'delimiter';
  2441. const OSS_MARKER = 'marker';
  2442. const OSS_ACCEPT_ENCODING = 'Accept-Encoding';
  2443. const OSS_CONTENT_MD5 = 'Content-Md5';
  2444. const OSS_SELF_CONTENT_MD5 = 'x-oss-meta-md5';
  2445. const OSS_CONTENT_TYPE = 'Content-Type';
  2446. const OSS_CONTENT_LENGTH = 'Content-Length';
  2447. const OSS_IF_MODIFIED_SINCE = 'If-Modified-Since';
  2448. const OSS_IF_UNMODIFIED_SINCE = 'If-Unmodified-Since';
  2449. const OSS_IF_MATCH = 'If-Match';
  2450. const OSS_IF_NONE_MATCH = 'If-None-Match';
  2451. const OSS_CACHE_CONTROL = 'Cache-Control';
  2452. const OSS_EXPIRES = 'Expires';
  2453. const OSS_PREAUTH = 'preauth';
  2454. const OSS_CONTENT_COING = 'Content-Coding';
  2455. const OSS_CONTENT_DISPOSTION = 'Content-Disposition';
  2456. const OSS_RANGE = 'range';
  2457. const OSS_ETAG = 'etag';
  2458. const OSS_LAST_MODIFIED = 'lastmodified';
  2459. const OS_CONTENT_RANGE = 'Content-Range';
  2460. const OSS_CONTENT = OssUtil::OSS_CONTENT;
  2461. const OSS_BODY = 'body';
  2462. const OSS_LENGTH = OssUtil::OSS_LENGTH;
  2463. const OSS_HOST = 'Host';
  2464. const OSS_DATE = 'Date';
  2465. const OSS_AUTHORIZATION = 'Authorization';
  2466. const OSS_FILE_DOWNLOAD = 'fileDownload';
  2467. const OSS_FILE_UPLOAD = 'fileUpload';
  2468. const OSS_PART_SIZE = 'partSize';
  2469. const OSS_SEEK_TO = 'seekTo';
  2470. const OSS_SIZE = 'size';
  2471. const OSS_QUERY_STRING = 'query_string';
  2472. const OSS_SUB_RESOURCE = 'sub_resource';
  2473. const OSS_DEFAULT_PREFIX = 'x-oss-';
  2474. const OSS_CHECK_MD5 = 'checkmd5';
  2475. const DEFAULT_CONTENT_TYPE = 'application/octet-stream';
  2476. const OSS_SYMLINK_TARGET = 'x-oss-symlink-target';
  2477. const OSS_SYMLINK = 'symlink';
  2478. const OSS_HTTP_CODE = 'http_code';
  2479. const OSS_REQUEST_ID = 'x-oss-request-id';
  2480. const OSS_INFO = 'info';
  2481. const OSS_STORAGE = 'storage';
  2482. const OSS_RESTORE = 'restore';
  2483. const OSS_STORAGE_STANDARD = 'Standard';
  2484. const OSS_STORAGE_IA = 'IA';
  2485. const OSS_STORAGE_ARCHIVE = 'Archive';
  2486. //private URLs
  2487. const OSS_URL_ACCESS_KEY_ID = 'OSSAccessKeyId';
  2488. const OSS_URL_EXPIRES = 'Expires';
  2489. const OSS_URL_SIGNATURE = 'Signature';
  2490. //HTTP METHOD
  2491. const OSS_HTTP_GET = 'GET';
  2492. const OSS_HTTP_PUT = 'PUT';
  2493. const OSS_HTTP_HEAD = 'HEAD';
  2494. const OSS_HTTP_POST = 'POST';
  2495. const OSS_HTTP_DELETE = 'DELETE';
  2496. const OSS_HTTP_OPTIONS = 'OPTIONS';
  2497. //Others
  2498. const OSS_ACL = 'x-oss-acl';
  2499. const OSS_OBJECT_ACL = 'x-oss-object-acl';
  2500. const OSS_OBJECT_GROUP = 'x-oss-file-group';
  2501. const OSS_MULTI_PART = 'uploads';
  2502. const OSS_MULTI_DELETE = 'delete';
  2503. const OSS_OBJECT_COPY_SOURCE = 'x-oss-copy-source';
  2504. const OSS_OBJECT_COPY_SOURCE_RANGE = "x-oss-copy-source-range";
  2505. const OSS_PROCESS = "x-oss-process";
  2506. const OSS_CALLBACK = "x-oss-callback";
  2507. const OSS_CALLBACK_VAR = "x-oss-callback-var";
  2508. //Constants for STS SecurityToken
  2509. const OSS_SECURITY_TOKEN = "x-oss-security-token";
  2510. const OSS_ACL_TYPE_PRIVATE = 'private';
  2511. const OSS_ACL_TYPE_PUBLIC_READ = 'public-read';
  2512. const OSS_ACL_TYPE_PUBLIC_READ_WRITE = 'public-read-write';
  2513. const OSS_ENCODING_TYPE = "encoding-type";
  2514. const OSS_ENCODING_TYPE_URL = "url";
  2515. // Domain Types
  2516. const OSS_HOST_TYPE_NORMAL = "normal";//http://bucket.oss-cn-hangzhou.aliyuncs.com/object
  2517. const OSS_HOST_TYPE_IP = "ip"; //http://1.1.1.1/bucket/object
  2518. const OSS_HOST_TYPE_SPECIAL = 'special'; //http://bucket.guizhou.gov/object
  2519. const OSS_HOST_TYPE_CNAME = "cname"; //http://mydomain.com/object
  2520. //OSS ACL array
  2521. static $OSS_ACL_TYPES = array(
  2522. self::OSS_ACL_TYPE_PRIVATE,
  2523. self::OSS_ACL_TYPE_PUBLIC_READ,
  2524. self::OSS_ACL_TYPE_PUBLIC_READ_WRITE
  2525. );
  2526. // OssClient version information
  2527. const OSS_NAME = "aliyun-sdk-php";
  2528. const OSS_VERSION = "2.3.0";
  2529. const OSS_BUILD = "20180105";
  2530. const OSS_AUTHOR = "";
  2531. const OSS_OPTIONS_ORIGIN = 'Origin';
  2532. const OSS_OPTIONS_REQUEST_METHOD = 'Access-Control-Request-Method';
  2533. const OSS_OPTIONS_REQUEST_HEADERS = 'Access-Control-Request-Headers';
  2534. //use ssl flag
  2535. private $useSSL = false;
  2536. private $maxRetries = 3;
  2537. private $redirects = 0;
  2538. // user's domain type. It could be one of the four: OSS_HOST_TYPE_NORMAL, OSS_HOST_TYPE_IP, OSS_HOST_TYPE_SPECIAL, OSS_HOST_TYPE_CNAME
  2539. private $hostType = self::OSS_HOST_TYPE_NORMAL;
  2540. private $requestUrl;
  2541. private $requestProxy = null;
  2542. private $accessKeyId;
  2543. private $accessKeySecret;
  2544. private $hostname;
  2545. private $securityToken;
  2546. private $enableStsInUrl = false;
  2547. private $timeout = 0;
  2548. private $connectTimeout = 0;
  2549. }