OssClient.php 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689
  1. <?php
  2. namespace OSS;
  3. use OSS\Core\MimeTypes;
  4. use OSS\Core\OssException;
  5. use OSS\Http\RequestCore;
  6. use OSS\Http\RequestCore_Exception;
  7. use OSS\Http\ResponseCore;
  8. use OSS\Model\CorsConfig;
  9. use OSS\Model\CnameConfig;
  10. use OSS\Model\LoggingConfig;
  11. use OSS\Model\LiveChannelConfig;
  12. use OSS\Model\LiveChannelInfo;
  13. use OSS\Model\LiveChannelListInfo;
  14. use OSS\Model\ObjectListInfoV2;
  15. use OSS\Model\StorageCapacityConfig;
  16. use OSS\Result\AclResult;
  17. use OSS\Result\BodyResult;
  18. use OSS\Result\GetCorsResult;
  19. use OSS\Result\GetLifecycleResult;
  20. use OSS\Result\GetLocationResult;
  21. use OSS\Result\GetLoggingResult;
  22. use OSS\Result\GetRefererResult;
  23. use OSS\Result\GetStorageCapacityResult;
  24. use OSS\Result\GetWebsiteResult;
  25. use OSS\Result\GetCnameResult;
  26. use OSS\Result\HeaderResult;
  27. use OSS\Result\InitiateMultipartUploadResult;
  28. use OSS\Result\ListBucketsResult;
  29. use OSS\Result\ListMultipartUploadResult;
  30. use OSS\Model\ListMultipartUploadInfo;
  31. use OSS\Result\ListObjectsResult;
  32. use OSS\Result\ListObjectsV2Result;
  33. use OSS\Result\ListPartsResult;
  34. use OSS\Result\PutSetDeleteResult;
  35. use OSS\Result\DeleteObjectsResult;
  36. use OSS\Result\CopyObjectResult;
  37. use OSS\Result\CallbackResult;
  38. use OSS\Result\ExistResult;
  39. use OSS\Result\PutLiveChannelResult;
  40. use OSS\Result\GetLiveChannelHistoryResult;
  41. use OSS\Result\GetLiveChannelInfoResult;
  42. use OSS\Result\GetLiveChannelStatusResult;
  43. use OSS\Result\ListLiveChannelResult;
  44. use OSS\Result\AppendResult;
  45. use OSS\Model\ObjectListInfo;
  46. use OSS\Result\SymlinkResult;
  47. use OSS\Result\UploadPartResult;
  48. use OSS\Model\BucketListInfo;
  49. use OSS\Model\LifecycleConfig;
  50. use OSS\Model\RefererConfig;
  51. use OSS\Model\WebsiteConfig;
  52. use OSS\Core\OssUtil;
  53. use OSS\Model\ListPartsInfo;
  54. use OSS\Result\GetBucketInfoResult;
  55. use OSS\Model\BucketStat;
  56. use OSS\Result\GetBucketStatResult;
  57. use OSS\Model\ServerSideEncryptionConfig;
  58. use OSS\Result\GetBucketEncryptionResult;
  59. use OSS\Model\RequestPaymentConfig;
  60. use OSS\Result\GetBucketRequestPaymentResult;
  61. use OSS\Model\Tag;
  62. use OSS\Model\TaggingConfig;
  63. use OSS\Result\GetBucketTagsResult;
  64. use OSS\Model\VersioningConfig;
  65. use OSS\Result\GetBucketVersioningResult;
  66. use OSS\Model\InitiateWormConfig;
  67. use OSS\Result\InitiateBucketWormResult;
  68. use OSS\Model\ExtendWormConfig;
  69. use OSS\Result\GetBucketWormResult;
  70. use OSS\Model\RestoreConfig;
  71. use OSS\Model\ObjectVersionListInfo;
  72. use OSS\Result\ListObjectVersionsResult;
  73. use OSS\Model\DeleteObjectInfo;
  74. use OSS\Model\DeletedObjectInfo;
  75. use OSS\Result\DeleteObjectVersionsResult;
  76. use OSS\Model\TransferAccelerationConfig;
  77. use OSS\Result\GetBucketTransferAccelerationResult;
  78. use OSS\Model\CnameTokenInfo;
  79. use OSS\Result\CreateBucketCnameTokenResult;
  80. use OSS\Result\GetBucketCnameTokenResult;
  81. /**
  82. * Class OssClient
  83. *
  84. * Object Storage Service(OSS)'s client class, which wraps all OSS APIs user could call to talk to OSS.
  85. * Users could do operations on bucket, object, including MultipartUpload or setting ACL via an OSSClient instance.
  86. * For more details, please check out the OSS API document:https://www.alibabacloud.com/help/doc-detail/31947.htm
  87. */
  88. class OssClient
  89. {
  90. /**
  91. * Constructor
  92. *
  93. * There're a few different ways to create an OssClient object:
  94. * 1. Most common one from access Id, access Key and the endpoint: $ossClient = new OssClient($id, $key, $endpoint)
  95. * 2. If the endpoint is the CName (such as www.testoss.com, make sure it's CName binded in the OSS console),
  96. * uses $ossClient = new OssClient($id, $key, $endpoint, true)
  97. * 3. If using Alicloud's security token service (STS), then the AccessKeyId, AccessKeySecret and STS token are all got from STS.
  98. * Use this: $ossClient = new OssClient($id, $key, $endpoint, false, $token)
  99. * 4. If the endpoint is in IP format, you could use this: $ossClient = new OssClient($id, $key, “1.2.3.4:8900”)
  100. *
  101. * @param string $accessKeyId The AccessKeyId from OSS or STS
  102. * @param string $accessKeySecret The AccessKeySecret from OSS or STS
  103. * @param string $endpoint The domain name of the datacenter,For example: oss-cn-hangzhou.aliyuncs.com
  104. * @param boolean $isCName If this is the CName and binded in the bucket.
  105. * @param string $securityToken from STS.
  106. * @param string $requestProxy
  107. * @throws OssException
  108. */
  109. public function __construct($accessKeyId, $accessKeySecret, $endpoint, $isCName = false, $securityToken = NULL, $requestProxy = NULL)
  110. {
  111. $accessKeyId = trim($accessKeyId);
  112. $accessKeySecret = trim($accessKeySecret);
  113. $endpoint = trim(trim($endpoint), "/");
  114. if (empty($accessKeyId)) {
  115. throw new OssException("access key id is empty");
  116. }
  117. if (empty($accessKeySecret)) {
  118. throw new OssException("access key secret is empty");
  119. }
  120. if (empty($endpoint)) {
  121. throw new OssException("endpoint is empty");
  122. }
  123. $this->hostname = $this->checkEndpoint($endpoint, $isCName);
  124. $this->accessKeyId = $accessKeyId;
  125. $this->accessKeySecret = $accessKeySecret;
  126. $this->securityToken = $securityToken;
  127. $this->requestProxy = $requestProxy;
  128. self::checkEnv();
  129. }
  130. /**
  131. * Lists the Bucket [GetService]. Not applicable if the endpoint is CName (because CName must be binded to a specific bucket).
  132. *
  133. * @param array $options
  134. * @throws OssException
  135. * @return BucketListInfo
  136. */
  137. public function listBuckets($options = NULL)
  138. {
  139. if ($this->hostType === self::OSS_HOST_TYPE_CNAME) {
  140. throw new OssException("operation is not permitted with CName host");
  141. }
  142. $this->precheckOptions($options);
  143. $options[self::OSS_BUCKET] = '';
  144. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  145. $options[self::OSS_OBJECT] = '/';
  146. $response = $this->auth($options);
  147. $result = new ListBucketsResult($response);
  148. return $result->getData();
  149. }
  150. /**
  151. * Creates bucket,The ACL of the bucket created by default is OssClient::OSS_ACL_TYPE_PRIVATE
  152. *
  153. * @param string $bucket
  154. * @param string $acl
  155. * @param array $options
  156. * @param string $storageType
  157. * @return null
  158. */
  159. public function createBucket($bucket, $acl = self::OSS_ACL_TYPE_PRIVATE, $options = NULL)
  160. {
  161. $this->precheckCommon($bucket, NULL, $options, false);
  162. $options[self::OSS_BUCKET] = $bucket;
  163. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  164. $options[self::OSS_OBJECT] = '/';
  165. $options[self::OSS_HEADERS] = array(self::OSS_ACL => $acl);
  166. if (isset($options[self::OSS_STORAGE])) {
  167. $this->precheckStorage($options[self::OSS_STORAGE]);
  168. $options[self::OSS_CONTENT] = OssUtil::createBucketXmlBody($options[self::OSS_STORAGE]);
  169. unset($options[self::OSS_STORAGE]);
  170. }
  171. $response = $this->auth($options);
  172. $result = new PutSetDeleteResult($response);
  173. return $result->getData();
  174. }
  175. /**
  176. * Deletes bucket
  177. * The deletion will not succeed if the bucket is not empty (either has objects or parts)
  178. * To delete a bucket, all its objects and parts must be deleted first.
  179. *
  180. * @param string $bucket
  181. * @param array $options
  182. * @return null
  183. */
  184. public function deleteBucket($bucket, $options = NULL)
  185. {
  186. $this->precheckCommon($bucket, NULL, $options, false);
  187. $options[self::OSS_BUCKET] = $bucket;
  188. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  189. $options[self::OSS_OBJECT] = '/';
  190. $response = $this->auth($options);
  191. $result = new PutSetDeleteResult($response);
  192. return $result->getData();
  193. }
  194. /**
  195. * Checks if a bucket exists
  196. *
  197. * @param string $bucket
  198. * @return bool
  199. * @throws OssException
  200. */
  201. public function doesBucketExist($bucket)
  202. {
  203. $this->precheckCommon($bucket, NULL, $options, false);
  204. $options[self::OSS_BUCKET] = $bucket;
  205. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  206. $options[self::OSS_OBJECT] = '/';
  207. $options[self::OSS_SUB_RESOURCE] = 'acl';
  208. $response = $this->auth($options);
  209. $result = new ExistResult($response);
  210. return $result->getData();
  211. }
  212. /**
  213. * Get the data center location information for the bucket
  214. *
  215. * @param string $bucket
  216. * @param array $options
  217. * @throws OssException
  218. * @return string
  219. */
  220. public function getBucketLocation($bucket, $options = NULL)
  221. {
  222. $this->precheckCommon($bucket, NULL, $options, false);
  223. $options[self::OSS_BUCKET] = $bucket;
  224. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  225. $options[self::OSS_OBJECT] = '/';
  226. $options[self::OSS_SUB_RESOURCE] = 'location';
  227. $response = $this->auth($options);
  228. $result = new GetLocationResult($response);
  229. return $result->getData();
  230. }
  231. /**
  232. * Get the Meta information for the Bucket
  233. *
  234. * @param string $bucket
  235. * @param array $options Refer to the SDK documentation
  236. * @return array
  237. */
  238. public function getBucketMeta($bucket, $options = NULL)
  239. {
  240. $this->precheckCommon($bucket, NULL, $options, false);
  241. $options[self::OSS_BUCKET] = $bucket;
  242. $options[self::OSS_METHOD] = self::OSS_HTTP_HEAD;
  243. $options[self::OSS_OBJECT] = '/';
  244. $response = $this->auth($options);
  245. $result = new HeaderResult($response);
  246. return $result->getData();
  247. }
  248. /**
  249. * Gets the bucket ACL
  250. *
  251. * @param string $bucket
  252. * @param array $options
  253. * @throws OssException
  254. * @return string
  255. */
  256. public function getBucketAcl($bucket, $options = NULL)
  257. {
  258. $this->precheckCommon($bucket, NULL, $options, false);
  259. $options[self::OSS_BUCKET] = $bucket;
  260. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  261. $options[self::OSS_OBJECT] = '/';
  262. $options[self::OSS_SUB_RESOURCE] = 'acl';
  263. $response = $this->auth($options);
  264. $result = new AclResult($response);
  265. return $result->getData();
  266. }
  267. /**
  268. * Sets the bucket ACL
  269. *
  270. * @param string $bucket bucket name
  271. * @param string $acl access permissions, valid values are ['private', 'public-read', 'public-read-write']
  272. * @param array $options by default is empty
  273. * @throws OssException
  274. * @return null
  275. */
  276. public function putBucketAcl($bucket, $acl, $options = NULL)
  277. {
  278. $this->precheckCommon($bucket, NULL, $options, false);
  279. $options[self::OSS_BUCKET] = $bucket;
  280. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  281. $options[self::OSS_OBJECT] = '/';
  282. $options[self::OSS_HEADERS] = array(self::OSS_ACL => $acl);
  283. $options[self::OSS_SUB_RESOURCE] = 'acl';
  284. $response = $this->auth($options);
  285. $result = new PutSetDeleteResult($response);
  286. return $result->getData();
  287. }
  288. /**
  289. * Gets object ACL
  290. *
  291. * @param string $bucket
  292. * @param string $object
  293. * @param array $options
  294. * @throws OssException
  295. * @return string
  296. */
  297. public function getObjectAcl($bucket, $object, $options = NULL)
  298. {
  299. $this->precheckCommon($bucket, $object, $options, true);
  300. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  301. $options[self::OSS_BUCKET] = $bucket;
  302. $options[self::OSS_OBJECT] = $object;
  303. $options[self::OSS_SUB_RESOURCE] = 'acl';
  304. $response = $this->auth($options);
  305. $result = new AclResult($response);
  306. return $result->getData();
  307. }
  308. /**
  309. * Sets the object ACL
  310. *
  311. * @param string $bucket bucket name
  312. * @param string $object object name
  313. * @param string $acl access permissions, valid values are ['default', 'private', 'public-read', 'public-read-write']
  314. * @param array $options
  315. * @throws OssException
  316. * @return null
  317. */
  318. public function putObjectAcl($bucket, $object, $acl, $options = NULL)
  319. {
  320. $this->precheckCommon($bucket, $object, $options, true);
  321. $options[self::OSS_BUCKET] = $bucket;
  322. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  323. $options[self::OSS_OBJECT] = $object;
  324. $options[self::OSS_HEADERS] = array(self::OSS_OBJECT_ACL => $acl);
  325. $options[self::OSS_SUB_RESOURCE] = 'acl';
  326. $response = $this->auth($options);
  327. $result = new PutSetDeleteResult($response);
  328. return $result->getData();
  329. }
  330. /**
  331. * Gets the bucket logging config
  332. *
  333. * @param string $bucket bucket name
  334. * @param array $options by default is empty
  335. * @throws OssException
  336. * @return LoggingConfig
  337. */
  338. public function getBucketLogging($bucket, $options = NULL)
  339. {
  340. $this->precheckCommon($bucket, NULL, $options, false);
  341. $options[self::OSS_BUCKET] = $bucket;
  342. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  343. $options[self::OSS_OBJECT] = '/';
  344. $options[self::OSS_SUB_RESOURCE] = 'logging';
  345. $response = $this->auth($options);
  346. $result = new GetLoggingResult($response);
  347. return $result->getData();
  348. }
  349. /**
  350. * Sets the bycket logging config. Only owner can call this API.
  351. *
  352. * @param string $bucket bucket name
  353. * @param string $targetBucket The logging file's bucket
  354. * @param string $targetPrefix The logging file's prefix
  355. * @param array $options By default is empty.
  356. * @throws OssException
  357. * @return null
  358. */
  359. public function putBucketLogging($bucket, $targetBucket, $targetPrefix, $options = NULL)
  360. {
  361. $this->precheckCommon($bucket, NULL, $options, false);
  362. $this->precheckBucket($targetBucket, 'targetbucket is not allowed empty');
  363. $options[self::OSS_BUCKET] = $bucket;
  364. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  365. $options[self::OSS_OBJECT] = '/';
  366. $options[self::OSS_SUB_RESOURCE] = 'logging';
  367. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  368. $loggingConfig = new LoggingConfig($targetBucket, $targetPrefix);
  369. $options[self::OSS_CONTENT] = $loggingConfig->serializeToXml();
  370. $response = $this->auth($options);
  371. $result = new PutSetDeleteResult($response);
  372. return $result->getData();
  373. }
  374. /**
  375. * Deletes the bucket logging config
  376. *
  377. * @param string $bucket bucket name
  378. * @param array $options
  379. * @throws OssException
  380. * @return null
  381. */
  382. public function deleteBucketLogging($bucket, $options = NULL)
  383. {
  384. $this->precheckCommon($bucket, NULL, $options, false);
  385. $options[self::OSS_BUCKET] = $bucket;
  386. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  387. $options[self::OSS_OBJECT] = '/';
  388. $options[self::OSS_SUB_RESOURCE] = 'logging';
  389. $response = $this->auth($options);
  390. $result = new PutSetDeleteResult($response);
  391. return $result->getData();
  392. }
  393. /**
  394. * Sets the website config in bucket---that is could make the bucket as a static website once the CName is binded.
  395. *
  396. * @param string $bucket bucket name
  397. * @param WebsiteConfig $websiteConfig
  398. * @param array $options
  399. * @throws OssException
  400. * @return null
  401. */
  402. public function putBucketWebsite($bucket, $websiteConfig, $options = NULL)
  403. {
  404. $this->precheckCommon($bucket, NULL, $options, false);
  405. $options[self::OSS_BUCKET] = $bucket;
  406. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  407. $options[self::OSS_OBJECT] = '/';
  408. $options[self::OSS_SUB_RESOURCE] = 'website';
  409. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  410. $options[self::OSS_CONTENT] = $websiteConfig->serializeToXml();
  411. $response = $this->auth($options);
  412. $result = new PutSetDeleteResult($response);
  413. return $result->getData();
  414. }
  415. /**
  416. * Gets the website config in the bucket
  417. *
  418. * @param string $bucket bucket name
  419. * @param array $options
  420. * @throws OssException
  421. * @return WebsiteConfig
  422. */
  423. public function getBucketWebsite($bucket, $options = NULL)
  424. {
  425. $this->precheckCommon($bucket, NULL, $options, false);
  426. $options[self::OSS_BUCKET] = $bucket;
  427. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  428. $options[self::OSS_OBJECT] = '/';
  429. $options[self::OSS_SUB_RESOURCE] = 'website';
  430. $response = $this->auth($options);
  431. $result = new GetWebsiteResult($response);
  432. return $result->getData();
  433. }
  434. /**
  435. * Deletes the website config in the bucket
  436. *
  437. * @param string $bucket bucket name
  438. * @param array $options
  439. * @throws OssException
  440. * @return null
  441. */
  442. public function deleteBucketWebsite($bucket, $options = NULL)
  443. {
  444. $this->precheckCommon($bucket, NULL, $options, false);
  445. $options[self::OSS_BUCKET] = $bucket;
  446. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  447. $options[self::OSS_OBJECT] = '/';
  448. $options[self::OSS_SUB_RESOURCE] = 'website';
  449. $response = $this->auth($options);
  450. $result = new PutSetDeleteResult($response);
  451. return $result->getData();
  452. }
  453. /**
  454. * Sets the cross-origin-resource-sharing (CORS) rule. It would overwrite the originl one.
  455. *
  456. * @param string $bucket bucket name
  457. * @param CorsConfig $corsConfig CORS config. Check out the details from OSS API document
  458. * @param array $options array
  459. * @throws OssException
  460. * @return null
  461. */
  462. public function putBucketCors($bucket, $corsConfig, $options = NULL)
  463. {
  464. $this->precheckCommon($bucket, NULL, $options, false);
  465. $options[self::OSS_BUCKET] = $bucket;
  466. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  467. $options[self::OSS_OBJECT] = '/';
  468. $options[self::OSS_SUB_RESOURCE] = 'cors';
  469. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  470. $options[self::OSS_CONTENT] = $corsConfig->serializeToXml();
  471. $response = $this->auth($options);
  472. $result = new PutSetDeleteResult($response);
  473. return $result->getData();
  474. }
  475. /**
  476. * Gets the bucket CORS config
  477. *
  478. * @param string $bucket bucket name
  479. * @param array $options
  480. * @throws OssException
  481. * @return CorsConfig
  482. */
  483. public function getBucketCors($bucket, $options = NULL)
  484. {
  485. $this->precheckCommon($bucket, NULL, $options, false);
  486. $options[self::OSS_BUCKET] = $bucket;
  487. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  488. $options[self::OSS_OBJECT] = '/';
  489. $options[self::OSS_SUB_RESOURCE] = 'cors';
  490. $response = $this->auth($options);
  491. $result = new GetCorsResult($response, __FUNCTION__);
  492. return $result->getData();
  493. }
  494. /**
  495. * Deletes the bucket's CORS config and disable the CORS on the bucket.
  496. *
  497. * @param string $bucket bucket name
  498. * @param array $options
  499. * @throws OssException
  500. * @return null
  501. */
  502. public function deleteBucketCors($bucket, $options = NULL)
  503. {
  504. $this->precheckCommon($bucket, NULL, $options, false);
  505. $options[self::OSS_BUCKET] = $bucket;
  506. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  507. $options[self::OSS_OBJECT] = '/';
  508. $options[self::OSS_SUB_RESOURCE] = 'cors';
  509. $response = $this->auth($options);
  510. $result = new PutSetDeleteResult($response);
  511. return $result->getData();
  512. }
  513. /**
  514. * Bind a CName for the bucket
  515. *
  516. * @param string $bucket bucket name
  517. * @param string $cname
  518. * @param array $options
  519. * @throws OssException
  520. * @return null
  521. */
  522. public function addBucketCname($bucket, $cname, $options = NULL)
  523. {
  524. $this->precheckCommon($bucket, NULL, $options, false);
  525. $options[self::OSS_BUCKET] = $bucket;
  526. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  527. $options[self::OSS_OBJECT] = '/';
  528. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  529. $cnameConfig = new CnameConfig();
  530. $cnameConfig->addCname($cname);
  531. $options[self::OSS_CONTENT] = $cnameConfig->serializeToXml();
  532. $options[self::OSS_COMP] = 'add';
  533. $options[self::OSS_CNAME] = '';
  534. $response = $this->auth($options);
  535. $result = new PutSetDeleteResult($response);
  536. return $result->getData();
  537. }
  538. /**
  539. * Gets the binded CName list of the bucket
  540. *
  541. * @param string $bucket bucket name
  542. * @param array $options
  543. * @throws OssException
  544. * @return CnameConfig
  545. */
  546. public function getBucketCname($bucket, $options = NULL)
  547. {
  548. $this->precheckCommon($bucket, NULL, $options, false);
  549. $options[self::OSS_BUCKET] = $bucket;
  550. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  551. $options[self::OSS_OBJECT] = '/';
  552. $options[self::OSS_CNAME] = '';
  553. $response = $this->auth($options);
  554. $result = new GetCnameResult($response);
  555. return $result->getData();
  556. }
  557. /**
  558. * Remove a CName binding from the bucket
  559. *
  560. * @param string $bucket bucket name
  561. * @param CnameConfig $cnameConfig
  562. * @param array $options
  563. * @throws OssException
  564. * @return null
  565. */
  566. public function deleteBucketCname($bucket, $cname, $options = NULL)
  567. {
  568. $this->precheckCommon($bucket, NULL, $options, false);
  569. $options[self::OSS_BUCKET] = $bucket;
  570. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  571. $options[self::OSS_OBJECT] = '/';
  572. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  573. $cnameConfig = new CnameConfig();
  574. $cnameConfig->addCname($cname);
  575. $options[self::OSS_CONTENT] = $cnameConfig->serializeToXml();
  576. $options[self::OSS_COMP] = 'delete';
  577. $options[self::OSS_CNAME] = '';
  578. $response = $this->auth($options);
  579. $result = new PutSetDeleteResult($response);
  580. return $result->getData();
  581. }
  582. /**
  583. * create a cname token for a bucket
  584. *
  585. * @param string $bucket bucket name
  586. * @param array $options
  587. * @throws OssException
  588. * @return CnameToken
  589. */
  590. public function createBucketCnameToken($bucket, $cname, $options = NULL)
  591. {
  592. $this->precheckCommon($bucket, NULL, $options, false);
  593. $options[self::OSS_BUCKET] = $bucket;
  594. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  595. $options[self::OSS_OBJECT] = '/';
  596. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  597. $cnameConfig = new CnameConfig();
  598. $cnameConfig->addCname($cname);
  599. $options[self::OSS_CONTENT] = $cnameConfig->serializeToXml();
  600. $options[self::OSS_COMP] = 'token';
  601. $options[self::OSS_CNAME] = '';
  602. $response = $this->auth($options);
  603. $result = new CreateBucketCnameTokenResult($response);
  604. return $result->getData();
  605. }
  606. /**
  607. * get a cname token for a bucket
  608. *
  609. * @param string $bucket bucket name
  610. * @param array $options
  611. * @throws OssException
  612. * @return CnameToken
  613. */
  614. public function getBucketCnameToken($bucket, $cname, $options = NULL)
  615. {
  616. $this->precheckCommon($bucket, NULL, $options, false);
  617. $options[self::OSS_BUCKET] = $bucket;
  618. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  619. $options[self::OSS_OBJECT] = '/';
  620. $options[self::OSS_COMP] = 'token';
  621. $options[self::OSS_CNAME] = $cname;
  622. $response = $this->auth($options);
  623. $result = new GetBucketCnameTokenResult($response);
  624. return $result->getData();
  625. }
  626. /**
  627. * Creates a Live Channel under a bucket
  628. *
  629. * @param string $bucket bucket name
  630. * @param string channelName $channelName
  631. * @param LiveChannelConfig $channelConfig
  632. * @param array $options
  633. * @throws OssException
  634. * @return LiveChannelInfo
  635. */
  636. public function putBucketLiveChannel($bucket, $channelName, $channelConfig, $options = NULL)
  637. {
  638. $this->precheckCommon($bucket, NULL, $options, false);
  639. $options[self::OSS_BUCKET] = $bucket;
  640. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  641. $options[self::OSS_OBJECT] = $channelName;
  642. $options[self::OSS_SUB_RESOURCE] = 'live';
  643. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  644. $options[self::OSS_CONTENT] = $channelConfig->serializeToXml();
  645. $response = $this->auth($options);
  646. $result = new PutLiveChannelResult($response);
  647. $info = $result->getData();
  648. $info->setName($channelName);
  649. $info->setDescription($channelConfig->getDescription());
  650. return $info;
  651. }
  652. /**
  653. * Sets the LiveChannel status
  654. *
  655. * @param string $bucket bucket name
  656. * @param string channelName $channelName
  657. * @param string channelStatus $channelStatus enabled or disabled
  658. * @param array $options
  659. * @throws OssException
  660. * @return null
  661. */
  662. public function putLiveChannelStatus($bucket, $channelName, $channelStatus, $options = NULL)
  663. {
  664. $this->precheckCommon($bucket, NULL, $options, false);
  665. $options[self::OSS_BUCKET] = $bucket;
  666. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  667. $options[self::OSS_OBJECT] = $channelName;
  668. $options[self::OSS_SUB_RESOURCE] = 'live';
  669. $options[self::OSS_LIVE_CHANNEL_STATUS] = $channelStatus;
  670. $response = $this->auth($options);
  671. $result = new PutSetDeleteResult($response);
  672. return $result->getData();
  673. }
  674. /**
  675. * Gets the LiveChannel information by the channel name
  676. *
  677. * @param string $bucket bucket name
  678. * @param string channelName $channelName
  679. * @param array $options
  680. * @throws OssException
  681. * @return GetLiveChannelInfo
  682. */
  683. public function getLiveChannelInfo($bucket, $channelName, $options = NULL)
  684. {
  685. $this->precheckCommon($bucket, NULL, $options, false);
  686. $options[self::OSS_BUCKET] = $bucket;
  687. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  688. $options[self::OSS_OBJECT] = $channelName;
  689. $options[self::OSS_SUB_RESOURCE] = 'live';
  690. $response = $this->auth($options);
  691. $result = new GetLiveChannelInfoResult($response);
  692. return $result->getData();
  693. }
  694. /**
  695. * Gets the status of LiveChannel
  696. *
  697. * @param string $bucket bucket name
  698. * @param string channelName $channelName
  699. * @param array $options
  700. * @throws OssException
  701. * @return GetLiveChannelStatus
  702. */
  703. public function getLiveChannelStatus($bucket, $channelName, $options = NULL)
  704. {
  705. $this->precheckCommon($bucket, NULL, $options, false);
  706. $options[self::OSS_BUCKET] = $bucket;
  707. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  708. $options[self::OSS_OBJECT] = $channelName;
  709. $options[self::OSS_SUB_RESOURCE] = 'live';
  710. $options[self::OSS_COMP] = 'stat';
  711. $response = $this->auth($options);
  712. $result = new GetLiveChannelStatusResult($response);
  713. return $result->getData();
  714. }
  715. /**
  716. * Gets the LiveChannel pushing streaming record
  717. *
  718. * @param string $bucket bucket name
  719. * @param string channelName $channelName
  720. * @param array $options
  721. * @throws OssException
  722. * @return GetLiveChannelHistory
  723. */
  724. public function getLiveChannelHistory($bucket, $channelName, $options = NULL)
  725. {
  726. $this->precheckCommon($bucket, NULL, $options, false);
  727. $options[self::OSS_BUCKET] = $bucket;
  728. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  729. $options[self::OSS_OBJECT] = $channelName;
  730. $options[self::OSS_SUB_RESOURCE] = 'live';
  731. $options[self::OSS_COMP] = 'history';
  732. $response = $this->auth($options);
  733. $result = new GetLiveChannelHistoryResult($response);
  734. return $result->getData();
  735. }
  736. /**
  737. *Gets the live channel list under a bucket.
  738. *
  739. * @param string $bucket bucket name
  740. * @param array $options
  741. * @throws OssException
  742. * @return LiveChannelListInfo
  743. */
  744. public function listBucketLiveChannels($bucket, $options = NULL)
  745. {
  746. $this->precheckCommon($bucket, NULL, $options, false);
  747. $options[self::OSS_BUCKET] = $bucket;
  748. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  749. $options[self::OSS_OBJECT] = '/';
  750. $options[self::OSS_SUB_RESOURCE] = 'live';
  751. $options[self::OSS_QUERY_STRING] = array(
  752. 'prefix' => isset($options['prefix']) ? $options['prefix'] : '',
  753. 'marker' => isset($options['marker']) ? $options['marker'] : '',
  754. 'max-keys' => isset($options['max-keys']) ? $options['max-keys'] : '',
  755. );
  756. $response = $this->auth($options);
  757. $result = new ListLiveChannelResult($response);
  758. $list = $result->getData();
  759. $list->setBucketName($bucket);
  760. return $list;
  761. }
  762. /**
  763. * Creates a play list file for the LiveChannel
  764. *
  765. * @param string $bucket bucket name
  766. * @param string channelName $channelName
  767. * @param string $playlistName The playlist name, must end with ".m3u8".
  768. * @param array $setTime startTime and EndTime in unix time. No more than 1 day.
  769. * @throws OssException
  770. * @return null
  771. */
  772. public function postVodPlaylist($bucket, $channelName, $playlistName, $setTime)
  773. {
  774. $this->precheckCommon($bucket, NULL, $options, false);
  775. $options[self::OSS_BUCKET] = $bucket;
  776. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  777. $options[self::OSS_OBJECT] = $channelName . '/' . $playlistName;
  778. $options[self::OSS_SUB_RESOURCE] = 'vod';
  779. $options[self::OSS_LIVE_CHANNEL_END_TIME] = $setTime['EndTime'];
  780. $options[self::OSS_LIVE_CHANNEL_START_TIME] = $setTime['StartTime'];
  781. $response = $this->auth($options);
  782. $result = new PutSetDeleteResult($response);
  783. return $result->getData();
  784. }
  785. /**
  786. * Deletes the Bucket LiveChannel
  787. *
  788. * @param string $bucket bucket name
  789. * @param string channelName $channelName
  790. * @param array $options
  791. * @throws OssException
  792. * @return null
  793. */
  794. public function deleteBucketLiveChannel($bucket, $channelName, $options = NULL)
  795. {
  796. $this->precheckCommon($bucket, NULL, $options, false);
  797. $options[self::OSS_BUCKET] = $bucket;
  798. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  799. $options[self::OSS_OBJECT] = $channelName;
  800. $options[self::OSS_SUB_RESOURCE] = 'live';
  801. $response = $this->auth($options);
  802. $result = new PutSetDeleteResult($response);
  803. return $result->getData();
  804. }
  805. /**
  806. * Generates the signed pushing streaming url
  807. *
  808. * @param string $bucket bucket name
  809. * @param string channelName $channelName
  810. * @param int timeout timeout value in seconds
  811. * @param array $options
  812. * @throws OssException
  813. * @return The signed pushing streaming url
  814. */
  815. public function signRtmpUrl($bucket, $channelName, $timeout = 60, $options = NULL)
  816. {
  817. $this->precheckCommon($bucket, $channelName, $options, false);
  818. $expires = time() + $timeout;
  819. $proto = 'rtmp://';
  820. $hostname = $this->generateHostname($bucket);
  821. $cano_params = '';
  822. $query_items = array();
  823. $params = isset($options['params']) ? $options['params'] : array();
  824. uksort($params, 'strnatcasecmp');
  825. foreach ($params as $key => $value) {
  826. $cano_params = $cano_params . $key . ':' . $value . "\n";
  827. $query_items[] = rawurlencode($key) . '=' . rawurlencode($value);
  828. }
  829. $resource = '/' . $bucket . '/' . $channelName;
  830. $string_to_sign = $expires . "\n" . $cano_params . $resource;
  831. $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->accessKeySecret, true));
  832. $query_items[] = 'OSSAccessKeyId=' . rawurlencode($this->accessKeyId);
  833. $query_items[] = 'Expires=' . rawurlencode($expires);
  834. $query_items[] = 'Signature=' . rawurlencode($signature);
  835. return $proto . $hostname . '/live/' . $channelName . '?' . implode('&', $query_items);
  836. }
  837. /**
  838. * Generates the signed pushing streaming url
  839. *
  840. * @param string $bucket bucket name
  841. * @param string $channelName channel name
  842. * @param int $expiration expiration time of the Url, unix epoch, since 1970.1.1 00.00.00 UTC
  843. * @param array $options
  844. * @throws OssException
  845. * @return The signed pushing streaming url
  846. */
  847. public function generatePresignedRtmpUrl($bucket, $channelName, $expiration, $options = NULL)
  848. {
  849. $this->precheckCommon($bucket, $channelName, $options, false);
  850. $proto = 'rtmp://';
  851. $hostname = $this->generateHostname($bucket);
  852. $cano_params = '';
  853. $query_items = array();
  854. $params = isset($options['params']) ? $options['params'] : array();
  855. uksort($params, 'strnatcasecmp');
  856. foreach ($params as $key => $value) {
  857. $cano_params = $cano_params . $key . ':' . $value . "\n";
  858. $query_items[] = rawurlencode($key) . '=' . rawurlencode($value);
  859. }
  860. $resource = '/' . $bucket . '/' . $channelName;
  861. $string_to_sign = $expiration . "\n" . $cano_params . $resource;
  862. $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->accessKeySecret, true));
  863. $query_items[] = 'OSSAccessKeyId=' . rawurlencode($this->accessKeyId);
  864. $query_items[] = 'Expires=' . rawurlencode($expiration);
  865. $query_items[] = 'Signature=' . rawurlencode($signature);
  866. return $proto . $hostname . '/live/' . $channelName . '?' . implode('&', $query_items);
  867. }
  868. /**
  869. * Precheck the CORS request. Before sending a CORS request, a preflight request (OPTIONS) is sent with the specific origin.
  870. * HTTP METHOD and headers information are sent to OSS as well for evaluating if the CORS request is allowed.
  871. *
  872. * Note: OSS could enable the CORS on the bucket by calling putBucketCors. Once CORS is enabled, the OSS could evaluate accordingto the preflight request.
  873. *
  874. * @param string $bucket bucket name
  875. * @param string $object object name
  876. * @param string $origin the origin of the request
  877. * @param string $request_method The actual HTTP method which will be used in CORS request
  878. * @param string $request_headers The actual HTTP headers which will be used in CORS request
  879. * @param array $options
  880. * @return array
  881. * @throws OssException
  882. * @link http://help.aliyun.com/document_detail/oss/api-reference/cors/OptionObject.html
  883. */
  884. public function optionsObject($bucket, $object, $origin, $request_method, $request_headers, $options = NULL)
  885. {
  886. $this->precheckCommon($bucket, NULL, $options, false);
  887. $options[self::OSS_BUCKET] = $bucket;
  888. $options[self::OSS_METHOD] = self::OSS_HTTP_OPTIONS;
  889. $options[self::OSS_OBJECT] = $object;
  890. $options[self::OSS_HEADERS] = array(
  891. self::OSS_OPTIONS_ORIGIN => $origin,
  892. self::OSS_OPTIONS_REQUEST_HEADERS => $request_headers,
  893. self::OSS_OPTIONS_REQUEST_METHOD => $request_method
  894. );
  895. $response = $this->auth($options);
  896. $result = new HeaderResult($response);
  897. return $result->getData();
  898. }
  899. /**
  900. * Sets the bucket's lifecycle config
  901. *
  902. * @param string $bucket bucket name
  903. * @param LifecycleConfig $lifecycleConfig LifecycleConfig instance
  904. * @param array $options
  905. * @throws OssException
  906. * @return null
  907. */
  908. public function putBucketLifecycle($bucket, $lifecycleConfig, $options = NULL)
  909. {
  910. $this->precheckCommon($bucket, NULL, $options, false);
  911. $options[self::OSS_BUCKET] = $bucket;
  912. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  913. $options[self::OSS_OBJECT] = '/';
  914. $options[self::OSS_SUB_RESOURCE] = 'lifecycle';
  915. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  916. $options[self::OSS_CONTENT] = $lifecycleConfig->serializeToXml();
  917. $response = $this->auth($options);
  918. $result = new PutSetDeleteResult($response);
  919. return $result->getData();
  920. }
  921. /**
  922. * Gets bucket's lifecycle config
  923. *
  924. * @param string $bucket bucket name
  925. * @param array $options
  926. * @throws OssException
  927. * @return LifecycleConfig
  928. */
  929. public function getBucketLifecycle($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] = 'lifecycle';
  936. $response = $this->auth($options);
  937. $result = new GetLifecycleResult($response);
  938. return $result->getData();
  939. }
  940. /**
  941. * Deletes the bucket's lifecycle config
  942. *
  943. * @param string $bucket bucket name
  944. * @param array $options
  945. * @throws OssException
  946. * @return null
  947. */
  948. public function deleteBucketLifecycle($bucket, $options = NULL)
  949. {
  950. $this->precheckCommon($bucket, NULL, $options, false);
  951. $options[self::OSS_BUCKET] = $bucket;
  952. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  953. $options[self::OSS_OBJECT] = '/';
  954. $options[self::OSS_SUB_RESOURCE] = 'lifecycle';
  955. $response = $this->auth($options);
  956. $result = new PutSetDeleteResult($response);
  957. return $result->getData();
  958. }
  959. /**
  960. * Sets a bucket's referer, which has a whitelist of referrer and specifies if empty referer is allowed.
  961. * Checks out API document for more details about "Bucket Referer"
  962. *
  963. * @param string $bucket bucket name
  964. * @param RefererConfig $refererConfig
  965. * @param array $options
  966. * @return ResponseCore
  967. * @throws null
  968. */
  969. public function putBucketReferer($bucket, $refererConfig, $options = NULL)
  970. {
  971. $this->precheckCommon($bucket, NULL, $options, false);
  972. $options[self::OSS_BUCKET] = $bucket;
  973. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  974. $options[self::OSS_OBJECT] = '/';
  975. $options[self::OSS_SUB_RESOURCE] = 'referer';
  976. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  977. $options[self::OSS_CONTENT] = $refererConfig->serializeToXml();
  978. $response = $this->auth($options);
  979. $result = new PutSetDeleteResult($response);
  980. return $result->getData();
  981. }
  982. /**
  983. * Gets the bucket's Referer
  984. * Checks out API document for more details about "Bucket Referer"
  985. *
  986. * @param string $bucket bucket name
  987. * @param array $options
  988. * @throws OssException
  989. * @return RefererConfig
  990. */
  991. public function getBucketReferer($bucket, $options = NULL)
  992. {
  993. $this->precheckCommon($bucket, NULL, $options, false);
  994. $options[self::OSS_BUCKET] = $bucket;
  995. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  996. $options[self::OSS_OBJECT] = '/';
  997. $options[self::OSS_SUB_RESOURCE] = 'referer';
  998. $response = $this->auth($options);
  999. $result = new GetRefererResult($response);
  1000. return $result->getData();
  1001. }
  1002. /**
  1003. * Set the size of the bucket,the unit is GB
  1004. * When the capacity of the bucket is bigger than the set, it's forbidden to continue writing
  1005. *
  1006. * @param string $bucket bucket name
  1007. * @param int $storageCapacity
  1008. * @param array $options
  1009. * @return ResponseCore
  1010. * @throws null
  1011. */
  1012. public function putBucketStorageCapacity($bucket, $storageCapacity, $options = NULL)
  1013. {
  1014. $this->precheckCommon($bucket, NULL, $options, false);
  1015. $options[self::OSS_BUCKET] = $bucket;
  1016. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1017. $options[self::OSS_OBJECT] = '/';
  1018. $options[self::OSS_SUB_RESOURCE] = 'qos';
  1019. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  1020. $storageCapacityConfig = new StorageCapacityConfig($storageCapacity);
  1021. $options[self::OSS_CONTENT] = $storageCapacityConfig->serializeToXml();
  1022. $response = $this->auth($options);
  1023. $result = new PutSetDeleteResult($response);
  1024. return $result->getData();
  1025. }
  1026. /**
  1027. * Get the capacity of the bucket, the unit is GB
  1028. *
  1029. * @param string $bucket bucket name
  1030. * @param array $options
  1031. * @throws OssException
  1032. * @return int
  1033. */
  1034. public function getBucketStorageCapacity($bucket, $options = NULL)
  1035. {
  1036. $this->precheckCommon($bucket, NULL, $options, false);
  1037. $options[self::OSS_BUCKET] = $bucket;
  1038. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1039. $options[self::OSS_OBJECT] = '/';
  1040. $options[self::OSS_SUB_RESOURCE] = 'qos';
  1041. $response = $this->auth($options);
  1042. $result = new GetStorageCapacityResult($response);
  1043. return $result->getData();
  1044. }
  1045. /**
  1046. * Get the information of the bucket
  1047. *
  1048. * @param string $bucket bucket name
  1049. * @param array $options
  1050. * @throws OssException
  1051. * @return BucketInfo
  1052. */
  1053. public function getBucketInfo($bucket, $options = NULL)
  1054. {
  1055. $this->precheckCommon($bucket, NULL, $options, false);
  1056. $options[self::OSS_BUCKET] = $bucket;
  1057. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1058. $options[self::OSS_OBJECT] = '/';
  1059. $options[self::OSS_SUB_RESOURCE] = 'bucketInfo';
  1060. $response = $this->auth($options);
  1061. $result = new GetBucketInfoResult($response);
  1062. return $result->getData();
  1063. }
  1064. /**
  1065. * Get the stat of the bucket
  1066. *
  1067. * @param string $bucket bucket name
  1068. * @param array $options
  1069. * @throws OssException
  1070. * @return BucketStat
  1071. */
  1072. public function getBucketStat($bucket, $options = NULL)
  1073. {
  1074. $this->precheckCommon($bucket, NULL, $options, false);
  1075. $options[self::OSS_BUCKET] = $bucket;
  1076. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1077. $options[self::OSS_OBJECT] = '/';
  1078. $options[self::OSS_SUB_RESOURCE] = 'stat';
  1079. $response = $this->auth($options);
  1080. $result = new GetBucketStatResult($response);
  1081. return $result->getData();
  1082. }
  1083. /**
  1084. * Sets the bucket's policy
  1085. *
  1086. * @param string $bucket bucket name
  1087. * @param string $policy policy json format content
  1088. * @param array $options
  1089. * @throws OssException
  1090. * @return null
  1091. */
  1092. public function putBucketPolicy($bucket, $policy, $options = NULL)
  1093. {
  1094. $this->precheckCommon($bucket, NULL, $options, false);
  1095. $options[self::OSS_BUCKET] = $bucket;
  1096. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1097. $options[self::OSS_OBJECT] = '/';
  1098. $options[self::OSS_SUB_RESOURCE] = 'policy';
  1099. $options[self::OSS_CONTENT_TYPE] = 'application/json';
  1100. $options[self::OSS_CONTENT] = $policy;
  1101. $response = $this->auth($options);
  1102. $result = new PutSetDeleteResult($response);
  1103. return $result->getData();
  1104. }
  1105. /**
  1106. * Gets bucket's policy
  1107. *
  1108. * @param string $bucket bucket name
  1109. * @param array $options
  1110. * @throws OssException
  1111. * @return string policy json content
  1112. */
  1113. public function getBucketPolicy($bucket, $options = NULL)
  1114. {
  1115. $this->precheckCommon($bucket, NULL, $options, false);
  1116. $options[self::OSS_BUCKET] = $bucket;
  1117. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1118. $options[self::OSS_OBJECT] = '/';
  1119. $options[self::OSS_SUB_RESOURCE] = 'policy';
  1120. $response = $this->auth($options);
  1121. $result = new BodyResult($response);
  1122. return $result->getData();
  1123. }
  1124. /**
  1125. * Deletes the bucket's policy
  1126. *
  1127. * @param string $bucket bucket name
  1128. * @param array $options
  1129. * @throws OssException
  1130. * @return null
  1131. */
  1132. public function deleteBucketPolicy($bucket, $options = NULL)
  1133. {
  1134. $this->precheckCommon($bucket, NULL, $options, false);
  1135. $options[self::OSS_BUCKET] = $bucket;
  1136. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  1137. $options[self::OSS_OBJECT] = '/';
  1138. $options[self::OSS_SUB_RESOURCE] = 'policy';
  1139. $response = $this->auth($options);
  1140. $result = new PutSetDeleteResult($response);
  1141. return $result->getData();
  1142. }
  1143. /**
  1144. * Sets the bucket's encryption
  1145. *
  1146. * @param string $bucket bucket name
  1147. * @param ServerSideEncryptionConfig $sseConfig
  1148. * @param array $options
  1149. * @throws OssException
  1150. * @return null
  1151. */
  1152. public function putBucketEncryption($bucket, $sseConfig, $options = NULL)
  1153. {
  1154. $this->precheckCommon($bucket, NULL, $options, false);
  1155. $options[self::OSS_BUCKET] = $bucket;
  1156. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1157. $options[self::OSS_OBJECT] = '/';
  1158. $options[self::OSS_SUB_RESOURCE] = 'encryption';
  1159. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  1160. $options[self::OSS_CONTENT] = $sseConfig->serializeToXml();
  1161. $response = $this->auth($options);
  1162. $result = new PutSetDeleteResult($response);
  1163. return $result->getData();
  1164. }
  1165. /**
  1166. * Gets bucket's encryption
  1167. *
  1168. * @param string $bucket bucket name
  1169. * @param array $options
  1170. * @throws OssException
  1171. * @return ServerSideEncryptionConfig
  1172. */
  1173. public function getBucketEncryption($bucket, $options = NULL)
  1174. {
  1175. $this->precheckCommon($bucket, NULL, $options, false);
  1176. $options[self::OSS_BUCKET] = $bucket;
  1177. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1178. $options[self::OSS_OBJECT] = '/';
  1179. $options[self::OSS_SUB_RESOURCE] = 'encryption';
  1180. $response = $this->auth($options);
  1181. $result = new GetBucketEncryptionResult($response);
  1182. return $result->getData();
  1183. }
  1184. /**
  1185. * Deletes the bucket's encryption
  1186. *
  1187. * @param string $bucket bucket name
  1188. * @param array $options
  1189. * @throws OssException
  1190. * @return null
  1191. */
  1192. public function deleteBucketEncryption($bucket, $options = NULL)
  1193. {
  1194. $this->precheckCommon($bucket, NULL, $options, false);
  1195. $options[self::OSS_BUCKET] = $bucket;
  1196. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  1197. $options[self::OSS_OBJECT] = '/';
  1198. $options[self::OSS_SUB_RESOURCE] = 'encryption';
  1199. $response = $this->auth($options);
  1200. $result = new PutSetDeleteResult($response);
  1201. return $result->getData();
  1202. }
  1203. /**
  1204. * Set the request playment of the bucket, Can be BucketOwner and Requester
  1205. *
  1206. * @param string $bucket bucket name
  1207. * @param string $payer
  1208. * @param array $options
  1209. * @return ResponseCore
  1210. * @throws null
  1211. */
  1212. public function putBucketRequestPayment($bucket, $payer, $options = NULL)
  1213. {
  1214. $this->precheckCommon($bucket, NULL, $options, false);
  1215. $options[self::OSS_BUCKET] = $bucket;
  1216. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1217. $options[self::OSS_OBJECT] = '/';
  1218. $options[self::OSS_SUB_RESOURCE] = 'requestPayment';
  1219. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  1220. $config = new RequestPaymentConfig($payer);
  1221. $options[self::OSS_CONTENT] = $config->serializeToXml();
  1222. $response = $this->auth($options);
  1223. $result = new PutSetDeleteResult($response);
  1224. return $result->getData();
  1225. }
  1226. /**
  1227. * Get the request playment of the bucket
  1228. *
  1229. * @param string $bucket bucket name
  1230. * @param array $options
  1231. * @throws OssException
  1232. * @return string
  1233. */
  1234. public function getBucketRequestPayment($bucket, $options = NULL)
  1235. {
  1236. $this->precheckCommon($bucket, NULL, $options, false);
  1237. $options[self::OSS_BUCKET] = $bucket;
  1238. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1239. $options[self::OSS_OBJECT] = '/';
  1240. $options[self::OSS_SUB_RESOURCE] = 'requestPayment';
  1241. $response = $this->auth($options);
  1242. $result = new GetBucketRequestPaymentResult($response);
  1243. return $result->getData();
  1244. }
  1245. /**
  1246. * Sets the bucket's tags
  1247. *
  1248. * @param string $bucket bucket name
  1249. * @param TaggingConfig $taggingConfig
  1250. * @param array $options
  1251. * @throws OssException
  1252. * @return null
  1253. */
  1254. public function putBucketTags($bucket, $taggingConfig, $options = NULL)
  1255. {
  1256. $this->precheckCommon($bucket, NULL, $options, false);
  1257. $options[self::OSS_BUCKET] = $bucket;
  1258. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1259. $options[self::OSS_OBJECT] = '/';
  1260. $options[self::OSS_SUB_RESOURCE] = self::OSS_TAGGING;
  1261. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  1262. $options[self::OSS_CONTENT] = $taggingConfig->serializeToXml();
  1263. $response = $this->auth($options);
  1264. $result = new PutSetDeleteResult($response);
  1265. return $result->getData();
  1266. }
  1267. /**
  1268. * Gets bucket's tags
  1269. *
  1270. * @param string $bucket bucket name
  1271. * @param array $options
  1272. * @throws OssException
  1273. * @return TaggingConfig
  1274. */
  1275. public function getBucketTags($bucket, $options = NULL)
  1276. {
  1277. $this->precheckCommon($bucket, NULL, $options, false);
  1278. $options[self::OSS_BUCKET] = $bucket;
  1279. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1280. $options[self::OSS_OBJECT] = '/';
  1281. $options[self::OSS_SUB_RESOURCE] = self::OSS_TAGGING;
  1282. $response = $this->auth($options);
  1283. $result = new GetBucketTagsResult($response);
  1284. return $result->getData();
  1285. }
  1286. /**
  1287. * Deletes the bucket's tags
  1288. * If want to delete specified tags for a bucket, please set the $tags
  1289. *
  1290. * @param string $bucket bucket name
  1291. * @param tag[] $tags (optional)
  1292. * @param array $options
  1293. * @throws OssException
  1294. * @return null
  1295. */
  1296. public function deleteBucketTags($bucket, $tags = NULL, $options = NULL)
  1297. {
  1298. $this->precheckCommon($bucket, NULL, $options, false);
  1299. $options[self::OSS_BUCKET] = $bucket;
  1300. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  1301. $options[self::OSS_OBJECT] = '/';
  1302. if (empty($tags)) {
  1303. $options[self::OSS_SUB_RESOURCE] = self::OSS_TAGGING;
  1304. } else {
  1305. $value = '';
  1306. foreach ($tags as $tag ) {
  1307. $value .= $tag->getKey().',';
  1308. }
  1309. $value = rtrim($value, ',');
  1310. $options[self::OSS_TAGGING] = $value;
  1311. }
  1312. $response = $this->auth($options);
  1313. $result = new PutSetDeleteResult($response);
  1314. return $result->getData();
  1315. }
  1316. /**
  1317. * Set the versioning of the bucket, Can be BucketOwner and Requester
  1318. *
  1319. * @param string $bucket bucket name
  1320. * @param string $status
  1321. * @param array $options
  1322. * @return ResponseCore
  1323. * @throws null
  1324. */
  1325. public function putBucketVersioning($bucket, $status, $options = NULL)
  1326. {
  1327. $this->precheckCommon($bucket, NULL, $options, false);
  1328. $options[self::OSS_BUCKET] = $bucket;
  1329. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1330. $options[self::OSS_OBJECT] = '/';
  1331. $options[self::OSS_SUB_RESOURCE] = 'versioning';
  1332. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  1333. $config = new VersioningConfig($status);
  1334. $options[self::OSS_CONTENT] = $config->serializeToXml();
  1335. $response = $this->auth($options);
  1336. $result = new PutSetDeleteResult($response);
  1337. return $result->getData();
  1338. }
  1339. /**
  1340. * Get the versioning of the bucket
  1341. *
  1342. * @param string $bucket bucket name
  1343. * @param array $options
  1344. * @throws OssException
  1345. * @return string
  1346. */
  1347. public function getBucketVersioning($bucket, $options = NULL)
  1348. {
  1349. $this->precheckCommon($bucket, NULL, $options, false);
  1350. $options[self::OSS_BUCKET] = $bucket;
  1351. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1352. $options[self::OSS_OBJECT] = '/';
  1353. $options[self::OSS_SUB_RESOURCE] = 'versioning';
  1354. $response = $this->auth($options);
  1355. $result = new GetBucketVersioningResult($response);
  1356. return $result->getData();
  1357. }
  1358. /**
  1359. * Initialize a bucket's worm
  1360. *
  1361. * @param string $bucket bucket name
  1362. * @param int $day
  1363. * @param array $options
  1364. * @throws OssException
  1365. * @return string returns uploadid
  1366. */
  1367. public function initiateBucketWorm($bucket, $day, $options = NULL)
  1368. {
  1369. $this->precheckCommon($bucket, NULL, $options, false);
  1370. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1371. $options[self::OSS_BUCKET] = $bucket;
  1372. $options[self::OSS_OBJECT] = '/';
  1373. $options[self::OSS_SUB_RESOURCE] = 'worm';
  1374. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  1375. $config = new InitiateWormConfig($day);
  1376. $options[self::OSS_CONTENT] = $config->serializeToXml();
  1377. $response = $this->auth($options);
  1378. $result = new InitiateBucketWormResult($response);
  1379. return $result->getData();
  1380. }
  1381. /**
  1382. * Aborts the bucket's worm
  1383. *
  1384. * @param string $bucket bucket name
  1385. * @param array $options
  1386. * @throws OssException
  1387. * @return null
  1388. */
  1389. public function abortBucketWorm($bucket, $options = NULL)
  1390. {
  1391. $this->precheckCommon($bucket, NULL, $options, false);
  1392. $options[self::OSS_BUCKET] = $bucket;
  1393. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  1394. $options[self::OSS_OBJECT] = '/';
  1395. $options[self::OSS_SUB_RESOURCE] = 'worm';
  1396. $response = $this->auth($options);
  1397. $result = new PutSetDeleteResult($response);
  1398. return $result->getData();
  1399. }
  1400. /**
  1401. * Complete a bucket's worm
  1402. *
  1403. * @param string $bucket bucket name
  1404. * @param string $wormId
  1405. * @param array $options
  1406. * @throws OssException
  1407. * @return string returns uploadid
  1408. */
  1409. public function completeBucketWorm($bucket, $wormId, $options = NULL)
  1410. {
  1411. $this->precheckCommon($bucket, NULL, $options, false);
  1412. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1413. $options[self::OSS_BUCKET] = $bucket;
  1414. $options[self::OSS_OBJECT] = '/';
  1415. $options[self::OSS_WORM_ID] = $wormId;
  1416. $options[self::OSS_CONTENT] = '';
  1417. $response = $this->auth($options);
  1418. $result = new PutSetDeleteResult($response);
  1419. return $result->getData();
  1420. }
  1421. /**
  1422. * Extend a bucket's worm
  1423. *
  1424. * @param string $bucket bucket name
  1425. * @param string $wormId
  1426. * @param int $day
  1427. * @param array $options
  1428. * @throws OssException
  1429. * @return string returns uploadid
  1430. */
  1431. public function extendBucketWorm($bucket, $wormId, $day, $options = NULL)
  1432. {
  1433. $this->precheckCommon($bucket, NULL, $options, false);
  1434. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1435. $options[self::OSS_BUCKET] = $bucket;
  1436. $options[self::OSS_OBJECT] = '/';
  1437. $options[self::OSS_WORM_ID] = $wormId;
  1438. $options[self::OSS_SUB_RESOURCE] = 'wormExtend';
  1439. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  1440. $config = new ExtendWormConfig($day);
  1441. $options[self::OSS_CONTENT] = $config->serializeToXml();
  1442. $response = $this->auth($options);
  1443. $result = new PutSetDeleteResult($response);
  1444. return $result->getData();
  1445. }
  1446. /**
  1447. * Get a bucket's worm
  1448. *
  1449. * @param string $bucket bucket name
  1450. * @param array $options
  1451. * @throws OssException
  1452. * @return string
  1453. */
  1454. public function getBucketWorm($bucket, $options = NULL)
  1455. {
  1456. $this->precheckCommon($bucket, NULL, $options, false);
  1457. $options[self::OSS_BUCKET] = $bucket;
  1458. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1459. $options[self::OSS_OBJECT] = '/';
  1460. $options[self::OSS_SUB_RESOURCE] = 'worm';
  1461. $response = $this->auth($options);
  1462. $result = new GetBucketWormResult($response);
  1463. return $result->getData();
  1464. }
  1465. /**
  1466. * Put Bucket TransferAcceleration
  1467. * @param $bucket
  1468. * @param $enabled boolean
  1469. * @param null $options
  1470. * @return null
  1471. * @throws OssException
  1472. */
  1473. public function putBucketTransferAcceleration($bucket,$enabled,$options = NULL)
  1474. {
  1475. $this->precheckCommon($bucket, NULL, $options, false);
  1476. $options[self::OSS_BUCKET] = $bucket;
  1477. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1478. $options[self::OSS_OBJECT] = '/';
  1479. $options[self::OSS_SUB_RESOURCE] = 'transferAcceleration';
  1480. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  1481. $config = new TransferAccelerationConfig();
  1482. $config->setEnabled($enabled);
  1483. $options[self::OSS_CONTENT] = $config->serializeToXml();
  1484. $response = $this->auth($options);
  1485. $result = new HeaderResult($response);
  1486. return $result->getData();
  1487. }
  1488. /**
  1489. * Put Bucket TransferAcceleration
  1490. * @param $bucket
  1491. * @param null $options
  1492. * @return enabled boolean
  1493. * @throws OssException
  1494. */
  1495. public function getBucketTransferAcceleration($bucket,$options = NULL)
  1496. {
  1497. $this->precheckCommon($bucket, NULL, $options, false);
  1498. $options[self::OSS_BUCKET] = $bucket;
  1499. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1500. $options[self::OSS_OBJECT] = '/';
  1501. $options[self::OSS_SUB_RESOURCE] = 'transferAcceleration';
  1502. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  1503. $response = $this->auth($options);
  1504. $result = new GetBucketTransferAccelerationResult($response);
  1505. return $result->getData();
  1506. }
  1507. /**
  1508. * Lists the bucket's object list (in ObjectListInfo)
  1509. *
  1510. * @param string $bucket
  1511. * @param array $options are defined below:
  1512. * $options = array(
  1513. * 'max-keys' => specifies max object count to return. By default is 100 and max value could be 1000.
  1514. * 'prefix' => specifies the key prefix the returned objects must have. Note that the returned keys still contain the prefix.
  1515. * '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.
  1516. * 'marker' => The key of returned object must be greater than the 'marker'.
  1517. *)
  1518. * Prefix and marker are for filtering and paging. Their length must be less than 256 bytes
  1519. * @throws OssException
  1520. * @return ObjectListInfo
  1521. */
  1522. public function listObjects($bucket, $options = NULL)
  1523. {
  1524. $this->precheckCommon($bucket, NULL, $options, false);
  1525. $options[self::OSS_BUCKET] = $bucket;
  1526. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1527. $options[self::OSS_OBJECT] = '/';
  1528. $query = isset($options[self::OSS_QUERY_STRING]) ? $options[self::OSS_QUERY_STRING] : array();
  1529. $options[self::OSS_QUERY_STRING] = array_merge(
  1530. $query,
  1531. array(self::OSS_ENCODING_TYPE => self::OSS_ENCODING_TYPE_URL,
  1532. self::OSS_DELIMITER => isset($options[self::OSS_DELIMITER]) ? $options[self::OSS_DELIMITER] : '/',
  1533. self::OSS_PREFIX => isset($options[self::OSS_PREFIX]) ? $options[self::OSS_PREFIX] : '',
  1534. self::OSS_MAX_KEYS => isset($options[self::OSS_MAX_KEYS]) ? $options[self::OSS_MAX_KEYS] : self::OSS_MAX_KEYS_VALUE,
  1535. self::OSS_MARKER => isset($options[self::OSS_MARKER]) ? $options[self::OSS_MARKER] : '')
  1536. );
  1537. $response = $this->auth($options);
  1538. $result = new ListObjectsResult($response);
  1539. return $result->getData();
  1540. }
  1541. /**
  1542. * Lists the bucket's object list v2 (in ObjectListInfoV2)
  1543. *
  1544. * @param string $bucket
  1545. * @param array $options are defined below:
  1546. * $options = array(
  1547. * 'max-keys' => specifies max object count to return. By default is 100 and max value could be 1000.
  1548. * 'prefix' => specifies the key prefix the returned objects must have. Note that the returned keys still contain the prefix.
  1549. * '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.
  1550. * 'start-after' => The key of returned object must be greater than the 'start-after'.
  1551. * 'continuation-token' => The token from which the list operation must start.
  1552. *)
  1553. * Prefix, start-after and continuation-token are for filtering and paging. Their length must be less than 256 bytes
  1554. * @throws OssException
  1555. * @return ObjectListInfoV2
  1556. */
  1557. public function listObjectsV2($bucket, $options = NULL)
  1558. {
  1559. $this->precheckCommon($bucket, NULL, $options, false);
  1560. $options[self::OSS_BUCKET] = $bucket;
  1561. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1562. $options[self::OSS_OBJECT] = '/';
  1563. $query = isset($options[self::OSS_QUERY_STRING]) ? $options[self::OSS_QUERY_STRING] : array();
  1564. $temp = array(
  1565. self::OSS_LIST_TYPE=>2,
  1566. self::OSS_ENCODING_TYPE => self::OSS_ENCODING_TYPE_URL,
  1567. self::OSS_DELIMITER => isset($options[self::OSS_DELIMITER]) ? $options[self::OSS_DELIMITER] : '/',
  1568. self::OSS_PREFIX => isset($options[self::OSS_PREFIX]) ? $options[self::OSS_PREFIX] : '',
  1569. self::OSS_MAX_KEYS => isset($options[self::OSS_MAX_KEYS]) ? $options[self::OSS_MAX_KEYS] : self::OSS_MAX_KEYS_VALUE,
  1570. self::OSS_START_AFTER => isset($options[self::OSS_START_AFTER]) ? $options[self::OSS_START_AFTER] : '',
  1571. );
  1572. if(isset($options[self::OSS_CONTINUATION_TOKEN])){
  1573. $temp[self::OSS_CONTINUATION_TOKEN] = $options[self::OSS_CONTINUATION_TOKEN];
  1574. }
  1575. $options[self::OSS_QUERY_STRING] = array_merge(
  1576. $query,$temp
  1577. );
  1578. $response = $this->auth($options);
  1579. $result = new ListObjectsV2Result($response);
  1580. return $result->getData();
  1581. }
  1582. /**
  1583. * Lists the bucket's object with version information (in ObjectListInfo)
  1584. *
  1585. * @param string $bucket
  1586. * @param array $options are defined below:
  1587. * $options = array(
  1588. * 'max-keys' => specifies max object count to return. By default is 100 and max value could be 1000.
  1589. * 'prefix' => specifies the key prefix the returned objects must have. Note that the returned keys still contain the prefix.
  1590. * 'delimiter' => The delimiter of object name for grouping object. When it's specified, listObjectVersions will differeniate the object and folder. And it will return subfolder's objects.
  1591. * 'key-marker' => The key of returned object must be greater than the 'key-marker'.
  1592. * 'version-id-marker' => The version id of returned object must be greater than the 'version-id-marker'.
  1593. *)
  1594. * Prefix and marker are for filtering and paging. Their length must be less than 256 bytes
  1595. * @throws OssException
  1596. * @return ObjectListInfo
  1597. */
  1598. public function listObjectVersions($bucket, $options = NULL)
  1599. {
  1600. $this->precheckCommon($bucket, NULL, $options, false);
  1601. $options[self::OSS_BUCKET] = $bucket;
  1602. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1603. $options[self::OSS_OBJECT] = '/';
  1604. $options[self::OSS_SUB_RESOURCE] = 'versions';
  1605. $query = isset($options[self::OSS_QUERY_STRING]) ? $options[self::OSS_QUERY_STRING] : array();
  1606. $options[self::OSS_QUERY_STRING] = array_merge(
  1607. $query,
  1608. array(self::OSS_ENCODING_TYPE => self::OSS_ENCODING_TYPE_URL,
  1609. self::OSS_DELIMITER => isset($options[self::OSS_DELIMITER]) ? $options[self::OSS_DELIMITER] : '/',
  1610. self::OSS_PREFIX => isset($options[self::OSS_PREFIX]) ? $options[self::OSS_PREFIX] : '',
  1611. self::OSS_MAX_KEYS => isset($options[self::OSS_MAX_KEYS]) ? $options[self::OSS_MAX_KEYS] : self::OSS_MAX_KEYS_VALUE,
  1612. self::OSS_KEY_MARKER => isset($options[self::OSS_KEY_MARKER]) ? $options[self::OSS_KEY_MARKER] : '',
  1613. self::OSS_VERSION_ID_MARKER => isset($options[self::OSS_VERSION_ID_MARKER]) ? $options[self::OSS_VERSION_ID_MARKER] : '')
  1614. );
  1615. $response = $this->auth($options);
  1616. $result = new ListObjectVersionsResult($response);
  1617. return $result->getData();
  1618. }
  1619. /**
  1620. * Creates a virtual 'folder' in OSS. The name should not end with '/' because the method will append the name with a '/' anyway.
  1621. *
  1622. * Internal use only.
  1623. *
  1624. * @param string $bucket bucket name
  1625. * @param string $object object name
  1626. * @param array $options
  1627. * @return null
  1628. */
  1629. public function createObjectDir($bucket, $object, $options = NULL)
  1630. {
  1631. $this->precheckCommon($bucket, $object, $options);
  1632. $options[self::OSS_BUCKET] = $bucket;
  1633. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1634. $options[self::OSS_OBJECT] = $object . '/';
  1635. $options[self::OSS_CONTENT_LENGTH] = array(self::OSS_CONTENT_LENGTH => 0);
  1636. $response = $this->auth($options);
  1637. $result = new PutSetDeleteResult($response);
  1638. return $result->getData();
  1639. }
  1640. /**
  1641. * Uploads the $content object to OSS.
  1642. *
  1643. * @param string $bucket bucket name
  1644. * @param string $object objcet name
  1645. * @param string $content The content object
  1646. * @param array $options
  1647. * @return null
  1648. */
  1649. public function putObject($bucket, $object, $content, $options = NULL)
  1650. {
  1651. $this->precheckCommon($bucket, $object, $options);
  1652. $options[self::OSS_CONTENT] = $content;
  1653. $options[self::OSS_BUCKET] = $bucket;
  1654. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1655. $options[self::OSS_OBJECT] = $object;
  1656. if (!isset($options[self::OSS_LENGTH])) {
  1657. $options[self::OSS_CONTENT_LENGTH] = strlen($options[self::OSS_CONTENT]);
  1658. } else {
  1659. $options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH];
  1660. }
  1661. $is_check_md5 = $this->isCheckMD5($options);
  1662. if ($is_check_md5) {
  1663. $content_md5 = base64_encode(md5($content, true));
  1664. $options[self::OSS_CONTENT_MD5] = $content_md5;
  1665. }
  1666. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  1667. $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object);
  1668. }
  1669. $response = $this->auth($options);
  1670. if (isset($options[self::OSS_CALLBACK]) && !empty($options[self::OSS_CALLBACK])) {
  1671. $result = new CallbackResult($response);
  1672. } else {
  1673. $result = new PutSetDeleteResult($response);
  1674. }
  1675. return $result->getData();
  1676. }
  1677. /**
  1678. * creates symlink
  1679. * @param string $bucket bucket name
  1680. * @param string $symlink symlink name
  1681. * @param string $targetObject targetObject name
  1682. * @param array $options
  1683. * @return null
  1684. */
  1685. public function putSymlink($bucket, $symlink ,$targetObject, $options = NULL)
  1686. {
  1687. $this->precheckCommon($bucket, $symlink, $options);
  1688. $options[self::OSS_BUCKET] = $bucket;
  1689. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1690. $options[self::OSS_OBJECT] = $symlink;
  1691. $options[self::OSS_SUB_RESOURCE] = self::OSS_SYMLINK;
  1692. $options[self::OSS_HEADERS][self::OSS_SYMLINK_TARGET] = rawurlencode($targetObject);
  1693. $response = $this->auth($options);
  1694. $result = new PutSetDeleteResult($response);
  1695. return $result->getData();
  1696. }
  1697. /**
  1698. * gets symlink
  1699. * @param string $bucket bucket name
  1700. * @param string $symlink symlink name
  1701. * @param array $options
  1702. * @return null
  1703. */
  1704. public function getSymlink($bucket, $symlink, $options = NULL)
  1705. {
  1706. $this->precheckCommon($bucket, $symlink, $options);
  1707. $options[self::OSS_BUCKET] = $bucket;
  1708. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1709. $options[self::OSS_OBJECT] = $symlink;
  1710. $options[self::OSS_SUB_RESOURCE] = self::OSS_SYMLINK;
  1711. $response = $this->auth($options);
  1712. $result = new SymlinkResult($response);
  1713. return $result->getData();
  1714. }
  1715. /**
  1716. * Uploads a local file
  1717. *
  1718. * @param string $bucket bucket name
  1719. * @param string $object object name
  1720. * @param string $file local file path
  1721. * @param array $options
  1722. * @return null
  1723. * @throws OssException
  1724. */
  1725. public function uploadFile($bucket, $object, $file, $options = NULL)
  1726. {
  1727. $this->precheckCommon($bucket, $object, $options);
  1728. OssUtil::throwOssExceptionWithMessageIfEmpty($file, "file path is invalid");
  1729. $file = OssUtil::encodePath($file);
  1730. if (!file_exists($file)) {
  1731. throw new OssException($file . " file does not exist");
  1732. }
  1733. $options[self::OSS_FILE_UPLOAD] = $file;
  1734. $file_size = sprintf('%u',filesize($options[self::OSS_FILE_UPLOAD]));
  1735. $is_check_md5 = $this->isCheckMD5($options);
  1736. if ($is_check_md5) {
  1737. $content_md5 = base64_encode(md5_file($options[self::OSS_FILE_UPLOAD], true));
  1738. $options[self::OSS_CONTENT_MD5] = $content_md5;
  1739. }
  1740. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  1741. $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object, $file);
  1742. }
  1743. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1744. $options[self::OSS_BUCKET] = $bucket;
  1745. $options[self::OSS_OBJECT] = $object;
  1746. $options[self::OSS_CONTENT_LENGTH] = $file_size;
  1747. $response = $this->auth($options);
  1748. $result = new PutSetDeleteResult($response);
  1749. return $result->getData();
  1750. }
  1751. /**
  1752. * Uploads object from file handle
  1753. *
  1754. * @param string $bucket bucket name
  1755. * @param string $object object name
  1756. * @param resource $handle file handle
  1757. * @param array $options
  1758. * @return null
  1759. * @throws OssException
  1760. */
  1761. public function uploadStream($bucket, $object, $handle, $options = NULL)
  1762. {
  1763. $this->precheckCommon($bucket, $object, $options);
  1764. if (!is_resource($handle)) {
  1765. throw new OssException("The handle must be an opened stream");
  1766. }
  1767. $options[self::OSS_FILE_UPLOAD] = $handle;
  1768. if ($this->isCheckMD5($options)) {
  1769. rewind($handle);
  1770. $ctx = hash_init('md5');
  1771. hash_update_stream($ctx, $handle);
  1772. $content_md5 = base64_encode(hash_final($ctx, true));
  1773. rewind($handle);
  1774. $options[self::OSS_CONTENT_MD5] = $content_md5;
  1775. }
  1776. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  1777. $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object);
  1778. }
  1779. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1780. $options[self::OSS_BUCKET] = $bucket;
  1781. $options[self::OSS_OBJECT] = $object;
  1782. if (!isset($options[self::OSS_CONTENT_LENGTH])) {
  1783. $options[self::OSS_CONTENT_LENGTH] = fstat($handle)[self::OSS_SIZE];
  1784. }
  1785. $response = $this->auth($options);
  1786. $result = new PutSetDeleteResult($response);
  1787. return $result->getData();
  1788. }
  1789. /**
  1790. * Append the object with the content at the specified position.
  1791. * The specified position is typically the lengh of the current file.
  1792. * @param string $bucket bucket name
  1793. * @param string $object objcet name
  1794. * @param string $content content to append
  1795. * @param array $options
  1796. * @return int next append position
  1797. * @throws OssException
  1798. */
  1799. public function appendObject($bucket, $object, $content, $position, $options = NULL)
  1800. {
  1801. $this->precheckCommon($bucket, $object, $options);
  1802. $options[self::OSS_CONTENT] = $content;
  1803. $options[self::OSS_BUCKET] = $bucket;
  1804. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1805. $options[self::OSS_OBJECT] = $object;
  1806. $options[self::OSS_SUB_RESOURCE] = 'append';
  1807. $options[self::OSS_POSITION] = strval($position);
  1808. if (!isset($options[self::OSS_LENGTH])) {
  1809. $options[self::OSS_CONTENT_LENGTH] = strlen($options[self::OSS_CONTENT]);
  1810. } else {
  1811. $options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH];
  1812. }
  1813. $is_check_md5 = $this->isCheckMD5($options);
  1814. if ($is_check_md5) {
  1815. $content_md5 = base64_encode(md5($content, true));
  1816. $options[self::OSS_CONTENT_MD5] = $content_md5;
  1817. }
  1818. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  1819. $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object);
  1820. }
  1821. $response = $this->auth($options);
  1822. $result = new AppendResult($response);
  1823. return $result->getData();
  1824. }
  1825. /**
  1826. * Append the object with a local file
  1827. *
  1828. * @param string $bucket bucket name
  1829. * @param string $object object name
  1830. * @param string $file The local file path to append with
  1831. * @param array $options
  1832. * @return int next append position
  1833. * @throws OssException
  1834. */
  1835. public function appendFile($bucket, $object, $file, $position, $options = NULL)
  1836. {
  1837. $this->precheckCommon($bucket, $object, $options);
  1838. OssUtil::throwOssExceptionWithMessageIfEmpty($file, "file path is invalid");
  1839. $file = OssUtil::encodePath($file);
  1840. if (!file_exists($file)) {
  1841. throw new OssException($file . " file does not exist");
  1842. }
  1843. $options[self::OSS_FILE_UPLOAD] = $file;
  1844. $file_size = sprintf('%u',filesize($options[self::OSS_FILE_UPLOAD]));
  1845. $is_check_md5 = $this->isCheckMD5($options);
  1846. if ($is_check_md5) {
  1847. $content_md5 = base64_encode(md5_file($options[self::OSS_FILE_UPLOAD], true));
  1848. $options[self::OSS_CONTENT_MD5] = $content_md5;
  1849. }
  1850. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  1851. $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object, $file);
  1852. }
  1853. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1854. $options[self::OSS_BUCKET] = $bucket;
  1855. $options[self::OSS_OBJECT] = $object;
  1856. $options[self::OSS_CONTENT_LENGTH] = $file_size;
  1857. $options[self::OSS_SUB_RESOURCE] = 'append';
  1858. $options[self::OSS_POSITION] = strval($position);
  1859. $response = $this->auth($options);
  1860. $result = new AppendResult($response);
  1861. return $result->getData();
  1862. }
  1863. /**
  1864. * Copy from an existing OSS object to another OSS object. If the target object exists already, it will be overwritten.
  1865. *
  1866. * @param string $fromBucket Source bucket name
  1867. * @param string $fromObject Source object name
  1868. * @param string $toBucket Target bucket name
  1869. * @param string $toObject Target object name
  1870. * @param array $options
  1871. * @return null
  1872. * @throws OssException
  1873. */
  1874. public function copyObject($fromBucket, $fromObject, $toBucket, $toObject, $options = NULL)
  1875. {
  1876. $this->precheckCommon($fromBucket, $fromObject, $options);
  1877. $this->precheckCommon($toBucket, $toObject, $options);
  1878. $options[self::OSS_BUCKET] = $toBucket;
  1879. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1880. $options[self::OSS_OBJECT] = $toObject;
  1881. $param = '/' . $fromBucket . '/' . rawurlencode($fromObject);
  1882. if (isset($options[self::OSS_VERSION_ID])) {
  1883. $param = $param . '?versionId='.$options[self::OSS_VERSION_ID];
  1884. unset($options[self::OSS_VERSION_ID]);
  1885. }
  1886. if (isset($options[self::OSS_HEADERS])) {
  1887. $options[self::OSS_HEADERS][self::OSS_OBJECT_COPY_SOURCE] = $param;
  1888. } else {
  1889. $options[self::OSS_HEADERS] = array(self::OSS_OBJECT_COPY_SOURCE => $param);
  1890. }
  1891. $response = $this->auth($options);
  1892. $result = new CopyObjectResult($response);
  1893. return $result->getData();
  1894. }
  1895. /**
  1896. * Gets Object metadata
  1897. *
  1898. * @param string $bucket bucket name
  1899. * @param string $object object name
  1900. * @param string $options Checks out the SDK document for the detail
  1901. * @return array
  1902. */
  1903. public function getObjectMeta($bucket, $object, $options = NULL)
  1904. {
  1905. $this->precheckCommon($bucket, $object, $options);
  1906. $options[self::OSS_BUCKET] = $bucket;
  1907. $options[self::OSS_METHOD] = self::OSS_HTTP_HEAD;
  1908. $options[self::OSS_OBJECT] = $object;
  1909. $response = $this->auth($options);
  1910. $result = new HeaderResult($response);
  1911. return $result->getData();
  1912. }
  1913. /**
  1914. * Gets the simplified metadata of a object.
  1915. * Simplified metadata includes ETag, Size, LastModified.
  1916. *
  1917. * @param string $bucket bucket name
  1918. * @param string $object object name
  1919. * @param string $options Checks out the SDK document for the detail
  1920. * @return array
  1921. */
  1922. public function getSimplifiedObjectMeta($bucket, $object, $options = NULL)
  1923. {
  1924. $this->precheckCommon($bucket, $object, $options);
  1925. $options[self::OSS_BUCKET] = $bucket;
  1926. $options[self::OSS_METHOD] = self::OSS_HTTP_HEAD;
  1927. $options[self::OSS_OBJECT] = $object;
  1928. $options[self::OSS_SUB_RESOURCE] = 'objectMeta';
  1929. $response = $this->auth($options);
  1930. $result = new HeaderResult($response);
  1931. return $result->getData();
  1932. }
  1933. /**
  1934. * Deletes a object
  1935. *
  1936. * @param string $bucket bucket name
  1937. * @param string $object object name
  1938. * @param array $options
  1939. * @return null
  1940. */
  1941. public function deleteObject($bucket, $object, $options = NULL)
  1942. {
  1943. $this->precheckCommon($bucket, $object, $options);
  1944. $options[self::OSS_BUCKET] = $bucket;
  1945. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  1946. $options[self::OSS_OBJECT] = $object;
  1947. $response = $this->auth($options);
  1948. $result = new PutSetDeleteResult($response);
  1949. return $result->getData();
  1950. }
  1951. /**
  1952. * Deletes multiple objects in a bucket
  1953. *
  1954. * @param string $bucket bucket name
  1955. * @param array $objects object list
  1956. * @param array $options
  1957. * @return ResponseCore
  1958. * @throws null
  1959. */
  1960. public function deleteObjects($bucket, $objects, $options = null)
  1961. {
  1962. $this->precheckCommon($bucket, NULL, $options, false);
  1963. if (!is_array($objects) || !$objects) {
  1964. throw new OssException('objects must be array');
  1965. }
  1966. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1967. $options[self::OSS_BUCKET] = $bucket;
  1968. $options[self::OSS_OBJECT] = '/';
  1969. $options[self::OSS_SUB_RESOURCE] = 'delete';
  1970. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  1971. $quiet = 'false';
  1972. if (isset($options['quiet'])) {
  1973. if (is_bool($options['quiet'])) { //Boolean
  1974. $quiet = $options['quiet'] ? 'true' : 'false';
  1975. } elseif (is_string($options['quiet'])) { // string
  1976. $quiet = ($options['quiet'] === 'true') ? 'true' : 'false';
  1977. }
  1978. }
  1979. $xmlBody = OssUtil::createDeleteObjectsXmlBody($objects, $quiet);
  1980. $options[self::OSS_CONTENT] = $xmlBody;
  1981. $response = $this->auth($options);
  1982. $result = new DeleteObjectsResult($response);
  1983. return $result->getData();
  1984. }
  1985. /**
  1986. * Deletes multiple objects with version id in a bucket
  1987. *
  1988. * @param string $bucket bucket name
  1989. * @param array $objects DeleteObjectInfo list
  1990. * @param array $options
  1991. * @return ResponseCore
  1992. * @throws null
  1993. */
  1994. public function deleteObjectVersions($bucket, $objects, $options = null)
  1995. {
  1996. $this->precheckCommon($bucket, NULL, $options, false);
  1997. if (!is_array($objects) || !$objects) {
  1998. throw new OssException('objects must be array');
  1999. }
  2000. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  2001. $options[self::OSS_BUCKET] = $bucket;
  2002. $options[self::OSS_OBJECT] = '/';
  2003. $options[self::OSS_SUB_RESOURCE] = 'delete';
  2004. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  2005. $quiet = 'false';
  2006. if (isset($options['quiet'])) {
  2007. if (is_bool($options['quiet'])) { //Boolean
  2008. $quiet = $options['quiet'] ? 'true' : 'false';
  2009. } elseif (is_string($options['quiet'])) { // string
  2010. $quiet = ($options['quiet'] === 'true') ? 'true' : 'false';
  2011. }
  2012. }
  2013. $xmlBody = OssUtil::createDeleteObjectVersionsXmlBody($objects, $quiet);
  2014. $options[self::OSS_CONTENT] = $xmlBody;
  2015. $response = $this->auth($options);
  2016. $result = new DeleteObjectVersionsResult($response);
  2017. return $result->getData();
  2018. }
  2019. /**
  2020. * Gets Object content
  2021. *
  2022. * @param string $bucket bucket name
  2023. * @param string $object object name
  2024. * @param array $options It must contain ALIOSS::OSS_FILE_DOWNLOAD. And ALIOSS::OSS_RANGE is optional and empty means to download the whole file.
  2025. * @return string
  2026. */
  2027. public function getObject($bucket, $object, $options = NULL)
  2028. {
  2029. $this->precheckCommon($bucket, $object, $options);
  2030. $options[self::OSS_BUCKET] = $bucket;
  2031. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  2032. $options[self::OSS_OBJECT] = $object;
  2033. if (isset($options[self::OSS_LAST_MODIFIED])) {
  2034. $options[self::OSS_HEADERS][self::OSS_IF_MODIFIED_SINCE] = $options[self::OSS_LAST_MODIFIED];
  2035. unset($options[self::OSS_LAST_MODIFIED]);
  2036. }
  2037. if (isset($options[self::OSS_ETAG])) {
  2038. $options[self::OSS_HEADERS][self::OSS_IF_NONE_MATCH] = $options[self::OSS_ETAG];
  2039. unset($options[self::OSS_ETAG]);
  2040. }
  2041. if (isset($options[self::OSS_RANGE])) {
  2042. $range = $options[self::OSS_RANGE];
  2043. $options[self::OSS_HEADERS][self::OSS_RANGE] = "bytes=$range";
  2044. unset($options[self::OSS_RANGE]);
  2045. }
  2046. $response = $this->auth($options);
  2047. $result = new BodyResult($response);
  2048. return $result->getData();
  2049. }
  2050. /**
  2051. * Checks if the object exists
  2052. * It's implemented by getObjectMeta().
  2053. *
  2054. * @param string $bucket bucket name
  2055. * @param string $object object name
  2056. * @param array $options
  2057. * @return bool True:object exists; False:object does not exist
  2058. */
  2059. public function doesObjectExist($bucket, $object, $options = NULL)
  2060. {
  2061. $this->precheckCommon($bucket, $object, $options);
  2062. $options[self::OSS_BUCKET] = $bucket;
  2063. $options[self::OSS_METHOD] = self::OSS_HTTP_HEAD;
  2064. $options[self::OSS_OBJECT] = $object;
  2065. $response = $this->auth($options);
  2066. $result = new ExistResult($response);
  2067. return $result->getData();
  2068. }
  2069. /**
  2070. * Object reading for Archive type
  2071. * Use Restore to enable the server to perform the thawing task
  2072. *
  2073. * @param string $bucket bucket name
  2074. * @param string $object object name
  2075. * @return null
  2076. * @throws OssException
  2077. */
  2078. public function restoreObject($bucket, $object, $options = NULL)
  2079. {
  2080. $this->precheckCommon($bucket, $object, $options);
  2081. $options[self::OSS_BUCKET] = $bucket;
  2082. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  2083. $options[self::OSS_OBJECT] = $object;
  2084. $options[self::OSS_SUB_RESOURCE] = self::OSS_RESTORE;
  2085. if (isset($options[self::OSS_RESTORE_CONFIG])) {
  2086. $config = $options[self::OSS_RESTORE_CONFIG];
  2087. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  2088. $options[self::OSS_CONTENT] = $config->serializeToXml();
  2089. }
  2090. $response = $this->auth($options);
  2091. $result = new PutSetDeleteResult($response);
  2092. return $result->getData();
  2093. }
  2094. /**
  2095. * Sets the object tagging
  2096. *
  2097. * @param string $bucket bucket name
  2098. * @param string $object object name
  2099. * @param TaggingConfig $taggingConfig
  2100. * @throws OssException
  2101. * @return null
  2102. */
  2103. public function putObjectTagging($bucket, $object, $taggingConfig, $options = NULL)
  2104. {
  2105. $this->precheckCommon($bucket, $object, $options, true);
  2106. $options[self::OSS_BUCKET] = $bucket;
  2107. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  2108. $options[self::OSS_OBJECT] = $object;
  2109. $options[self::OSS_SUB_RESOURCE] = self::OSS_TAGGING;
  2110. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  2111. $options[self::OSS_CONTENT] = $taggingConfig->serializeToXml();
  2112. $response = $this->auth($options);
  2113. $result = new PutSetDeleteResult($response);
  2114. return $result->getData();
  2115. }
  2116. /**
  2117. * Gets the object tagging
  2118. *
  2119. * @param string $bucket
  2120. * @param string $object
  2121. * @throws OssException
  2122. * @return TaggingConfig
  2123. */
  2124. public function getObjectTagging($bucket, $object, $options = NULL)
  2125. {
  2126. $this->precheckCommon($bucket, $object, $options, true);
  2127. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  2128. $options[self::OSS_BUCKET] = $bucket;
  2129. $options[self::OSS_OBJECT] = $object;
  2130. $options[self::OSS_SUB_RESOURCE] = self::OSS_TAGGING;
  2131. $response = $this->auth($options);
  2132. $result = new GetBucketTagsResult($response);
  2133. return $result->getData();
  2134. }
  2135. /**
  2136. * Deletes the object tagging
  2137. *
  2138. * @param string $bucket
  2139. * @param string $object
  2140. * @throws OssException
  2141. * @return TaggingConfig
  2142. */
  2143. public function deleteObjectTagging($bucket, $object, $options = NULL)
  2144. {
  2145. $this->precheckCommon($bucket, $object, $options, true);
  2146. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  2147. $options[self::OSS_BUCKET] = $bucket;
  2148. $options[self::OSS_OBJECT] = $object;
  2149. $options[self::OSS_SUB_RESOURCE] = self::OSS_TAGGING;
  2150. $response = $this->auth($options);
  2151. $result = new PutSetDeleteResult($response);
  2152. return $result->getData();
  2153. }
  2154. /**
  2155. * Processes the object
  2156. *
  2157. * @param string $bucket bucket name
  2158. * @param string $object object name
  2159. * @param string $process process script
  2160. * @return string process result, json format
  2161. */
  2162. public function processObject($bucket, $object, $process, $options = NULL)
  2163. {
  2164. $this->precheckCommon($bucket, $object, $options);
  2165. $options[self::OSS_BUCKET] = $bucket;
  2166. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  2167. $options[self::OSS_OBJECT] = $object;
  2168. $options[self::OSS_SUB_RESOURCE] = 'x-oss-process';
  2169. $options[self::OSS_CONTENT_TYPE] = 'application/octet-stream';
  2170. $options[self::OSS_CONTENT] = 'x-oss-process='.$process;
  2171. $response = $this->auth($options);
  2172. $result = new BodyResult($response);
  2173. return $result->getData();
  2174. }
  2175. /**
  2176. * Gets the part size according to the preferred part size.
  2177. * If the specified part size is too small or too big, it will return a min part or max part size instead.
  2178. * Otherwise returns the specified part size.
  2179. * @param int $partSize
  2180. * @return int
  2181. */
  2182. private function computePartSize($partSize)
  2183. {
  2184. $partSize = (integer)$partSize;
  2185. if ($partSize <= self::OSS_MIN_PART_SIZE) {
  2186. $partSize = self::OSS_MIN_PART_SIZE;
  2187. } elseif ($partSize > self::OSS_MAX_PART_SIZE) {
  2188. $partSize = self::OSS_MAX_PART_SIZE;
  2189. }
  2190. return $partSize;
  2191. }
  2192. /**
  2193. * Computes the parts count, size and start position according to the file size and the part size.
  2194. * It must be only called by upload_Part().
  2195. *
  2196. * @param integer $file_size File size
  2197. * @param integer $partSize part大小,part size. Default is 5MB
  2198. * @return array An array contains key-value pairs--the key is `seekTo`and value is `length`.
  2199. */
  2200. public function generateMultiuploadParts($file_size, $partSize = 5242880)
  2201. {
  2202. $i = 0;
  2203. $size_count = $file_size;
  2204. $values = array();
  2205. $partSize = $this->computePartSize($partSize);
  2206. while ($size_count > 0) {
  2207. $size_count -= $partSize;
  2208. $values[] = array(
  2209. self::OSS_SEEK_TO => ($partSize * $i),
  2210. self::OSS_LENGTH => (($size_count > 0) ? $partSize : ($size_count + $partSize)),
  2211. );
  2212. $i++;
  2213. }
  2214. return $values;
  2215. }
  2216. /**
  2217. * Initialize a multi-part upload
  2218. *
  2219. * @param string $bucket bucket name
  2220. * @param string $object object name
  2221. * @param array $options Key-Value array
  2222. * @throws OssException
  2223. * @return string returns uploadid
  2224. */
  2225. public function initiateMultipartUpload($bucket, $object, $options = NULL)
  2226. {
  2227. $this->precheckCommon($bucket, $object, $options);
  2228. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  2229. $options[self::OSS_BUCKET] = $bucket;
  2230. $options[self::OSS_OBJECT] = $object;
  2231. $options[self::OSS_SUB_RESOURCE] = 'uploads';
  2232. $options[self::OSS_CONTENT] = '';
  2233. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  2234. $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object);
  2235. }
  2236. if (!isset($options[self::OSS_HEADERS])) {
  2237. $options[self::OSS_HEADERS] = array();
  2238. }
  2239. $response = $this->auth($options);
  2240. $result = new InitiateMultipartUploadResult($response);
  2241. return $result->getData();
  2242. }
  2243. /**
  2244. * Upload a part in a multiparts upload.
  2245. *
  2246. * @param string $bucket bucket name
  2247. * @param string $object object name
  2248. * @param string $uploadId
  2249. * @param array $options Key-Value array
  2250. * @return string eTag
  2251. * @throws OssException
  2252. */
  2253. public function uploadPart($bucket, $object, $uploadId, $options = null)
  2254. {
  2255. $this->precheckCommon($bucket, $object, $options);
  2256. $this->precheckParam($options, self::OSS_FILE_UPLOAD, __FUNCTION__);
  2257. $this->precheckParam($options, self::OSS_PART_NUM, __FUNCTION__);
  2258. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  2259. $options[self::OSS_BUCKET] = $bucket;
  2260. $options[self::OSS_OBJECT] = $object;
  2261. $options[self::OSS_UPLOAD_ID] = $uploadId;
  2262. if (isset($options[self::OSS_LENGTH])) {
  2263. $options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH];
  2264. }
  2265. $response = $this->auth($options);
  2266. $result = new UploadPartResult($response);
  2267. return $result->getData();
  2268. }
  2269. /**
  2270. * Gets the uploaded parts.
  2271. *
  2272. * @param string $bucket bucket name
  2273. * @param string $object object name
  2274. * @param string $uploadId uploadId
  2275. * @param array $options Key-Value array
  2276. * @return ListPartsInfo
  2277. * @throws OssException
  2278. */
  2279. public function listParts($bucket, $object, $uploadId, $options = null)
  2280. {
  2281. $this->precheckCommon($bucket, $object, $options);
  2282. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  2283. $options[self::OSS_BUCKET] = $bucket;
  2284. $options[self::OSS_OBJECT] = $object;
  2285. $options[self::OSS_UPLOAD_ID] = $uploadId;
  2286. $options[self::OSS_QUERY_STRING] = array();
  2287. foreach (array('max-parts', 'part-number-marker') as $param) {
  2288. if (isset($options[$param])) {
  2289. $options[self::OSS_QUERY_STRING][$param] = $options[$param];
  2290. unset($options[$param]);
  2291. }
  2292. }
  2293. $response = $this->auth($options);
  2294. $result = new ListPartsResult($response);
  2295. return $result->getData();
  2296. }
  2297. /**
  2298. * Abort a multiparts upload
  2299. *
  2300. * @param string $bucket bucket name
  2301. * @param string $object object name
  2302. * @param string $uploadId uploadId
  2303. * @param array $options Key-Value name
  2304. * @return null
  2305. * @throws OssException
  2306. */
  2307. public function abortMultipartUpload($bucket, $object, $uploadId, $options = NULL)
  2308. {
  2309. $this->precheckCommon($bucket, $object, $options);
  2310. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  2311. $options[self::OSS_BUCKET] = $bucket;
  2312. $options[self::OSS_OBJECT] = $object;
  2313. $options[self::OSS_UPLOAD_ID] = $uploadId;
  2314. $response = $this->auth($options);
  2315. $result = new PutSetDeleteResult($response);
  2316. return $result->getData();
  2317. }
  2318. /**
  2319. * Completes a multiparts upload, after all parts are uploaded.
  2320. *
  2321. * @param string $bucket bucket name
  2322. * @param string $object object name
  2323. * @param string $uploadId uploadId
  2324. * @param array $listParts array( array("PartNumber"=> int, "ETag"=>string))
  2325. * @param array $options Key-Value array
  2326. * @throws OssException
  2327. * @return null
  2328. */
  2329. public function completeMultipartUpload($bucket, $object, $uploadId, $listParts, $options = NULL)
  2330. {
  2331. $this->precheckCommon($bucket, $object, $options);
  2332. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  2333. $options[self::OSS_BUCKET] = $bucket;
  2334. $options[self::OSS_OBJECT] = $object;
  2335. $options[self::OSS_UPLOAD_ID] = $uploadId;
  2336. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  2337. if (is_array($listParts)) {
  2338. $options[self::OSS_CONTENT] = OssUtil::createCompleteMultipartUploadXmlBody($listParts);
  2339. } else {
  2340. $options[self::OSS_CONTENT] = "";
  2341. }
  2342. $response = $this->auth($options);
  2343. if (isset($options[self::OSS_CALLBACK]) && !empty($options[self::OSS_CALLBACK])) {
  2344. $result = new CallbackResult($response);
  2345. } else {
  2346. $result = new PutSetDeleteResult($response);
  2347. }
  2348. return $result->getData();
  2349. }
  2350. /**
  2351. * Lists all ongoing multipart upload events, which means all initialized but not completed or aborted multipart uploads.
  2352. *
  2353. * @param string $bucket bucket
  2354. * @param array $options key-value array--expected keys are 'delimiter', 'key-marker', 'max-uploads', 'prefix', 'upload-id-marker'
  2355. * @throws OssException
  2356. * @return ListMultipartUploadInfo
  2357. */
  2358. public function listMultipartUploads($bucket, $options = null)
  2359. {
  2360. $this->precheckCommon($bucket, NULL, $options, false);
  2361. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  2362. $options[self::OSS_BUCKET] = $bucket;
  2363. $options[self::OSS_OBJECT] = '/';
  2364. $options[self::OSS_SUB_RESOURCE] = 'uploads';
  2365. foreach (array('delimiter', 'key-marker', 'max-uploads', 'prefix', 'upload-id-marker') as $param) {
  2366. if (isset($options[$param])) {
  2367. $options[self::OSS_QUERY_STRING][$param] = $options[$param];
  2368. unset($options[$param]);
  2369. }
  2370. }
  2371. $query = isset($options[self::OSS_QUERY_STRING]) ? $options[self::OSS_QUERY_STRING] : array();
  2372. $options[self::OSS_QUERY_STRING] = array_merge(
  2373. $query,
  2374. array(self::OSS_ENCODING_TYPE => self::OSS_ENCODING_TYPE_URL)
  2375. );
  2376. $response = $this->auth($options);
  2377. $result = new ListMultipartUploadResult($response);
  2378. return $result->getData();
  2379. }
  2380. /**
  2381. * Copy an existing file as a part
  2382. *
  2383. * @param string $fromBucket source bucket name
  2384. * @param string $fromObject source object name
  2385. * @param string $toBucket target bucket name
  2386. * @param string $toObject target object name
  2387. * @param int $partNumber Part number
  2388. * @param string $uploadId Upload Id
  2389. * @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.
  2390. * @return null
  2391. * @throws OssException
  2392. */
  2393. public function uploadPartCopy($fromBucket, $fromObject, $toBucket, $toObject, $partNumber, $uploadId, $options = NULL)
  2394. {
  2395. $this->precheckCommon($fromBucket, $fromObject, $options);
  2396. $this->precheckCommon($toBucket, $toObject, $options);
  2397. //If $options['isFullCopy'] is not set, copy from the beginning
  2398. $start_range = "0";
  2399. if (isset($options['start'])) {
  2400. $start_range = $options['start'];
  2401. }
  2402. $end_range = "";
  2403. if (isset($options['end'])) {
  2404. $end_range = $options['end'];
  2405. }
  2406. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  2407. $options[self::OSS_BUCKET] = $toBucket;
  2408. $options[self::OSS_OBJECT] = $toObject;
  2409. $options[self::OSS_PART_NUM] = $partNumber;
  2410. $options[self::OSS_UPLOAD_ID] = $uploadId;
  2411. if (!isset($options[self::OSS_HEADERS])) {
  2412. $options[self::OSS_HEADERS] = array();
  2413. }
  2414. $param = '/' . $fromBucket . '/' . rawurlencode($fromObject);
  2415. if (isset($options[self::OSS_VERSION_ID])) {
  2416. $param = $param . '?versionId='.$options[self::OSS_VERSION_ID];
  2417. unset($options[self::OSS_VERSION_ID]);
  2418. }
  2419. $options[self::OSS_HEADERS][self::OSS_OBJECT_COPY_SOURCE] = $param;
  2420. $options[self::OSS_HEADERS][self::OSS_OBJECT_COPY_SOURCE_RANGE] = "bytes=" . $start_range . "-" . $end_range;
  2421. $response = $this->auth($options);
  2422. $result = new UploadPartResult($response);
  2423. return $result->getData();
  2424. }
  2425. /**
  2426. * A higher level API for uploading a file with multipart upload. It consists of initialization, parts upload and completion.
  2427. *
  2428. * @param string $bucket bucket name
  2429. * @param string $object object name
  2430. * @param string $file The local file to upload
  2431. * @param array $options Key-Value array
  2432. * @return null
  2433. * @throws OssException
  2434. */
  2435. public function multiuploadFile($bucket, $object, $file, $options = null)
  2436. {
  2437. $this->precheckCommon($bucket, $object, $options);
  2438. if (isset($options[self::OSS_LENGTH])) {
  2439. $options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH];
  2440. unset($options[self::OSS_LENGTH]);
  2441. }
  2442. if (empty($file)) {
  2443. throw new OssException("parameter invalid, file is empty");
  2444. }
  2445. $uploadFile = OssUtil::encodePath($file);
  2446. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  2447. $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object, $uploadFile);
  2448. }
  2449. $upload_position = isset($options[self::OSS_SEEK_TO]) ? (integer)$options[self::OSS_SEEK_TO] : 0;
  2450. if (isset($options[self::OSS_CONTENT_LENGTH])) {
  2451. $upload_file_size = (integer)$options[self::OSS_CONTENT_LENGTH];
  2452. } else {
  2453. $upload_file_size = sprintf('%u',filesize($uploadFile));
  2454. if ($upload_file_size !== false) {
  2455. $upload_file_size -= $upload_position;
  2456. }
  2457. }
  2458. if ($upload_position === false || !isset($upload_file_size) || $upload_file_size === false || $upload_file_size < 0) {
  2459. throw new OssException('The size of `fileUpload` cannot be determined in ' . __FUNCTION__ . '().');
  2460. }
  2461. // Computes the part size and assign it to options.
  2462. if (isset($options[self::OSS_PART_SIZE])) {
  2463. $options[self::OSS_PART_SIZE] = $this->computePartSize($options[self::OSS_PART_SIZE]);
  2464. } else {
  2465. $options[self::OSS_PART_SIZE] = self::OSS_MID_PART_SIZE;
  2466. }
  2467. $is_check_md5 = $this->isCheckMD5($options);
  2468. // if the file size is less than part size, use simple file upload.
  2469. if ($upload_file_size < $options[self::OSS_PART_SIZE] && !isset($options[self::OSS_UPLOAD_ID])) {
  2470. return $this->uploadFile($bucket, $object, $uploadFile, $options);
  2471. }
  2472. // Using multipart upload, initialize if no OSS_UPLOAD_ID is specified in options.
  2473. if (isset($options[self::OSS_UPLOAD_ID])) {
  2474. $uploadId = $options[self::OSS_UPLOAD_ID];
  2475. } else {
  2476. // initialize
  2477. $uploadId = $this->initiateMultipartUpload($bucket, $object, $options);
  2478. }
  2479. // generates the parts information and upload them one by one
  2480. $pieces = $this->generateMultiuploadParts($upload_file_size, (integer)$options[self::OSS_PART_SIZE]);
  2481. $response_upload_part = array();
  2482. foreach ($pieces as $i => $piece) {
  2483. $from_pos = $upload_position + (integer)$piece[self::OSS_SEEK_TO];
  2484. $to_pos = (integer)$piece[self::OSS_LENGTH] + $from_pos - 1;
  2485. $up_options = array(
  2486. self::OSS_FILE_UPLOAD => $uploadFile,
  2487. self::OSS_PART_NUM => ($i + 1),
  2488. self::OSS_SEEK_TO => $from_pos,
  2489. self::OSS_LENGTH => $to_pos - $from_pos + 1,
  2490. self::OSS_CHECK_MD5 => $is_check_md5,
  2491. );
  2492. if ($is_check_md5) {
  2493. $content_md5 = OssUtil::getMd5SumForFile($uploadFile, $from_pos, $to_pos);
  2494. $up_options[self::OSS_CONTENT_MD5] = $content_md5;
  2495. }
  2496. $response_upload_part[] = $this->uploadPart($bucket, $object, $uploadId, $up_options);
  2497. }
  2498. $uploadParts = array();
  2499. foreach ($response_upload_part as $i => $etag) {
  2500. $uploadParts[] = array(
  2501. 'PartNumber' => ($i + 1),
  2502. 'ETag' => $etag,
  2503. );
  2504. }
  2505. //build complete options
  2506. $cmp_options = null;
  2507. if (isset($options[self::OSS_HEADERS]) && isset($options[self::OSS_HEADERS][self::OSS_REQUEST_PAYER])) {
  2508. $cmp_options = array(
  2509. OssClient::OSS_HEADERS => array(
  2510. OssClient::OSS_REQUEST_PAYER => $options[self::OSS_HEADERS][self::OSS_REQUEST_PAYER],
  2511. ));
  2512. }
  2513. return $this->completeMultipartUpload($bucket, $object, $uploadId, $uploadParts, $cmp_options);
  2514. }
  2515. /**
  2516. * Uploads the local directory to the specified bucket into specified folder (prefix)
  2517. *
  2518. * @param string $bucket bucket name
  2519. * @param string $prefix The object key prefix. Typically it's folder name. The name should not end with '/' as the API appends it automatically.
  2520. * @param string $localDirectory The local directory to upload
  2521. * @param string $exclude To excluded directories
  2522. * @param bool $recursive Recursive flag. True: Recursively upload all datas under the local directory; False: only upload first layer's files.
  2523. * @param bool $checkMd5
  2524. * @return array Returns two list: array("succeededList" => array("object"), "failedList" => array("object"=>"errorMessage"))
  2525. * @throws OssException
  2526. */
  2527. public function uploadDir($bucket, $prefix, $localDirectory, $exclude = '.|..|.svn|.git', $recursive = false, $checkMd5 = true)
  2528. {
  2529. $retArray = array("succeededList" => array(), "failedList" => array());
  2530. if (empty($bucket)) throw new OssException("parameter error, bucket is empty");
  2531. if (!is_string($prefix)) throw new OssException("parameter error, prefix is not string");
  2532. if (empty($localDirectory)) throw new OssException("parameter error, localDirectory is empty");
  2533. $directory = $localDirectory;
  2534. $directory = OssUtil::encodePath($directory);
  2535. //If it's not the local directory, throw OSSException.
  2536. if (!is_dir($directory)) {
  2537. throw new OssException('parameter error: ' . $directory . ' is not a directory, please check it');
  2538. }
  2539. //read directory
  2540. $file_list_array = OssUtil::readDir($directory, $exclude, $recursive);
  2541. if (!$file_list_array) {
  2542. throw new OssException($directory . ' is empty...');
  2543. }
  2544. foreach ($file_list_array as $k => $item) {
  2545. if (is_dir($item['path'])) {
  2546. continue;
  2547. }
  2548. $options = array(
  2549. self::OSS_PART_SIZE => self::OSS_MIN_PART_SIZE,
  2550. self::OSS_CHECK_MD5 => $checkMd5,
  2551. );
  2552. $realObject = (!empty($prefix) ? $prefix . '/' : '') . $item['file'];
  2553. try {
  2554. $this->multiuploadFile($bucket, $realObject, $item['path'], $options);
  2555. $retArray["succeededList"][] = $realObject;
  2556. } catch (OssException $e) {
  2557. $retArray["failedList"][$realObject] = $e->getMessage();
  2558. }
  2559. }
  2560. return $retArray;
  2561. }
  2562. /**
  2563. * Sign URL with specified expiration time in seconds (timeout) and HTTP method.
  2564. * The signed URL could be used to access the object directly.
  2565. *
  2566. * @param string $bucket
  2567. * @param string $object
  2568. * @param int $timeout expiration time in seconds.
  2569. * @param string $method
  2570. * @param array $options Key-Value array
  2571. * @return string
  2572. * @throws OssException
  2573. */
  2574. public function signUrl($bucket, $object, $timeout = 60, $method = self::OSS_HTTP_GET, $options = NULL)
  2575. {
  2576. $this->precheckCommon($bucket, $object, $options);
  2577. //method
  2578. if (self::OSS_HTTP_GET !== $method && self::OSS_HTTP_PUT !== $method) {
  2579. throw new OssException("method is invalid");
  2580. }
  2581. $options[self::OSS_BUCKET] = $bucket;
  2582. $options[self::OSS_OBJECT] = $object;
  2583. $options[self::OSS_METHOD] = $method;
  2584. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  2585. $options[self::OSS_CONTENT_TYPE] = '';
  2586. }
  2587. $timeout = time() + $timeout;
  2588. $options[self::OSS_PREAUTH] = $timeout;
  2589. $options[self::OSS_DATE] = $timeout;
  2590. $this->setSignStsInUrl(true);
  2591. return $this->auth($options);
  2592. }
  2593. /**
  2594. * Sign URL with specified expiration time in seconds and HTTP method.
  2595. * The signed URL could be used to access the object directly.
  2596. *
  2597. * @param string $bucket
  2598. * @param string $object
  2599. * @param int $expiration expiration time of the Url, unix epoch, since 1970.1.1 00.00.00 UTC
  2600. * @param string $method
  2601. * @param array $options Key-Value array
  2602. * @return string
  2603. * @throws OssException
  2604. */
  2605. public function generatePresignedUrl($bucket, $object, $expiration, $method = self::OSS_HTTP_GET, $options = NULL)
  2606. {
  2607. $this->precheckCommon($bucket, $object, $options);
  2608. //method
  2609. if (self::OSS_HTTP_GET !== $method && self::OSS_HTTP_PUT !== $method) {
  2610. throw new OssException("method is invalid");
  2611. }
  2612. $options[self::OSS_BUCKET] = $bucket;
  2613. $options[self::OSS_OBJECT] = $object;
  2614. $options[self::OSS_METHOD] = $method;
  2615. if (!isset($options[self::OSS_CONTENT_TYPE])) {
  2616. $options[self::OSS_CONTENT_TYPE] = '';
  2617. }
  2618. $options[self::OSS_PREAUTH] = $expiration;
  2619. $options[self::OSS_DATE] = $expiration;
  2620. $this->setSignStsInUrl(true);
  2621. return $this->auth($options);
  2622. }
  2623. /**
  2624. * validates options. Create a empty array if it's NULL.
  2625. *
  2626. * @param array $options
  2627. * @throws OssException
  2628. */
  2629. private function precheckOptions(&$options)
  2630. {
  2631. OssUtil::validateOptions($options);
  2632. if (!$options) {
  2633. $options = array();
  2634. }
  2635. }
  2636. /**
  2637. * Validates bucket parameter
  2638. *
  2639. * @param string $bucket
  2640. * @param string $errMsg
  2641. * @throws OssException
  2642. */
  2643. private function precheckBucket($bucket, $errMsg = 'bucket is not allowed empty')
  2644. {
  2645. OssUtil::throwOssExceptionWithMessageIfEmpty($bucket, $errMsg);
  2646. }
  2647. /**
  2648. * validates object parameter
  2649. *
  2650. * @param string $object
  2651. * @throws OssException
  2652. */
  2653. private function precheckObject($object)
  2654. {
  2655. OssUtil::throwOssExceptionWithMessageIfEmpty($object, "object name is empty");
  2656. }
  2657. /**
  2658. * 校验option restore
  2659. *
  2660. * @param string $restore
  2661. * @throws OssException
  2662. */
  2663. private function precheckStorage($storage)
  2664. {
  2665. if (is_string($storage)) {
  2666. switch ($storage) {
  2667. case self::OSS_STORAGE_ARCHIVE:
  2668. return;
  2669. case self::OSS_STORAGE_IA:
  2670. return;
  2671. case self::OSS_STORAGE_STANDARD:
  2672. return;
  2673. case self::OSS_STORAGE_COLDARCHIVE:
  2674. return;
  2675. default:
  2676. break;
  2677. }
  2678. }
  2679. throw new OssException('storage name is invalid');
  2680. }
  2681. /**
  2682. * Validates bucket,options parameters and optionally validate object parameter.
  2683. *
  2684. * @param string $bucket
  2685. * @param string $object
  2686. * @param array $options
  2687. * @param bool $isCheckObject
  2688. */
  2689. private function precheckCommon($bucket, $object, &$options, $isCheckObject = true)
  2690. {
  2691. if ($isCheckObject) {
  2692. $this->precheckObject($object);
  2693. }
  2694. $this->precheckOptions($options);
  2695. $this->precheckBucket($bucket);
  2696. }
  2697. /**
  2698. * checks parameters
  2699. *
  2700. * @param array $options
  2701. * @param string $param
  2702. * @param string $funcName
  2703. * @throws OssException
  2704. */
  2705. private function precheckParam($options, $param, $funcName)
  2706. {
  2707. if (!isset($options[$param])) {
  2708. throw new OssException('The `' . $param . '` options is required in ' . $funcName . '().');
  2709. }
  2710. }
  2711. /**
  2712. * Checks md5
  2713. *
  2714. * @param array $options
  2715. * @return bool|null
  2716. */
  2717. private function isCheckMD5($options)
  2718. {
  2719. return $this->getValue($options, self::OSS_CHECK_MD5, false, true, true);
  2720. }
  2721. /**
  2722. * Gets value of the specified key from the options
  2723. *
  2724. * @param array $options
  2725. * @param string $key
  2726. * @param string $default
  2727. * @param bool $isCheckEmpty
  2728. * @param bool $isCheckBool
  2729. * @return bool|null
  2730. */
  2731. private function getValue($options, $key, $default = NULL, $isCheckEmpty = false, $isCheckBool = false)
  2732. {
  2733. $value = $default;
  2734. if (isset($options[$key])) {
  2735. if ($isCheckEmpty) {
  2736. if (!empty($options[$key])) {
  2737. $value = $options[$key];
  2738. }
  2739. } else {
  2740. $value = $options[$key];
  2741. }
  2742. unset($options[$key]);
  2743. }
  2744. if ($isCheckBool) {
  2745. if ($value !== true && $value !== false) {
  2746. $value = false;
  2747. }
  2748. }
  2749. return $value;
  2750. }
  2751. /**
  2752. * Gets mimetype
  2753. *
  2754. * @param string $object
  2755. * @return string
  2756. */
  2757. private function getMimeType($object, $file = null)
  2758. {
  2759. if (!is_null($file)) {
  2760. $type = MimeTypes::getMimetype($file);
  2761. if (!is_null($type)) {
  2762. return $type;
  2763. }
  2764. }
  2765. $type = MimeTypes::getMimetype($object);
  2766. if (!is_null($type)) {
  2767. return $type;
  2768. }
  2769. return self::DEFAULT_CONTENT_TYPE;
  2770. }
  2771. /**
  2772. * Validates and executes the request according to OSS API protocol.
  2773. *
  2774. * @param array $options
  2775. * @return ResponseCore
  2776. * @throws OssException
  2777. * @throws RequestCore_Exception
  2778. */
  2779. private function auth($options)
  2780. {
  2781. OssUtil::validateOptions($options);
  2782. //Validates bucket, not required for list_bucket
  2783. $this->authPrecheckBucket($options);
  2784. //Validates object
  2785. $this->authPrecheckObject($options);
  2786. //object name encoding must be UTF-8
  2787. $this->authPrecheckObjectEncoding($options);
  2788. //Validates ACL
  2789. $this->authPrecheckAcl($options);
  2790. // Should https or http be used?
  2791. $scheme = $this->useSSL ? 'https://' : 'http://';
  2792. // 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.
  2793. $hostname = $this->generateHostname($options[self::OSS_BUCKET]);
  2794. $string_to_sign = '';
  2795. $headers = $this->generateHeaders($options, $hostname);
  2796. $signable_query_string_params = $this->generateSignableQueryStringParam($options);
  2797. $signable_query_string = OssUtil::toQueryString($signable_query_string_params);
  2798. $resource_uri = $this->generateResourceUri($options);
  2799. //Generates the URL (add query parameters)
  2800. $conjunction = '?';
  2801. $non_signable_resource = '';
  2802. if (isset($options[self::OSS_SUB_RESOURCE])) {
  2803. $conjunction = '&';
  2804. }
  2805. if ($signable_query_string !== '') {
  2806. $signable_query_string = $conjunction . $signable_query_string;
  2807. $conjunction = '&';
  2808. }
  2809. $query_string = $this->generateQueryString($options);
  2810. if ($query_string !== '') {
  2811. $non_signable_resource .= $conjunction . $query_string;
  2812. $conjunction = '&';
  2813. }
  2814. $requestUrl = $scheme . $hostname . $resource_uri . $signable_query_string . $non_signable_resource;
  2815. //Creates the request
  2816. $request = new RequestCore($requestUrl, $this->requestProxy);
  2817. $request->set_useragent($this->generateUserAgent());
  2818. // Streaming uploads
  2819. if (isset($options[self::OSS_FILE_UPLOAD])) {
  2820. if (is_resource($options[self::OSS_FILE_UPLOAD])) {
  2821. $length = null;
  2822. if (isset($options[self::OSS_CONTENT_LENGTH])) {
  2823. $length = $options[self::OSS_CONTENT_LENGTH];
  2824. } elseif (isset($options[self::OSS_SEEK_TO])) {
  2825. $stats = fstat($options[self::OSS_FILE_UPLOAD]);
  2826. if ($stats && $stats[self::OSS_SIZE] >= 0) {
  2827. $length = $stats[self::OSS_SIZE] - (integer)$options[self::OSS_SEEK_TO];
  2828. }
  2829. }
  2830. $request->set_read_stream($options[self::OSS_FILE_UPLOAD], $length);
  2831. } else {
  2832. $request->set_read_file($options[self::OSS_FILE_UPLOAD]);
  2833. $length = $request->read_stream_size;
  2834. if (isset($options[self::OSS_CONTENT_LENGTH])) {
  2835. $length = $options[self::OSS_CONTENT_LENGTH];
  2836. } elseif (isset($options[self::OSS_SEEK_TO]) && isset($length)) {
  2837. $length -= (integer)$options[self::OSS_SEEK_TO];
  2838. }
  2839. $request->set_read_stream_size($length);
  2840. }
  2841. }
  2842. if (isset($options[self::OSS_SEEK_TO])) {
  2843. $request->set_seek_position((integer)$options[self::OSS_SEEK_TO]);
  2844. }
  2845. if (isset($options[self::OSS_FILE_DOWNLOAD])) {
  2846. if (is_resource($options[self::OSS_FILE_DOWNLOAD])) {
  2847. $request->set_write_stream($options[self::OSS_FILE_DOWNLOAD]);
  2848. } else {
  2849. $request->set_write_file($options[self::OSS_FILE_DOWNLOAD]);
  2850. }
  2851. }
  2852. if (isset($options[self::OSS_METHOD])) {
  2853. $request->set_method($options[self::OSS_METHOD]);
  2854. $string_to_sign .= $options[self::OSS_METHOD] . "\n";
  2855. }
  2856. if (isset($options[self::OSS_CONTENT])) {
  2857. $request->set_body($options[self::OSS_CONTENT]);
  2858. if ($headers[self::OSS_CONTENT_TYPE] === 'application/x-www-form-urlencoded') {
  2859. $headers[self::OSS_CONTENT_TYPE] = 'application/octet-stream';
  2860. }
  2861. $headers[self::OSS_CONTENT_LENGTH] = strlen($options[self::OSS_CONTENT]);
  2862. $headers[self::OSS_CONTENT_MD5] = base64_encode(md5($options[self::OSS_CONTENT], true));
  2863. }
  2864. if (isset($options[self::OSS_CALLBACK])) {
  2865. $headers[self::OSS_CALLBACK] = base64_encode($options[self::OSS_CALLBACK]);
  2866. }
  2867. if (isset($options[self::OSS_CALLBACK_VAR])) {
  2868. $headers[self::OSS_CALLBACK_VAR] = base64_encode($options[self::OSS_CALLBACK_VAR]);
  2869. }
  2870. if (!isset($headers[self::OSS_ACCEPT_ENCODING])) {
  2871. $headers[self::OSS_ACCEPT_ENCODING] = '';
  2872. }
  2873. uksort($headers, 'strnatcasecmp');
  2874. foreach ($headers as $header_key => $header_value) {
  2875. $header_value = str_replace(array("\r", "\n"), '', $header_value);
  2876. if ($header_value !== '' || $header_key === self::OSS_ACCEPT_ENCODING) {
  2877. $request->add_header($header_key, $header_value);
  2878. }
  2879. if (
  2880. strtolower($header_key) === 'content-md5' ||
  2881. strtolower($header_key) === 'content-type' ||
  2882. strtolower($header_key) === 'date' ||
  2883. (isset($options['self::OSS_PREAUTH']) && (integer)$options['self::OSS_PREAUTH'] > 0)
  2884. ) {
  2885. $string_to_sign .= $header_value . "\n";
  2886. } elseif (substr(strtolower($header_key), 0, 6) === self::OSS_DEFAULT_PREFIX) {
  2887. $string_to_sign .= strtolower($header_key) . ':' . $header_value . "\n";
  2888. }
  2889. }
  2890. // Generates the signable_resource
  2891. $signable_resource = $this->generateSignableResource($options);
  2892. $signable_resource = rawurldecode($signable_resource) . urldecode($signable_query_string);
  2893. $string_to_sign_ordered = $string_to_sign;
  2894. $string_to_sign .= $signable_resource;
  2895. // Sort the strings to be signed.
  2896. $string_to_sign_ordered .= $this->stringToSignSorted($signable_resource);
  2897. $signature = base64_encode(hash_hmac('sha1', $string_to_sign_ordered, $this->accessKeySecret, true));
  2898. $request->add_header('Authorization', 'OSS ' . $this->accessKeyId . ':' . $signature);
  2899. if (isset($options[self::OSS_PREAUTH]) && (integer)$options[self::OSS_PREAUTH] > 0) {
  2900. $signed_url = $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);
  2901. return $signed_url;
  2902. } elseif (isset($options[self::OSS_PREAUTH])) {
  2903. return $requestUrl;
  2904. }
  2905. if ($this->timeout !== 0) {
  2906. $request->timeout = $this->timeout;
  2907. }
  2908. if ($this->connectTimeout !== 0) {
  2909. $request->connect_timeout = $this->connectTimeout;
  2910. }
  2911. try {
  2912. $request->send_request();
  2913. } catch (RequestCore_Exception $e) {
  2914. throw(new OssException('RequestCoreException: ' . $e->getMessage()));
  2915. }
  2916. $response_header = $request->get_response_header();
  2917. $response_header['oss-request-url'] = $requestUrl;
  2918. $response_header['oss-redirects'] = $this->redirects;
  2919. $response_header['oss-stringtosign'] = $string_to_sign;
  2920. $response_header['oss-requestheaders'] = $request->request_headers;
  2921. $data = new ResponseCore($response_header, $request->get_response_body(), $request->get_response_code());
  2922. //retry if OSS Internal Error
  2923. if ((integer)$request->get_response_code() === 500) {
  2924. if ($this->redirects <= $this->maxRetries) {
  2925. //Sets the sleep time betwen each retry.
  2926. $delay = (integer)(pow(4, $this->redirects) * 100000);
  2927. usleep($delay);
  2928. $this->redirects++;
  2929. $data = $this->auth($options);
  2930. }
  2931. }
  2932. $this->redirects = 0;
  2933. return $data;
  2934. }
  2935. /**
  2936. * Sets the max retry count
  2937. *
  2938. * @param int $maxRetries
  2939. * @return void
  2940. */
  2941. public function setMaxTries($maxRetries = 3)
  2942. {
  2943. $this->maxRetries = $maxRetries;
  2944. }
  2945. /**
  2946. * Gets the max retry count
  2947. *
  2948. * @return int
  2949. */
  2950. public function getMaxRetries()
  2951. {
  2952. return $this->maxRetries;
  2953. }
  2954. /**
  2955. * Enaable/disable STS in the URL. This is to determine the $sts value passed from constructor take effect or not.
  2956. *
  2957. * @param boolean $enable
  2958. */
  2959. public function setSignStsInUrl($enable)
  2960. {
  2961. $this->enableStsInUrl = $enable;
  2962. }
  2963. /**
  2964. * @return boolean
  2965. */
  2966. public function isUseSSL()
  2967. {
  2968. return $this->useSSL;
  2969. }
  2970. /**
  2971. * @param boolean $useSSL
  2972. */
  2973. public function setUseSSL($useSSL)
  2974. {
  2975. $this->useSSL = $useSSL;
  2976. }
  2977. /**
  2978. * Validates bucket name--throw OssException if it's invalid
  2979. *
  2980. * @param $options
  2981. * @throws OssException
  2982. */
  2983. private function authPrecheckBucket($options)
  2984. {
  2985. if (!(('/' == $options[self::OSS_OBJECT]) && ('' == $options[self::OSS_BUCKET]) && ('GET' == $options[self::OSS_METHOD])) && !OssUtil::validateBucket($options[self::OSS_BUCKET])) {
  2986. throw new OssException('"' . $options[self::OSS_BUCKET] . '"' . 'bucket name is invalid');
  2987. }
  2988. }
  2989. /**
  2990. *
  2991. * Validates the object name--throw OssException if it's invalid.
  2992. *
  2993. * @param $options
  2994. * @throws OssException
  2995. */
  2996. private function authPrecheckObject($options)
  2997. {
  2998. if (isset($options[self::OSS_OBJECT]) && $options[self::OSS_OBJECT] === '/') {
  2999. return;
  3000. }
  3001. if (isset($options[self::OSS_OBJECT]) && !OssUtil::validateObject($options[self::OSS_OBJECT])) {
  3002. throw new OssException('"' . $options[self::OSS_OBJECT] . '"' . ' object name is invalid');
  3003. }
  3004. }
  3005. /**
  3006. * Checks the object's encoding. Convert it to UTF8 if it's in GBK or GB2312
  3007. *
  3008. * @param mixed $options parameter
  3009. */
  3010. private function authPrecheckObjectEncoding(&$options)
  3011. {
  3012. $tmp_object = $options[self::OSS_OBJECT];
  3013. try {
  3014. if (OssUtil::isGb2312($options[self::OSS_OBJECT])) {
  3015. $options[self::OSS_OBJECT] = iconv('GB2312', "UTF-8//IGNORE", $options[self::OSS_OBJECT]);
  3016. } elseif (OssUtil::checkChar($options[self::OSS_OBJECT], true)) {
  3017. $options[self::OSS_OBJECT] = iconv('GBK', "UTF-8//IGNORE", $options[self::OSS_OBJECT]);
  3018. }
  3019. } catch (\Exception $e) {
  3020. try {
  3021. $tmp_object = iconv(mb_detect_encoding($tmp_object), "UTF-8", $tmp_object);
  3022. } catch (\Exception $e) {
  3023. }
  3024. }
  3025. $options[self::OSS_OBJECT] = $tmp_object;
  3026. }
  3027. /**
  3028. * Checks if the ACL is one of the 3 predefined one. Throw OSSException if not.
  3029. *
  3030. * @param $options
  3031. * @throws OssException
  3032. */
  3033. private function authPrecheckAcl($options)
  3034. {
  3035. if (isset($options[self::OSS_HEADERS][self::OSS_ACL]) && !empty($options[self::OSS_HEADERS][self::OSS_ACL])) {
  3036. if (!in_array(strtolower($options[self::OSS_HEADERS][self::OSS_ACL]), self::$OSS_ACL_TYPES)) {
  3037. throw new OssException($options[self::OSS_HEADERS][self::OSS_ACL] . ':' . 'acl is invalid(private,public-read,public-read-write)');
  3038. }
  3039. }
  3040. }
  3041. /**
  3042. * Gets the host name for the current request.
  3043. * It could be either a third level domain (prefixed by bucket name) or second level domain if it's CName or IP
  3044. *
  3045. * @param $bucket
  3046. * @return string The host name without the protocol scheem (e.g. https://)
  3047. */
  3048. private function generateHostname($bucket)
  3049. {
  3050. if ($this->hostType === self::OSS_HOST_TYPE_IP) {
  3051. $hostname = $this->hostname;
  3052. } elseif ($this->hostType === self::OSS_HOST_TYPE_CNAME) {
  3053. $hostname = $this->hostname;
  3054. } else {
  3055. // Private domain or public domain
  3056. $hostname = ($bucket == '') ? $this->hostname : ($bucket . '.') . $this->hostname;
  3057. }
  3058. return $hostname;
  3059. }
  3060. /**
  3061. * Gets the resource Uri in the current request
  3062. *
  3063. * @param $options
  3064. * @return string return the resource uri.
  3065. */
  3066. private function generateResourceUri($options)
  3067. {
  3068. $resource_uri = "";
  3069. // resource_uri + bucket
  3070. if (isset($options[self::OSS_BUCKET]) && '' !== $options[self::OSS_BUCKET]) {
  3071. if ($this->hostType === self::OSS_HOST_TYPE_IP) {
  3072. $resource_uri = '/' . $options[self::OSS_BUCKET];
  3073. }
  3074. }
  3075. // resource_uri + object
  3076. if (isset($options[self::OSS_OBJECT]) && '/' !== $options[self::OSS_OBJECT]) {
  3077. $resource_uri .= '/' . str_replace(array('%2F', '%25'), array('/', '%'), rawurlencode($options[self::OSS_OBJECT]));
  3078. }
  3079. // resource_uri + sub_resource
  3080. $conjunction = '?';
  3081. if (isset($options[self::OSS_SUB_RESOURCE])) {
  3082. $resource_uri .= $conjunction . $options[self::OSS_SUB_RESOURCE];
  3083. }
  3084. return $resource_uri;
  3085. }
  3086. /**
  3087. * Generates the signalbe query string parameters in array type
  3088. *
  3089. * @param array $options
  3090. * @return array
  3091. */
  3092. private function generateSignableQueryStringParam($options)
  3093. {
  3094. $signableQueryStringParams = array();
  3095. $signableList = array(
  3096. self::OSS_PART_NUM,
  3097. 'response-content-type',
  3098. 'response-content-language',
  3099. 'response-cache-control',
  3100. 'response-content-encoding',
  3101. 'response-expires',
  3102. 'response-content-disposition',
  3103. self::OSS_UPLOAD_ID,
  3104. self::OSS_COMP,
  3105. self::OSS_LIVE_CHANNEL_STATUS,
  3106. self::OSS_LIVE_CHANNEL_START_TIME,
  3107. self::OSS_LIVE_CHANNEL_END_TIME,
  3108. self::OSS_PROCESS,
  3109. self::OSS_POSITION,
  3110. self::OSS_SYMLINK,
  3111. self::OSS_RESTORE,
  3112. self::OSS_TAGGING,
  3113. self::OSS_WORM_ID,
  3114. self::OSS_TRAFFIC_LIMIT,
  3115. self::OSS_VERSION_ID,
  3116. self::OSS_CONTINUATION_TOKEN,
  3117. self::OSS_CNAME,
  3118. );
  3119. foreach ($signableList as $item) {
  3120. if (isset($options[$item])) {
  3121. $signableQueryStringParams[$item] = $options[$item];
  3122. }
  3123. }
  3124. if ($this->enableStsInUrl && (!is_null($this->securityToken))) {
  3125. $signableQueryStringParams["security-token"] = $this->securityToken;
  3126. }
  3127. return $signableQueryStringParams;
  3128. }
  3129. /**
  3130. * Generates the resource uri for signing
  3131. *
  3132. * @param mixed $options
  3133. * @return string
  3134. */
  3135. private function generateSignableResource($options)
  3136. {
  3137. $signableResource = "";
  3138. $signableResource .= '/';
  3139. if (isset($options[self::OSS_BUCKET]) && '' !== $options[self::OSS_BUCKET]) {
  3140. $signableResource .= $options[self::OSS_BUCKET];
  3141. // if there's no object in options, adding a '/' if the host type is not IP.\
  3142. if ($options[self::OSS_OBJECT] == '/') {
  3143. if ($this->hostType !== self::OSS_HOST_TYPE_IP) {
  3144. $signableResource .= "/";
  3145. }
  3146. }
  3147. }
  3148. //signable_resource + object
  3149. if (isset($options[self::OSS_OBJECT]) && '/' !== $options[self::OSS_OBJECT]) {
  3150. $signableResource .= '/' . str_replace(array('%2F', '%25'), array('/', '%'), rawurlencode($options[self::OSS_OBJECT]));
  3151. }
  3152. if (isset($options[self::OSS_SUB_RESOURCE])) {
  3153. $signableResource .= '?' . $options[self::OSS_SUB_RESOURCE];
  3154. }
  3155. return $signableResource;
  3156. }
  3157. /**
  3158. * generates query string
  3159. *
  3160. * @param mixed $options
  3161. * @return string
  3162. */
  3163. private function generateQueryString($options)
  3164. {
  3165. //query parameters
  3166. $queryStringParams = array();
  3167. if (isset($options[self::OSS_QUERY_STRING])) {
  3168. $queryStringParams = array_merge($queryStringParams, $options[self::OSS_QUERY_STRING]);
  3169. }
  3170. return OssUtil::toQueryString($queryStringParams);
  3171. }
  3172. private function stringToSignSorted($string_to_sign)
  3173. {
  3174. $queryStringSorted = '';
  3175. $explodeResult = explode('?', $string_to_sign);
  3176. $index = count($explodeResult);
  3177. if ($index === 1)
  3178. return $string_to_sign;
  3179. $queryStringParams = explode('&', $explodeResult[$index - 1]);
  3180. sort($queryStringParams);
  3181. foreach($queryStringParams as $params)
  3182. {
  3183. $queryStringSorted .= $params . '&';
  3184. }
  3185. $queryStringSorted = substr($queryStringSorted, 0, -1);
  3186. $result = '';
  3187. for ($i = 0; $i < $index -1; $i++)
  3188. {
  3189. $result .= $explodeResult[$i] . '?';
  3190. }
  3191. return $result . $queryStringSorted;
  3192. }
  3193. /**
  3194. * Initialize headers
  3195. *
  3196. * @param mixed $options
  3197. * @param string $hostname hostname
  3198. * @return array
  3199. */
  3200. private function generateHeaders($options, $hostname)
  3201. {
  3202. $headers = array(
  3203. self::OSS_CONTENT_MD5 => '',
  3204. self::OSS_CONTENT_TYPE => isset($options[self::OSS_CONTENT_TYPE]) ? $options[self::OSS_CONTENT_TYPE] : self::DEFAULT_CONTENT_TYPE,
  3205. self::OSS_DATE => isset($options[self::OSS_DATE]) ? $options[self::OSS_DATE] : gmdate('D, d M Y H:i:s \G\M\T'),
  3206. self::OSS_HOST => $hostname,
  3207. );
  3208. if (isset($options[self::OSS_CONTENT_MD5])) {
  3209. $headers[self::OSS_CONTENT_MD5] = $options[self::OSS_CONTENT_MD5];
  3210. }
  3211. //Add stsSecurityToken
  3212. if ((!is_null($this->securityToken)) && (!$this->enableStsInUrl)) {
  3213. $headers[self::OSS_SECURITY_TOKEN] = $this->securityToken;
  3214. }
  3215. //Merge HTTP headers
  3216. if (isset($options[self::OSS_HEADERS])) {
  3217. $headers = array_merge($headers, $options[self::OSS_HEADERS]);
  3218. }
  3219. return $headers;
  3220. }
  3221. /**
  3222. * Generates UserAgent
  3223. *
  3224. * @return string
  3225. */
  3226. private function generateUserAgent()
  3227. {
  3228. return self::OSS_NAME . "/" . self::OSS_VERSION . " (" . php_uname('s') . "/" . php_uname('r') . "/" . php_uname('m') . ";" . PHP_VERSION . ")";
  3229. }
  3230. /**
  3231. * Checks endpoint type and returns the endpoint without the protocol schema.
  3232. * Figures out the domain's type (ip, cname or private/public domain).
  3233. *
  3234. * @param string $endpoint
  3235. * @param boolean $isCName
  3236. * @return string The domain name without the protocol schema.
  3237. */
  3238. private function checkEndpoint($endpoint, $isCName)
  3239. {
  3240. $ret_endpoint = null;
  3241. if (strpos($endpoint, 'http://') === 0) {
  3242. $ret_endpoint = substr($endpoint, strlen('http://'));
  3243. } elseif (strpos($endpoint, 'https://') === 0) {
  3244. $ret_endpoint = substr($endpoint, strlen('https://'));
  3245. $this->useSSL = true;
  3246. } else {
  3247. $ret_endpoint = $endpoint;
  3248. }
  3249. $ret_endpoint = OssUtil::getHostPortFromEndpoint($ret_endpoint);
  3250. if ($isCName) {
  3251. $this->hostType = self::OSS_HOST_TYPE_CNAME;
  3252. } elseif (OssUtil::isIPFormat($ret_endpoint)) {
  3253. $this->hostType = self::OSS_HOST_TYPE_IP;
  3254. } else {
  3255. $this->hostType = self::OSS_HOST_TYPE_NORMAL;
  3256. }
  3257. return $ret_endpoint;
  3258. }
  3259. /**
  3260. * Check if all dependent extensions are installed correctly.
  3261. * For now only "curl" is needed.
  3262. * @throws OssException
  3263. */
  3264. public static function checkEnv()
  3265. {
  3266. if (function_exists('get_loaded_extensions')) {
  3267. //Test curl extension
  3268. $enabled_extension = array("curl");
  3269. $extensions = get_loaded_extensions();
  3270. if ($extensions) {
  3271. foreach ($enabled_extension as $item) {
  3272. if (!in_array($item, $extensions)) {
  3273. throw new OssException("Extension {" . $item . "} is not installed or not enabled, please check your php env.");
  3274. }
  3275. }
  3276. } else {
  3277. throw new OssException("function get_loaded_extensions not found.");
  3278. }
  3279. } else {
  3280. throw new OssException('Function get_loaded_extensions has been disabled, please check php config.');
  3281. }
  3282. }
  3283. /**
  3284. * Sets the http's timeout (in seconds)
  3285. *
  3286. * @param int $timeout
  3287. */
  3288. public function setTimeout($timeout)
  3289. {
  3290. $this->timeout = $timeout;
  3291. }
  3292. /**
  3293. * Sets the http's connection timeout (in seconds)
  3294. *
  3295. * @param int $connectTimeout
  3296. */
  3297. public function setConnectTimeout($connectTimeout)
  3298. {
  3299. $this->connectTimeout = $connectTimeout;
  3300. }
  3301. // Constants for Life cycle
  3302. const OSS_LIFECYCLE_EXPIRATION = "Expiration";
  3303. const OSS_LIFECYCLE_TIMING_DAYS = "Days";
  3304. const OSS_LIFECYCLE_TIMING_DATE = "Date";
  3305. //OSS Internal constants
  3306. const OSS_BUCKET = 'bucket';
  3307. const OSS_OBJECT = 'object';
  3308. const OSS_HEADERS = OssUtil::OSS_HEADERS;
  3309. const OSS_METHOD = 'method';
  3310. const OSS_QUERY = 'query';
  3311. const OSS_BASENAME = 'basename';
  3312. const OSS_MAX_KEYS = 'max-keys';
  3313. const OSS_UPLOAD_ID = 'uploadId';
  3314. const OSS_PART_NUM = 'partNumber';
  3315. const OSS_COMP = 'comp';
  3316. const OSS_LIVE_CHANNEL_STATUS = 'status';
  3317. const OSS_LIVE_CHANNEL_START_TIME = 'startTime';
  3318. const OSS_LIVE_CHANNEL_END_TIME = 'endTime';
  3319. const OSS_POSITION = 'position';
  3320. const OSS_MAX_KEYS_VALUE = 100;
  3321. const OSS_MAX_OBJECT_GROUP_VALUE = OssUtil::OSS_MAX_OBJECT_GROUP_VALUE;
  3322. const OSS_MAX_PART_SIZE = OssUtil::OSS_MAX_PART_SIZE;
  3323. const OSS_MID_PART_SIZE = OssUtil::OSS_MID_PART_SIZE;
  3324. const OSS_MIN_PART_SIZE = OssUtil::OSS_MIN_PART_SIZE;
  3325. const OSS_FILE_SLICE_SIZE = 8192;
  3326. const OSS_PREFIX = 'prefix';
  3327. const OSS_DELIMITER = 'delimiter';
  3328. const OSS_MARKER = 'marker';
  3329. const OSS_FETCH_OWNER = 'fetch-owner';
  3330. const OSS_START_AFTER = 'start-after';
  3331. const OSS_CONTINUATION_TOKEN = 'continuation-token';
  3332. const OSS_ACCEPT_ENCODING = 'Accept-Encoding';
  3333. const OSS_CONTENT_MD5 = 'Content-Md5';
  3334. const OSS_SELF_CONTENT_MD5 = 'x-oss-meta-md5';
  3335. const OSS_CONTENT_TYPE = 'Content-Type';
  3336. const OSS_CONTENT_LENGTH = 'Content-Length';
  3337. const OSS_IF_MODIFIED_SINCE = 'If-Modified-Since';
  3338. const OSS_IF_UNMODIFIED_SINCE = 'If-Unmodified-Since';
  3339. const OSS_IF_MATCH = 'If-Match';
  3340. const OSS_IF_NONE_MATCH = 'If-None-Match';
  3341. const OSS_CACHE_CONTROL = 'Cache-Control';
  3342. const OSS_EXPIRES = 'Expires';
  3343. const OSS_PREAUTH = 'preauth';
  3344. const OSS_CONTENT_COING = 'Content-Coding';
  3345. const OSS_CONTENT_DISPOSTION = 'Content-Disposition';
  3346. const OSS_RANGE = 'range';
  3347. const OSS_ETAG = 'etag';
  3348. const OSS_LAST_MODIFIED = 'lastmodified';
  3349. const OS_CONTENT_RANGE = 'Content-Range';
  3350. const OSS_CONTENT = OssUtil::OSS_CONTENT;
  3351. const OSS_BODY = 'body';
  3352. const OSS_LENGTH = OssUtil::OSS_LENGTH;
  3353. const OSS_HOST = 'Host';
  3354. const OSS_DATE = 'Date';
  3355. const OSS_AUTHORIZATION = 'Authorization';
  3356. const OSS_FILE_DOWNLOAD = 'fileDownload';
  3357. const OSS_FILE_UPLOAD = 'fileUpload';
  3358. const OSS_PART_SIZE = 'partSize';
  3359. const OSS_SEEK_TO = 'seekTo';
  3360. const OSS_SIZE = 'size';
  3361. const OSS_QUERY_STRING = 'query_string';
  3362. const OSS_SUB_RESOURCE = 'sub_resource';
  3363. const OSS_DEFAULT_PREFIX = 'x-oss-';
  3364. const OSS_CHECK_MD5 = 'checkmd5';
  3365. const DEFAULT_CONTENT_TYPE = 'application/octet-stream';
  3366. const OSS_SYMLINK_TARGET = 'x-oss-symlink-target';
  3367. const OSS_SYMLINK = 'symlink';
  3368. const OSS_HTTP_CODE = 'http_code';
  3369. const OSS_REQUEST_ID = 'x-oss-request-id';
  3370. const OSS_INFO = 'info';
  3371. const OSS_STORAGE = 'storage';
  3372. const OSS_RESTORE = 'restore';
  3373. const OSS_STORAGE_STANDARD = 'Standard';
  3374. const OSS_STORAGE_IA = 'IA';
  3375. const OSS_STORAGE_ARCHIVE = 'Archive';
  3376. const OSS_STORAGE_COLDARCHIVE = 'ColdArchive';
  3377. const OSS_TAGGING = 'tagging';
  3378. const OSS_WORM_ID = 'wormId';
  3379. const OSS_RESTORE_CONFIG = 'restore-config';
  3380. const OSS_KEY_MARKER = 'key-marker';
  3381. const OSS_VERSION_ID_MARKER = 'version-id-marker';
  3382. const OSS_VERSION_ID = 'versionId';
  3383. const OSS_HEADER_VERSION_ID = 'x-oss-version-id';
  3384. const OSS_CNAME = 'cname';
  3385. //private URLs
  3386. const OSS_URL_ACCESS_KEY_ID = 'OSSAccessKeyId';
  3387. const OSS_URL_EXPIRES = 'Expires';
  3388. const OSS_URL_SIGNATURE = 'Signature';
  3389. //HTTP METHOD
  3390. const OSS_HTTP_GET = 'GET';
  3391. const OSS_HTTP_PUT = 'PUT';
  3392. const OSS_HTTP_HEAD = 'HEAD';
  3393. const OSS_HTTP_POST = 'POST';
  3394. const OSS_HTTP_DELETE = 'DELETE';
  3395. const OSS_HTTP_OPTIONS = 'OPTIONS';
  3396. //Others
  3397. const OSS_ACL = 'x-oss-acl';
  3398. const OSS_OBJECT_ACL = 'x-oss-object-acl';
  3399. const OSS_OBJECT_GROUP = 'x-oss-file-group';
  3400. const OSS_MULTI_PART = 'uploads';
  3401. const OSS_MULTI_DELETE = 'delete';
  3402. const OSS_OBJECT_COPY_SOURCE = 'x-oss-copy-source';
  3403. const OSS_OBJECT_COPY_SOURCE_RANGE = "x-oss-copy-source-range";
  3404. const OSS_PROCESS = "x-oss-process";
  3405. const OSS_CALLBACK = "x-oss-callback";
  3406. const OSS_CALLBACK_VAR = "x-oss-callback-var";
  3407. const OSS_REQUEST_PAYER = "x-oss-request-payer";
  3408. const OSS_TRAFFIC_LIMIT = "x-oss-traffic-limit";
  3409. //Constants for STS SecurityToken
  3410. const OSS_SECURITY_TOKEN = "x-oss-security-token";
  3411. const OSS_ACL_TYPE_PRIVATE = 'private';
  3412. const OSS_ACL_TYPE_PUBLIC_READ = 'public-read';
  3413. const OSS_ACL_TYPE_PUBLIC_READ_WRITE = 'public-read-write';
  3414. const OSS_ENCODING_TYPE = "encoding-type";
  3415. const OSS_ENCODING_TYPE_URL = "url";
  3416. const OSS_LIST_TYPE = "list-type";
  3417. // Domain Types
  3418. const OSS_HOST_TYPE_NORMAL = "normal";//http://bucket.oss-cn-hangzhou.aliyuncs.com/object
  3419. const OSS_HOST_TYPE_IP = "ip"; //http://1.1.1.1/bucket/object
  3420. const OSS_HOST_TYPE_SPECIAL = 'special'; //http://bucket.guizhou.gov/object
  3421. const OSS_HOST_TYPE_CNAME = "cname"; //http://mydomain.com/object
  3422. //OSS ACL array
  3423. static $OSS_ACL_TYPES = array(
  3424. self::OSS_ACL_TYPE_PRIVATE,
  3425. self::OSS_ACL_TYPE_PUBLIC_READ,
  3426. self::OSS_ACL_TYPE_PUBLIC_READ_WRITE
  3427. );
  3428. // OssClient version information
  3429. const OSS_NAME = "aliyun-sdk-php";
  3430. const OSS_VERSION = "2.5.0";
  3431. const OSS_BUILD = "20220513";
  3432. const OSS_AUTHOR = "";
  3433. const OSS_OPTIONS_ORIGIN = 'Origin';
  3434. const OSS_OPTIONS_REQUEST_METHOD = 'Access-Control-Request-Method';
  3435. const OSS_OPTIONS_REQUEST_HEADERS = 'Access-Control-Request-Headers';
  3436. //use ssl flag
  3437. private $useSSL = false;
  3438. private $maxRetries = 3;
  3439. private $redirects = 0;
  3440. // 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
  3441. private $hostType = self::OSS_HOST_TYPE_NORMAL;
  3442. private $requestProxy = null;
  3443. private $accessKeyId;
  3444. private $accessKeySecret;
  3445. private $hostname;
  3446. private $securityToken;
  3447. private $enableStsInUrl = false;
  3448. private $timeout = 0;
  3449. private $connectTimeout = 0;
  3450. }