OssClient.php 129 KB

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