sdk.class.php 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309
  1. <?php
  2. /**
  3. * OSS(Open Storage Services) PHP SDK v1.1.5
  4. */
  5. //设置默认时区
  6. date_default_timezone_set('Asia/Shanghai');
  7. //检测API路径
  8. if(!defined('OSS_API_PATH'))
  9. define('OSS_API_PATH', dirname(__FILE__));
  10. //加载conf.inc.php文件
  11. require_once OSS_API_PATH.DIRECTORY_SEPARATOR.'conf.inc.php';
  12. //加载RequestCore
  13. require_once OSS_API_PATH.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'requestcore'.DIRECTORY_SEPARATOR.'requestcore.class.php';
  14. //加载MimeTypes
  15. require_once OSS_API_PATH.DIRECTORY_SEPARATOR.'util'.DIRECTORY_SEPARATOR.'mimetypes.class.php';
  16. //检测语言包
  17. if(file_exists(OSS_API_PATH.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.ALI_LANG.'.inc.php')){
  18. require_once OSS_API_PATH.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.ALI_LANG.'.inc.php';
  19. }else{
  20. throw new OSS_Exception(OSS_LANG_FILE_NOT_EXIST);
  21. }
  22. //定义软件名称,版本号等信息
  23. define('OSS_NAME','oss-sdk-php');
  24. define('OSS_VERSION','1.1.5');
  25. define('OSS_BUILD','201210121010245');
  26. define('OSS_AUTHOR', 'xiaobing.meng@alibaba-inc.com');
  27. // EXCEPTIONS
  28. /**
  29. * OSS异常类,继承自基类
  30. */
  31. class OSS_Exception extends Exception {}
  32. //检测get_loaded_extensions函数是否被禁用。由于有些版本把该函数禁用了,所以先检测该函数是否存在。
  33. if(function_exists('get_loaded_extensions')){
  34. //检测curl扩展
  35. $extensions = get_loaded_extensions();
  36. if($extensions){
  37. if(!in_array('curl', $extensions)){
  38. throw new OSS_Exception(OSS_CURL_EXTENSION_MUST_BE_LOAD);
  39. }
  40. }else{
  41. throw new OSS_Exception(OSS_NO_ANY_EXTENSIONS_LOADED);
  42. }
  43. }else{
  44. throw new OSS_Exception('Function get_loaded_extensions has been disabled,Pls check php config.');
  45. }
  46. //CLASS
  47. /**
  48. * OSS基础类
  49. * @author xiaobing.meng@alibaba-inc.com
  50. * @since 2012-05-31
  51. */
  52. class ALIOSS{
  53. /*%******************************************************************************************%*/
  54. // CONSTANTS
  55. /**
  56. * OSS服务地址
  57. */
  58. const DEFAULT_OSS_HOST = 'oss.aliyuncs.com';
  59. /**
  60. * 软件名称
  61. */
  62. const NAME = OSS_NAME;
  63. /**
  64. * OSS软件Build ID
  65. */
  66. const BUILD = OSS_BUILD;
  67. /**
  68. * 版本号
  69. */
  70. const VERSION = OSS_VERSION;
  71. /**
  72. * 作者
  73. */
  74. const AUTHOR = OSS_AUTHOR;
  75. /*%******************************************************************************************%*/
  76. //OSS 内部常量
  77. const OSS_BUCKET = 'bucket';
  78. const OSS_OBJECT = 'object';
  79. const OSS_HEADERS = 'headers';
  80. const OSS_METHOD = 'method';
  81. const OSS_QUERY = 'query';
  82. const OSS_BASENAME = 'basename';
  83. const OSS_MAX_KEYS = 'max-keys';
  84. const OSS_UPLOAD_ID = 'uploadId';
  85. const OSS_MAX_KEYS_VALUE = 100;
  86. const OSS_MAX_OBJECT_GROUP_VALUE = 1000;
  87. const OSS_FILE_SLICE_SIZE = 8192;
  88. const OSS_PREFIX = 'prefix';
  89. const OSS_DELIMITER = 'delimiter';
  90. const OSS_MARKER = 'marker';
  91. const OSS_CONTENT_MD5 = 'Content-Md5';
  92. const OSS_CONTENT_TYPE = 'Content-Type';
  93. const OSS_CONTENT_LENGTH = 'Content-Length';
  94. const OSS_IF_MODIFIED_SINCE = 'If-Modified-Since';
  95. const OSS_IF_UNMODIFIED_SINCE = 'If-Unmodified-Since';
  96. const OSS_IF_MATCH = 'If-Match';
  97. const OSS_IF_NONE_MATCH = 'If-None-Match';
  98. const OSS_CACHE_CONTROL = 'Cache-Control';
  99. const OSS_EXPIRES = 'Expires';
  100. const OSS_PREAUTH = 'preauth';
  101. const OSS_CONTENT_COING = 'Content-Coding';
  102. const OSS_CONTENT_DISPOSTION = 'Content-Disposition';
  103. const OSS_RANGE = 'Range';
  104. const OS_CONTENT_RANGE = 'Content-Range';
  105. const OSS_CONTENT = 'content';
  106. const OSS_BODY = 'body';
  107. const OSS_LENGTH = 'length';
  108. const OSS_HOST = 'Host';
  109. const OSS_DATE = 'Date';
  110. const OSS_AUTHORIZATION = 'Authorization';
  111. const OSS_FILE_DOWNLOAD = 'fileDownload';
  112. const OSS_FILE_UPLOAD = 'fileUpload';
  113. const OSS_PART_SIZE = 'partSize';
  114. const OSS_SEEK_TO = 'seekTo';
  115. const OSS_SIZE = 'size';
  116. const OSS_QUERY_STRING = 'query_string';
  117. const OSS_SUB_RESOURCE = 'sub_resource';
  118. const OSS_DEFAULT_PREFIX = 'x-oss-';
  119. /*%******************************************************************************************%*/
  120. //私有URL变量
  121. const OSS_URL_ACCESS_KEY_ID = 'OSSAccessKeyId';
  122. const OSS_URL_EXPIRES = 'Expires';
  123. const OSS_URL_SIGNATURE = 'Signature';
  124. /*%******************************************************************************************%*/
  125. //HTTP方法
  126. const OSS_HTTP_GET = 'GET';
  127. const OSS_HTTP_PUT = 'PUT';
  128. const OSS_HTTP_HEAD = 'HEAD';
  129. const OSS_HTTP_POST = 'POST';
  130. const OSS_HTTP_DELETE = 'DELETE';
  131. /*%******************************************************************************************%*/
  132. //其他常量
  133. //x-oss
  134. const OSS_ACL = 'x-oss-acl';
  135. //OBJECT GROUP
  136. const OSS_OBJECT_GROUP = 'x-oss-file-group';
  137. //Multi Part
  138. const OSS_MULTI_PART = 'uploads';
  139. //Multi Delete
  140. const OSS_MULTI_DELETE = 'delete';
  141. //OBJECT COPY SOURCE
  142. const OSS_OBJECT_COPY_SOURCE = 'x-oss-copy-source';
  143. //private,only owner
  144. const OSS_ACL_TYPE_PRIVATE = 'private';
  145. //public reand
  146. const OSS_ACL_TYPE_PUBLIC_READ = 'public-read';
  147. //public read write
  148. const OSS_ACL_TYPE_PUBLIC_READ_WRITE = 'public-read-write';
  149. //OSS ACL数组
  150. static $OSS_ACL_TYPES = array(
  151. self::OSS_ACL_TYPE_PRIVATE,
  152. self::OSS_ACL_TYPE_PUBLIC_READ,
  153. self::OSS_ACL_TYPE_PUBLIC_READ_WRITE
  154. );
  155. /*%******************************************************************************************%*/
  156. // PROPERTIES
  157. /**
  158. * 是否使用SSL
  159. */
  160. protected $use_ssl = false;
  161. /**
  162. * 是否开启debug模式
  163. */
  164. private $debug_mode = true;
  165. /**
  166. * 最大重试次数
  167. */
  168. private $max_retries = 3;
  169. /**
  170. * 已经重试次数
  171. */
  172. private $redirects = 0;
  173. /**
  174. * 虚拟地址
  175. */
  176. private $vhost;
  177. /**
  178. * 路径表现方式
  179. */
  180. private $enable_domain_style = true;
  181. /**
  182. * 请求URL
  183. */
  184. private $request_url;
  185. /**
  186. * OSS API ACCESS ID
  187. */
  188. private $access_id;
  189. /**
  190. * OSS API ACCESS KEY
  191. */
  192. private $access_key;
  193. /**
  194. * hostname
  195. */
  196. private $hostname;
  197. /**
  198. * port number
  199. */
  200. private $port;
  201. /*%******************************************************************************************************%*/
  202. //Constructor
  203. /**
  204. * 默认构造函数
  205. * @param string $_access_id (Optional)
  206. * @param string $access_key (Optional)
  207. * @param string $hostname (Optional)
  208. * @throws OSS_Exception
  209. * @author xiaobing.meng@alibaba-inc.com
  210. * @since 2011-11-08
  211. */
  212. public function __construct($access_id = NULL,$access_key = NULL, $hostname = NULL ){
  213. //验证access_id,access_key
  214. if(!$access_id && !defined('OSS_ACCESS_ID')){
  215. throw new OSS_Exception(NOT_SET_OSS_ACCESS_ID);
  216. }
  217. if(!$access_key && !defined('OSS_ACCESS_KEY')){
  218. throw new OSS_Exception(NOT_SET_OSS_ACCESS_KEY);
  219. }
  220. if($access_id && $access_key){
  221. $this->access_id = $access_id;
  222. $this->access_key = $access_key;
  223. }elseif (defined('OSS_ACCESS_ID') && defined('OSS_ACCESS_KEY')){
  224. $this->access_id = OSS_ACCESS_ID;
  225. $this->access_key = OSS_ACCESS_KEY;
  226. }else{
  227. throw new OSS_Exception(NOT_SET_OSS_ACCESS_ID_AND_ACCESS_KEY);
  228. }
  229. //校验access_id&access_key
  230. if(empty($this->access_id) || empty($this->access_key)){
  231. throw new OSS_Exception(OSS_ACCESS_ID_OR_ACCESS_KEY_EMPTY);
  232. }
  233. //校验hostname
  234. if(NULL === $hostname){
  235. $this->hostname = self::DEFAULT_OSS_HOST;
  236. }else{
  237. $this->hostname = $hostname;
  238. }
  239. }
  240. /*%******************************************************************************************************%*/
  241. //属性
  242. /**
  243. * 设置debug模式
  244. * @param boolean $debug_mode (Optional)
  245. * @author xiaobing.meng@alibaba-inc.com
  246. * @since 2012-05-29
  247. * @return void
  248. */
  249. public function set_debug_mode($debug_mode = true){
  250. $this->debug_mode = $debug_mode;
  251. }
  252. /**
  253. * 设置最大尝试次数
  254. * @param int $max_retries
  255. * @author xiaobing.meng@alibaba-inc.com
  256. * @since 2012-05-29
  257. * @return void
  258. */
  259. public function set_max_retries($max_retries = 3){
  260. $this->max_retries = $max_retries;
  261. }
  262. /**
  263. * 获取最大尝试次数
  264. * @author xiaobing.meng@alibaba-inc.com
  265. * @since 2012-05-29
  266. * @return int
  267. */
  268. public function get_max_retries(){
  269. return $this->max_retries;
  270. }
  271. /**
  272. * 设置host地址
  273. * @author xiaobing.meng@alibaba-inc.com
  274. * @param string $hostname host name
  275. * @param int $port int
  276. * @since 2012-06-11
  277. * @return void
  278. */
  279. public function set_host_name($hostname,$port = null){
  280. $this->hostname = $hostname;
  281. if($port){
  282. $this->port = $port;
  283. $this->hostname .= ':'.$port;
  284. }
  285. }
  286. /**
  287. * 设置vhost地址
  288. * @author xiaobing.meng@alibaba-inc.com
  289. * @param string $vhost vhost
  290. * @since 2012-06-11
  291. * @return void
  292. */
  293. public function set_vhost($vhost){
  294. $this->vhost = $vhost;
  295. }
  296. /**
  297. * 设置路径形式,如果为true,则启用三级域名,如bucket.oss.aliyuncs.com
  298. * @author xiaobing.meng@alibaba-inc.com
  299. * @param boolean $enable_domain_style
  300. * @since 2012-06-11
  301. * @return void
  302. */
  303. public function set_enable_domain_style($enable_domain_style = true){
  304. $this->enable_domain_style = $enable_domain_style;
  305. }
  306. /*%******************************************************************************************************%*/
  307. //请求
  308. /**
  309. * Authorization
  310. * @param array $options (Required)
  311. * @throws OSS_Exception
  312. * @author xiaobing.meng@alibaba-inc.com
  313. * @since 2012-05-31
  314. */
  315. public function auth($options){
  316. //开始记录LOG
  317. $msg = "---LOG START---------------------------------------------------------------------------\n";
  318. //验证Bucket,list_bucket时不需要验证
  319. if(!( ('/' == $options[self::OSS_OBJECT]) && ('' == $options[self::OSS_BUCKET]) && ('GET' == $options[self::OSS_METHOD])) && !$this->validate_bucket($options[self::OSS_BUCKET])){
  320. throw new OSS_Exception('"'.$options[self::OSS_BUCKET].'"'.OSS_BUCKET_NAME_INVALID);
  321. }
  322. //验证Object
  323. if(isset($options[self::OSS_OBJECT]) && !$this->validate_object($options[self::OSS_OBJECT])){
  324. throw new OSS_Exception($options[self::OSS_OBJECT].OSS_OBJECT_NAME_INVALID);
  325. }
  326. //Object编码为UTF-8
  327. if($this->is_gb2312($options[self::OSS_OBJECT])){
  328. $options[self::OSS_OBJECT] = iconv('GB2312', "UTF-8",$options[self::OSS_OBJECT]);
  329. }elseif($this->check_char($options[self::OSS_OBJECT],true)){
  330. $options[self::OSS_OBJECT] = iconv('GBK', "UTF-8",$options[self::OSS_OBJECT]);
  331. }
  332. //验证ACL
  333. if(isset($options[self::OSS_HEADERS][self::OSS_ACL]) && !empty($options[self::OSS_HEADERS][self::OSS_ACL])){
  334. if(!in_array(strtolower($options[self::OSS_HEADERS][self::OSS_ACL]), self::$OSS_ACL_TYPES)){
  335. throw new OSS_Exception($options[self::OSS_HEADERS][self::OSS_ACL].':'.OSS_ACL_INVALID);
  336. }
  337. }
  338. //定义scheme
  339. $scheme = $this->use_ssl ? 'https://' : 'http://';
  340. //匹配bucket
  341. if(strpos($options[self::OSS_BUCKET],"-")!== false){//bucket 带"-"的时候
  342. $this->set_enable_domain_style(false);
  343. }else{
  344. $this->set_enable_domain_style(true);
  345. }
  346. if($this->enable_domain_style){
  347. $hostname = $this->vhost ? $this->vhost : (($options[self::OSS_BUCKET] =='')?$this->hostname:($options[self::OSS_BUCKET].'.').$this->hostname);
  348. }else{
  349. $hostname = (isset($options[self::OSS_BUCKET]) && ''!==$options[self::OSS_BUCKET])?$this->hostname.'/'.$options[self::OSS_BUCKET]:$this->hostname;
  350. }
  351. //请求参数
  352. $resource = '';
  353. $sub_resource = '';
  354. $signable_resource = '';
  355. $query_string_params = array();
  356. $signable_query_string_params = array();
  357. $string_to_sign = '';
  358. $headers = array (
  359. self::OSS_CONTENT_MD5 => '',
  360. self::OSS_CONTENT_TYPE => isset($options[self::OSS_CONTENT_TYPE])?$options[self::OSS_CONTENT_TYPE]:'application/x-www-form-urlencoded',
  361. self::OSS_DATE => isset($options[self::OSS_DATE])? $options[self::OSS_DATE]: gmdate('D, d M Y H:i:s \G\M\T'),
  362. self::OSS_HOST => $this->enable_domain_style?$hostname:$this->hostname,
  363. );
  364. if(isset ( $options [self::OSS_OBJECT] ) && '/' !== $options [self::OSS_OBJECT]){
  365. $signable_resource = '/'.str_replace('%2F', '/', rawurlencode($options[self::OSS_OBJECT]));
  366. }
  367. if(isset($options[self::OSS_QUERY_STRING])){
  368. $query_string_params = array_merge($query_string_params,$options[self::OSS_QUERY_STRING]);
  369. }
  370. $query_string = $this->to_query_string($query_string_params);
  371. $signable_list = array(
  372. 'partNumber',
  373. 'uploadId',
  374. );
  375. foreach ($signable_list as $item){
  376. if(isset($options[$item])){
  377. $signable_query_string_params[$item] = $options[$item];
  378. }
  379. }
  380. $signable_query_string = $this->to_query_string($signable_query_string_params);
  381. //合并 HTTP headers
  382. if (isset ( $options [self::OSS_HEADERS] )) {
  383. $headers = array_merge ( $headers, $options [self::OSS_HEADERS] );
  384. }
  385. //生成请求URL
  386. $conjunction = '?';
  387. $non_signable_resource = '';
  388. if (isset($options[self::OSS_SUB_RESOURCE])){
  389. $signable_resource .= $conjunction . $options[self::OSS_SUB_RESOURCE];
  390. $conjunction = '&';
  391. }
  392. if($signable_query_string !== ''){
  393. $signable_query_string = $conjunction.$signable_query_string;
  394. $conjunction = '&';
  395. }
  396. if($query_string !== ''){
  397. $non_signable_resource .= $conjunction . $query_string;
  398. $conjunction = '&';
  399. }
  400. $this->request_url = $scheme . $hostname . $signable_resource . $signable_query_string . $non_signable_resource;
  401. $msg .= "--REQUEST URL:----------------------------------------------\n".$this->request_url."\n";
  402. //创建请求
  403. $request = new RequestCore($this->request_url);
  404. // Streaming uploads
  405. if (isset($options[self::OSS_FILE_UPLOAD])){
  406. if (is_resource($options[self::OSS_FILE_UPLOAD])){
  407. $length = null;
  408. if (isset($options[self::OSS_CONTENT_LENGTH])){
  409. $length = $options[self::OSS_CONTENT_LENGTH];
  410. }elseif (isset($options[self::OSS_SEEK_TO])){
  411. $stats = fstat($options[self::OSS_FILE_UPLOAD]);
  412. if ($stats && $stats[self::OSS_SIZE] >= 0){
  413. $length = $stats[self::OSS_SIZE] - (integer) $options[self::OSS_SEEK_TO];
  414. }
  415. }
  416. $request->set_read_stream($options[self::OSS_FILE_UPLOAD], $length);
  417. if ($headers[self::OSS_CONTENT_TYPE] === 'application/x-www-form-urlencoded'){
  418. $headers[self::OSS_CONTENT_TYPE] = 'application/octet-stream';
  419. }
  420. }else{
  421. $request->set_read_file($options[self::OSS_FILE_UPLOAD]);
  422. $length = $request->read_stream_size;
  423. if (isset($options[self::OSS_CONTENT_LENGTH])){
  424. $length = $options[self::OSS_CONTENT_LENGTH];
  425. }elseif (isset($options[self::OSS_SEEK_TO]) && isset($length)){
  426. $length -= (integer) $options[self::OSS_SEEK_TO];
  427. }
  428. $request->set_read_stream_size($length);
  429. if (isset($headers[self::OSS_CONTENT_TYPE]) && ($headers[self::OSS_CONTENT_TYPE] === 'application/x-www-form-urlencoded')){
  430. $extension = explode('.', $options[self::OSS_FILE_UPLOAD]);
  431. $extension = array_pop($extension);
  432. $mime_type = MimeTypes::get_mimetype($extension);
  433. $headers[self::OSS_CONTENT_TYPE] = $mime_type;
  434. }
  435. }
  436. $options[self::OSS_CONTENT_MD5] = '';
  437. }
  438. if (isset($options[self::OSS_SEEK_TO])){
  439. $request->set_seek_position((integer) $options[self::OSS_SEEK_TO]);
  440. }
  441. if (isset($options[self::OSS_FILE_DOWNLOAD])){
  442. if (is_resource($options[self::OSS_FILE_DOWNLOAD])){
  443. $request->set_write_stream($options[self::OSS_FILE_DOWNLOAD]);
  444. }else{
  445. $request->set_write_file($options[self::OSS_FILE_DOWNLOAD]);
  446. }
  447. }
  448. if(isset($options[self::OSS_METHOD])){
  449. $request->set_method($options[self::OSS_METHOD]);
  450. $string_to_sign .= $options[self::OSS_METHOD] . "\n";
  451. }
  452. if (isset ( $options [self::OSS_CONTENT] )) {
  453. $request->set_body ( $options [self::OSS_CONTENT] );
  454. if ($headers[self::OSS_CONTENT_TYPE] === 'application/x-www-form-urlencoded'){
  455. $headers[self::OSS_CONTENT_TYPE] = 'application/octet-stream';
  456. }
  457. $headers[self::OSS_CONTENT_LENGTH] = strlen($options [self::OSS_CONTENT]);
  458. $headers[self::OSS_CONTENT_MD5] = $this->hex_to_base64(md5($options[self::OSS_CONTENT]));
  459. }
  460. uksort($headers, 'strnatcasecmp');
  461. foreach ( $headers as $header_key => $header_value ) {
  462. $header_value = str_replace ( array ("\r", "\n" ), '', $header_value );
  463. if ($header_value !== '') {
  464. $request->add_header ( $header_key, $header_value );
  465. }
  466. if (
  467. strtolower($header_key) === 'content-md5' ||
  468. strtolower($header_key) === 'content-type' ||
  469. strtolower($header_key) === 'date' ||
  470. (isset($options['self::OSS_PREAUTH']) && (integer) $options['self::OSS_PREAUTH'] > 0)
  471. ){
  472. $string_to_sign .= $header_value . "\n";
  473. }elseif (substr(strtolower($header_key), 0, 6) === self::OSS_DEFAULT_PREFIX){
  474. $string_to_sign .= strtolower($header_key) . ':' . $header_value . "\n";
  475. }
  476. }
  477. $string_to_sign .= '/' . $options[self::OSS_BUCKET];
  478. $string_to_sign .= $this->enable_domain_style ? ($options[self::OSS_BUCKET]!=''? ($options[self::OSS_OBJECT]=='/'?'/':'') :'' ) : '';
  479. $string_to_sign .= rawurldecode($signable_resource) . urldecode($signable_query_string);
  480. $msg .= "STRING TO SIGN:----------------------------------------------\n".$string_to_sign."\n";
  481. $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->access_key, true));
  482. $request->add_header('Authorization', 'OSS ' . $this->access_id . ':' . $signature);
  483. if (isset($options[self::OSS_PREAUTH]) && (integer) $options[self::OSS_PREAUTH] > 0){
  484. return $this->request_url . $conjunction . self::OSS_URL_ACCESS_KEY_ID.'=' . $this->access_id . '&'.self::OSS_URL_EXPIRES.'=' . $options[self::OSS_PREAUTH] . '&'.self::OSS_URL_SIGNATURE.'=' . rawurlencode($signature);
  485. }elseif (isset($options[self::OSS_PREAUTH])){
  486. return $this->request_url;
  487. }
  488. if ($this->debug_mode){
  489. $request->debug_mode = $this->debug_mode;
  490. }
  491. $msg .= "REQUEST HEADERS:----------------------------------------------\n".serialize($request->request_headers)."\n";
  492. $request->send_request();
  493. $response_header = $request->get_response_header();
  494. $response_header['x-oss-request-url'] = $this->request_url;
  495. $response_header['x-oss-redirects'] = $this->redirects;
  496. $response_header['x-oss-stringtosign'] = $string_to_sign;
  497. $response_header['x-oss-requestheaders'] = $request->request_headers;
  498. $msg .= "RESPONSE HEADERS:----------------------------------------------\n".serialize($response_header)."\n";
  499. $data = new ResponseCore ( $response_header , $request->get_response_body (), $request->get_response_code () );
  500. if((integer)$request->get_response_code() === 400 /*Bad Request*/ || (integer)$request->get_response_code() === 500 /*Internal Error*/ || (integer)$request->get_response_code() === 503 /*Service Unavailable*/){
  501. if($this->redirects <= $this->max_retries ){
  502. //设置休眠
  503. $delay = (integer) (pow(4, $this->redirects) * 100000);
  504. usleep($delay);
  505. $this->redirects++;
  506. $data = $this->auth($options);
  507. }
  508. }
  509. $msg .= "RESPONSE DATA:----------------------------------------------\n".serialize($data)."\n";
  510. $msg .= date('Y-m-d H:i:s').":---LOG END---------------------------------------------------------------------------\n";
  511. //add log
  512. $this->log($msg);
  513. $this->redirects = 0;
  514. return $data;
  515. }
  516. /*%******************************************************************************************************%*/
  517. //Service Operation
  518. /**
  519. * Get Buket list
  520. * @param array $options (Optional)
  521. * @throws OSS_Exception
  522. * @author xiaobing.meng@alibaba-inc.com
  523. * @since 2011-11-14
  524. * @return ResponseCore
  525. */
  526. public function list_bucket($options = NULL) {
  527. //$options
  528. $this->validate_options($options);
  529. if (! $options) {
  530. $options = array ();
  531. }
  532. $options[self::OSS_BUCKET] = '';
  533. $options[self::OSS_METHOD] = 'GET';
  534. $options[self::OSS_OBJECT] = '/';
  535. $response = $this->auth ( $options );
  536. return $response;
  537. }
  538. /*%******************************************************************************************************%*/
  539. //Bucket Operation
  540. /**
  541. * Create Bucket
  542. * @param string $bucket (Required)
  543. * @param string $acl (Optional)
  544. * @param array $options (Optional)
  545. * @author xiaobing.meng@alibaba-inc.com
  546. * @since 2011-11-14
  547. * @return ResponseCore
  548. */
  549. public function create_bucket($bucket,$acl = self::OSS_ACL_TYPE_PRIVATE, $options = NULL){
  550. //$options
  551. $this->validate_options($options);
  552. if (! $options) {
  553. $options = array ();
  554. }
  555. //bucket
  556. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  557. $options[self::OSS_BUCKET] = $bucket;
  558. $options[self::OSS_METHOD] = 'PUT';
  559. $options[self::OSS_OBJECT] = '/';
  560. $options[self::OSS_HEADERS] = array(self::OSS_ACL => $acl);
  561. $response = $this->auth ( $options );
  562. return $response;
  563. }
  564. /**
  565. * Delete Bucket
  566. * @param string $bucket (Required)
  567. * @param array $options (Optional)
  568. * @author xiaobing.meng@alibaba-inc.com
  569. * @since 2011-11-14
  570. * @return ResponseCore
  571. */
  572. public function delete_bucket($bucket,$options = NULL){
  573. //$options
  574. $this->validate_options($options);
  575. if (! $options) {
  576. $options = array ();
  577. }
  578. //bucket
  579. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  580. $options[self::OSS_BUCKET] = $bucket;
  581. $options[self::OSS_METHOD] = 'DELETE';
  582. $options[self::OSS_OBJECT] = '/';
  583. $response = $this->auth ( $options );
  584. return $response;
  585. }
  586. /**
  587. * Get Bucket's ACL
  588. * @param string $bucket (Required)
  589. * @param array $options (Optional)
  590. * @throws OSS_Exception
  591. * @author xiaobing.meng@alibaba-inc.com
  592. * @since 2011-11-14
  593. * @return ResponseCore
  594. */
  595. public function get_bucket_acl($bucket ,$options = NULL){
  596. //options
  597. $this->validate_options($options);
  598. if(!$options){
  599. $options = array();
  600. }
  601. //bucket
  602. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  603. $options[self::OSS_BUCKET] = $bucket;
  604. $options[self::OSS_METHOD] = 'GET';
  605. $options[self::OSS_OBJECT] = '/';
  606. $options[self::OSS_SUB_RESOURCE] = 'acl';
  607. $response = $this->auth ( $options );
  608. return $response;
  609. }
  610. /**
  611. * Set bucket'ACL
  612. * @param string $bucket (Required)
  613. * @param string $acl (Required)
  614. * @param array $options (Optional)
  615. * @throws OSS_Exception
  616. * @author xiaobing.meng@alibaba-inc.com
  617. * @since 2011-11-14
  618. * @return ResponseCore
  619. */
  620. public function set_bucket_acl($bucket ,$acl , $options = NULL){
  621. //options
  622. $this->validate_options($options);
  623. if(!$options){
  624. $options = array();
  625. }
  626. //bucket
  627. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  628. $options[self::OSS_BUCKET] = $bucket;
  629. $options[self::OSS_METHOD] = 'PUT';
  630. $options[self::OSS_OBJECT] = '/';
  631. $options[self::OSS_HEADERS] = array(self::OSS_ACL => $acl);
  632. $response = $this->auth ( $options );
  633. return $response;
  634. }
  635. /*%******************************************************************************************************%*/
  636. //Object Operation
  637. /**
  638. * List Object
  639. * @param string $bucket (Required)
  640. * @param array $options (Optional)
  641. * 其中options中的参数如下
  642. * $options = array(
  643. * 'max-keys' => max-keys用于限定此次返回object的最大数,如果不设定,默认为100,max-keys取值不能大于100。
  644. * 'prefix' => 限定返回的object key必须以prefix作为前缀。注意使用prefix查询时,返回的key中仍会包含prefix。
  645. * 'delimiter' => 是一个用于对Object名字进行分组的字符。所有名字包含指定的前缀且第一次出现delimiter字符之间的object作为一组元素
  646. * 'marker' => 用户设定结果从marker之后按字母排序的第一个开始返回。
  647. * )
  648. * 其中 prefix,marker用来实现分页显示效果,参数的长度必须小于256字节。
  649. * @throws OSS_Exception
  650. * @author xiaobing.meng@alibaba-inc.com
  651. * @since 2011-11-14
  652. * @return ResponseCore
  653. */
  654. public function list_object($bucket,$options = NULL){
  655. //options
  656. $this->validate_options($options);
  657. if(!$options){
  658. $options = array();
  659. }
  660. //bucket
  661. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  662. $options[self::OSS_BUCKET] = $bucket;
  663. $options[self::OSS_METHOD] = 'GET';
  664. $options[self::OSS_OBJECT] = '/';
  665. $options[self::OSS_HEADERS] = array(
  666. self::OSS_DELIMITER => isset($options[self::OSS_DELIMITER])?$options[self::OSS_DELIMITER]:'/',
  667. self::OSS_PREFIX => isset($options[self::OSS_PREFIX])?$options[self::OSS_PREFIX]:'',
  668. self::OSS_MAX_KEYS => isset($options[self::OSS_MAX_KEYS])?$options[self::OSS_MAX_KEYS]:self::OSS_MAX_KEYS_VALUE,
  669. self::OSS_MARKER => isset($options[self::OSS_MARKER])?$options[self::OSS_MARKER]:'',
  670. );
  671. $response = $this->auth ( $options );
  672. return $response;
  673. }
  674. /**
  675. * 创建目录(目录和文件的区别在于,目录最后增加'/')
  676. * @param string $bucket
  677. * @param string $object
  678. * @param array $options
  679. * @author xiaobing.meng@alibaba-inc.com
  680. * @since 2011-11-14
  681. * @return ResponseCore
  682. */
  683. public function create_object_dir($bucket,$object,$options = NULL){
  684. //options
  685. $this->validate_options($options);
  686. if(!$options){
  687. $options = array();
  688. }
  689. //bucket
  690. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  691. //object
  692. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  693. $options[self::OSS_BUCKET] = $bucket;
  694. $options[self::OSS_METHOD] = 'PUT';
  695. $options[self::OSS_OBJECT] = $object.'/'; //虚拟目录需要以'/结尾'
  696. $options[self::OSS_CONTENT_LENGTH] = array(self::OSS_CONTENT_LENGTH => 0);
  697. $response = $this->auth ( $options );
  698. return $response;
  699. }
  700. /**
  701. * 通过在http body中添加内容来上传文件,适合比较小的文件
  702. * 根据api约定,需要在http header中增加content-length字段
  703. * @param string $bucket (Required)
  704. * @param string $object (Required)
  705. * @param string $content (Required)
  706. * @param array $options (Optional)
  707. * @author xiaobing.meng@alibaba-inc.com
  708. * @since 2011-11-14
  709. * @return ResponseCore
  710. */
  711. public function upload_file_by_content($bucket,$object,$options = NULL){
  712. //options
  713. $this->validate_options($options);
  714. if(!$options){
  715. $options = array();
  716. }
  717. //bucket
  718. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  719. //object
  720. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  721. //内容校验
  722. $this->validate_content($options);
  723. $objArr = explode('/', $object);
  724. $basename = array_pop($objArr);
  725. $extension = explode ( '.', $basename );
  726. $extension = array_pop ( $extension );
  727. $content_type = MimeTypes::get_mimetype(strtolower($extension));
  728. $options[self::OSS_BUCKET] = $bucket;
  729. $options[self::OSS_METHOD] = 'PUT';
  730. $options[self::OSS_OBJECT] = $object;
  731. if(!isset($options[self::OSS_LENGTH])){
  732. $options[self::OSS_CONTENT_LENGTH] = strlen($options[self::OSS_CONTENT]);
  733. }else{
  734. $options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH];
  735. }
  736. if(!isset($options[self::OSS_CONTENT_TYPE]) && isset($content_type) && !empty($content_type) ){
  737. $options[self::OSS_CONTENT_TYPE] = $content_type;
  738. }
  739. $response = $this->auth ( $options );
  740. return $response;
  741. }
  742. /**
  743. * 上传文件,适合比较大的文件
  744. * @param string $bucket (Required)
  745. * @param string $object (Required)
  746. * @param string $file (Required)
  747. * @param array $options (Optional)
  748. * @author xiaobing.meng@alibaba-inc.com
  749. * @since 2012-02-28
  750. * @return ResponseCore
  751. */
  752. public function upload_file_by_file($bucket,$object,$file,$options = NULL){
  753. //options
  754. $this->validate_options($options);
  755. if(!$options){
  756. $options = array();
  757. }
  758. //bucket
  759. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  760. //object
  761. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  762. //file
  763. $this->is_empty($file, OSS_FILE_PATH_IS_NOT_ALLOWED_EMPTY);
  764. if($this->chk_chinese($file)){
  765. $file = iconv('utf-8','gbk',$file);
  766. }
  767. $options[self::OSS_FILE_UPLOAD] = $file;
  768. if(!file_exists($options[self::OSS_FILE_UPLOAD])){
  769. throw new OSS_Exception($options[self::OSS_FILE_UPLOAD].OSS_FILE_NOT_EXIST);
  770. }
  771. $filesize = filesize($options[self::OSS_FILE_UPLOAD]);
  772. $partsize = 1024 * 1024 ; //默认为 1M
  773. $extension = explode ( '.', $file );
  774. $extension = array_pop ( $extension );
  775. $content_type = MimeTypes::get_mimetype(strtolower($extension));
  776. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  777. $options[self::OSS_BUCKET] = $bucket;
  778. $options[self::OSS_OBJECT] = $object;
  779. $options[self::OSS_CONTENT_TYPE] = $content_type;
  780. $options[self::OSS_CONTENT_LENGTH] = $filesize;
  781. $response = $this->auth($options);
  782. return $response;
  783. }
  784. /**
  785. * 拷贝Object
  786. * @param string $bucket (Required)
  787. * @param string $from_object (Required)
  788. * @param string $to_object (Required)
  789. * @param string $options (Optional)
  790. * @author xiaobing.meng@alibaba-inc.com
  791. * @since 2011-12-21
  792. * @return ResponseCore
  793. */
  794. public function copy_object($from_bucket,$from_object,$to_bucket,$to_object,$options = NULL){
  795. //options
  796. $this->validate_options($options);
  797. if(!$options){
  798. $options = array();
  799. }
  800. //from bucket
  801. $this->is_empty($from_bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  802. //to bucket
  803. $this->is_empty($to_bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  804. //from object
  805. $this->is_empty($from_object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  806. //to object
  807. $this->is_empty($to_object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  808. $options[self::OSS_BUCKET] = $to_bucket;
  809. $options[self::OSS_METHOD] = 'PUT';
  810. $options[self::OSS_OBJECT] = $to_object;
  811. $options[self::OSS_HEADERS] = array(self::OSS_OBJECT_COPY_SOURCE => '/'.$from_bucket.'/'.$from_object);
  812. $response = $this->auth ( $options );
  813. return $response;
  814. }
  815. /**
  816. * 获得object的meta信息
  817. * @param string $bucket (Required)
  818. * @param string $object (Required)
  819. * @param string $options (Optional)
  820. * @author xiaobing.meng@alibaba-inc.com
  821. * @since 2011-11-14
  822. * @return ResponseCore
  823. */
  824. public function get_object_meta($bucket,$object,$options = NULL){
  825. //options
  826. $this->validate_options($options);
  827. if(!$options){
  828. $options = array();
  829. }
  830. //bucket
  831. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  832. //object
  833. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  834. $options[self::OSS_BUCKET] = $bucket;
  835. $options[self::OSS_METHOD] = 'HEAD';
  836. $options[self::OSS_OBJECT] = $object;
  837. $response = $this->auth ( $options );
  838. return $response;
  839. }
  840. /**
  841. * 删除object
  842. * @param string $bucket(Required)
  843. * @param string $object (Required)
  844. * @param array $options (Optional)
  845. * @author xiaobing.meng@alibaba-inc.com
  846. * @since 2011-11-14
  847. * @return ResponseCore
  848. */
  849. public function delete_object($bucket,$object,$options = NULL){
  850. //options
  851. $this->validate_options($options);
  852. if(!$options){
  853. $options = array();
  854. }
  855. //bucket
  856. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  857. //object
  858. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  859. $options[self::OSS_BUCKET] = $bucket;
  860. $options[self::OSS_METHOD] = 'DELETE';
  861. $options[self::OSS_OBJECT] = $object;
  862. $response = $this->auth ( $options );
  863. return $response;
  864. }
  865. /**
  866. * 批量删除objects
  867. * @param string $bucket(Required)
  868. * @param array $objects (Required)
  869. * @param array $options (Optional)
  870. * @author xiaobing.meng@alibaba-inc.com
  871. * @since 2012-03-09
  872. * @return ResponseCore
  873. */
  874. public function delete_objects($bucket,$objects,$options = null){
  875. //options
  876. $this->validate_options($options);
  877. if(!$options){
  878. $options = array();
  879. }
  880. //bucket
  881. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  882. //objects
  883. if(!is_array($objects) || !$objects){
  884. throw new OSS_Exception('The ' . __FUNCTION__ . ' method requires the "objects" option to be set as an array.');
  885. }
  886. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  887. $options[self::OSS_BUCKET] = $bucket;
  888. $options[self::OSS_OBJECT] = '/';
  889. $options[self::OSS_SUB_RESOURCE] = 'delete';
  890. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  891. $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><Delete></Delete>');
  892. // Quiet mode?
  893. if (isset($options['quiet'])){
  894. $quiet = 'false';
  895. if (is_bool($options['quiet'])) { //Boolean
  896. $quiet = $options['quiet'] ? 'true' : 'false';
  897. }elseif (is_string($options['quiet'])){ // String
  898. $quiet = ($options['quiet'] === 'true') ? 'true' : 'false';
  899. }
  900. $xml->addChild('Quiet', $quiet);
  901. }
  902. // Add the objects
  903. foreach ($objects as $object){
  904. $xobject = $xml->addChild('Object');
  905. $object = $this->s_replace($object);
  906. $xobject->addChild('Key', $object);
  907. }
  908. $options[self::OSS_CONTENT] = $xml->asXML();
  909. return $this->auth($options);
  910. }
  911. /**
  912. * 获得Object内容
  913. * @param string $bucket(Required)
  914. * @param string $object (Required)
  915. * @param array $options (Optional)
  916. * @author xiaobing.meng@alibaba-inc.com
  917. * @since 2011-11-14
  918. * @return ResponseCore
  919. */
  920. public function get_object($bucket,$object,$options = NULL){
  921. //options
  922. $this->validate_options($options);
  923. if(!$options){
  924. $options = array();
  925. }
  926. //bucket
  927. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  928. //object
  929. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  930. if(isset($options[self::OSS_FILE_DOWNLOAD]) && $this->chk_chinese($options[self::OSS_FILE_DOWNLOAD])){
  931. $options[self::OSS_FILE_DOWNLOAD] = iconv('utf-8','gbk',$options[self::OSS_FILE_DOWNLOAD]);
  932. }
  933. $options[self::OSS_BUCKET] = $bucket;
  934. $options[self::OSS_METHOD] = 'GET';
  935. $options[self::OSS_OBJECT] = $object;
  936. if(isset($options['lastmodified'])){
  937. $options[self::OSS_HEADERS][self::OSS_IF_MODIFIED_SINCE] = $options['lastmodified'];
  938. unset($options['lastmodified']);
  939. }
  940. if(isset($options['etag'])){
  941. $options[self::OSS_HEADERS][self::OSS_IF_NONE_MATCH] = $options['etag'];
  942. unset($options['etag']);
  943. }
  944. if(isset($options['range'])){
  945. $options[self::OSS_HEADERS][self::OSS_RANGE] = 'bytes=' . $options['range'];
  946. unset($options['range']);
  947. }
  948. return $this->auth ( $options );
  949. }
  950. /**
  951. * 检测Object是否存在
  952. * @param string $bucket(Required)
  953. * @param string $object (Required)
  954. * @param array $options (Optional)
  955. * @author xiaobing.meng@alibaba-inc.com
  956. * @since 2011-11-14
  957. * @return boolean
  958. */
  959. public function is_object_exist($bucket,$object,$options = NULL){
  960. //options
  961. $this->validate_options($options);
  962. if(!$options){
  963. $options = array();
  964. }
  965. //bucket
  966. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  967. //object
  968. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  969. $options[self::OSS_BUCKET] = $bucket;
  970. $options[self::OSS_METHOD] = 'GET';
  971. $options[self::OSS_OBJECT] = $object;
  972. $response = $this->get_object_meta($bucket, $object,$options);
  973. return $response;
  974. }
  975. /*%******************************************************************************************************%*/
  976. //Multi Part相关操作
  977. /**
  978. * 计算文件可以分成多少个part,以及每个part的长度以及起始位置
  979. * 方法必须在 <upload_part()>中调用
  980. *
  981. * @param integer $filesize (Required) 文件大小
  982. * @param integer $part_size (Required) part大小,默认5M
  983. * @return array An array 包含 key-value 键值对. Key 为 `seekTo` 和 `length`.
  984. */
  985. public function get_multipart_counts($filesize, $part_size = 5242880 ){
  986. $i = 0;
  987. $sizecount = $filesize;
  988. $values = array();
  989. if((integer)$part_size <= 5242880){
  990. $part_size = 5242880; //5M
  991. }elseif ((integer)$part_size > 524288000){
  992. $part_size = 524288000; //500M
  993. }else{
  994. $part_size = 52428800; //50M
  995. }
  996. while ($sizecount > 0)
  997. {
  998. $sizecount -= $part_size;
  999. $values[] = array(
  1000. self::OSS_SEEK_TO => ($part_size * $i),
  1001. self::OSS_LENGTH => (($sizecount > 0) ? $part_size : ($sizecount + $part_size)),
  1002. );
  1003. $i++;
  1004. }
  1005. return $values;
  1006. }
  1007. /**
  1008. * 初始化multi-part upload,并且返回uploadId
  1009. * @param string $bucket (Required) Bucket名称
  1010. * @param string $object (Required) Object名称
  1011. * @param array $options (Optional) Key-Value数组,其中可以包括以下的key
  1012. * @return ResponseCore
  1013. */
  1014. public function initiate_multipart_upload($bucket,$object,$options = NULL){
  1015. //options
  1016. $this->validate_options($options);
  1017. if(!$options){
  1018. $options = array();
  1019. }
  1020. //bucket
  1021. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1022. //object
  1023. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  1024. // 发送请求
  1025. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1026. $options[self::OSS_BUCKET] = $bucket;
  1027. $options[self::OSS_OBJECT] = $object;
  1028. $options[self::OSS_SUB_RESOURCE] = 'uploads';
  1029. $options[self::OSS_CONTENT] = '';
  1030. //$options[self::OSS_CONTENT_LENGTH] = 0;
  1031. $options[self::OSS_HEADERS] = array(self::OSS_CONTENT_TYPE => 'application/octet-stream');
  1032. $response = $this->auth ( $options );
  1033. return $response;
  1034. }
  1035. /**
  1036. * 上传part
  1037. * @param string $bucket (Required) Bucket名称
  1038. * @param string $object (Required) Object名称
  1039. * @param string $upload_id (Required) uploadId
  1040. * @param array $options (Optional) Key-Value数组,其中可以包括以下的key
  1041. * @return ResponseCore
  1042. */
  1043. public function upload_part($bucket, $object, $upload_id, $options = null){
  1044. //options
  1045. $this->validate_options($options);
  1046. if(!$options){
  1047. $options = array();
  1048. }
  1049. //bucket
  1050. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1051. //object
  1052. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  1053. if (!isset($options[self::OSS_FILE_UPLOAD]) || !isset($options['partNumber'])){
  1054. throw new OSS_Exception('The `fileUpload` and `partNumber` options are both required in ' . __FUNCTION__ . '().');
  1055. }
  1056. $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
  1057. $options[self::OSS_BUCKET] = $bucket;
  1058. $options[self::OSS_OBJECT] = $object;
  1059. $options[self::OSS_UPLOAD_ID] = $upload_id;
  1060. if(isset($options[self::OSS_LENGTH])){
  1061. $options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH];
  1062. }
  1063. return $this->auth($options);
  1064. }
  1065. /**
  1066. * list part
  1067. * @param string $bucket (Required) Bucket名称
  1068. * @param string $object (Required) Object名称
  1069. * @param string $upload_id (Required) uploadId
  1070. * @param array $options (Optional) Key-Value数组,其中可以包括以下的key
  1071. * @return ResponseCore
  1072. */
  1073. public function list_parts($bucket, $object, $upload_id, $options = null){
  1074. //options
  1075. $this->validate_options($options);
  1076. if(!$options){
  1077. $options = array();
  1078. }
  1079. //bucket
  1080. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1081. //object
  1082. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  1083. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1084. $options[self::OSS_BUCKET] = $bucket;
  1085. $options[self::OSS_OBJECT] = $object;
  1086. $options[self::OSS_UPLOAD_ID] = $upload_id;
  1087. $options[self::OSS_QUERY_STRING] = array();
  1088. foreach (array('max-parts', 'part-number-marker') as $param){
  1089. if (isset($options[$param])){
  1090. $options[self::OSS_QUERY_STRING][$param] = $options[$param];
  1091. unset($options[$param]);
  1092. }
  1093. }
  1094. return $this->auth($options);
  1095. }
  1096. /**
  1097. * 中止上传mulit-part upload
  1098. * @param string $bucket (Required) Bucket名称
  1099. * @param string $object (Required) Object名称
  1100. * @param string $upload_id (Required) uploadId
  1101. * @param array $options (Optional) Key-Value数组,其中可以包括以下的key
  1102. * @return ResponseCore
  1103. */
  1104. public function abort_multipart_upload($bucket, $object, $upload_id, $options = NULL){
  1105. //options
  1106. $this->validate_options($options);
  1107. if(!$options){
  1108. $options = array();
  1109. }
  1110. //bucket
  1111. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1112. //object
  1113. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  1114. $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
  1115. $options[self::OSS_BUCKET] = $bucket;
  1116. $options[self::OSS_OBJECT] = $object;
  1117. $options[self::OSS_UPLOAD_ID] = $upload_id;
  1118. return $this->auth($options);
  1119. }
  1120. /**
  1121. * 完成multi-part上传
  1122. * @param string $bucket (Required) Bucket名称
  1123. * @param string $object (Required) Object名称
  1124. * @param string $upload_id (Required) uploadId
  1125. * @param string $parts xml格式文件
  1126. * @param array $options (Optional) Key-Value数组,其中可以包括以下的key
  1127. * @return ResponseCore
  1128. */
  1129. public function complete_multipart_upload($bucket, $object, $upload_id, $parts, $options = NULL){
  1130. //options
  1131. $this->validate_options($options);
  1132. if(!$options){
  1133. $options = array();
  1134. }
  1135. //bucket
  1136. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1137. //object
  1138. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  1139. $options[self::OSS_METHOD] = self::OSS_HTTP_POST;
  1140. $options[self::OSS_BUCKET] = $bucket;
  1141. $options[self::OSS_OBJECT] = $object;
  1142. $options[self::OSS_UPLOAD_ID] = $upload_id;
  1143. $options[self::OSS_CONTENT_TYPE] = 'application/xml';
  1144. if(is_string($parts)){
  1145. $options[self::OSS_CONTENT] = $parts;
  1146. }else if($parts instanceof SimpleXMLElement){
  1147. $options[self::OSS_CONTENT] = $parts->asXML();
  1148. }else if((is_array($parts) || $parts instanceof ResponseCore)){
  1149. $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><CompleteMultipartUpload></CompleteMultipartUpload>');
  1150. if (is_array($parts)){
  1151. //生成关联的xml
  1152. foreach ($parts as $node){
  1153. $part = $xml->addChild('Part');
  1154. $part->addChild('PartNumber', $node['PartNumber']);
  1155. $part->addChild('ETag', $node['ETag']);
  1156. }
  1157. }elseif ($parts instanceof ResponseCore){
  1158. foreach ($parts->body->Part as $node){
  1159. $part = $xml->addChild('Part');
  1160. $part->addChild('PartNumber', (string) $node->PartNumber);
  1161. $part->addChild('ETag', (string) $node->ETag);
  1162. }
  1163. }
  1164. $options[self::OSS_CONTENT] = $xml->asXML();
  1165. }
  1166. return $this->auth($options);
  1167. }
  1168. /**
  1169. * 列出multipart上传
  1170. * @param string $bucket (Requeired) bucket
  1171. * @param array $options (Optional) 关联数组
  1172. * @author xiaobing.meng@alibaba-inc.com
  1173. * @since 2012-03-05
  1174. * @return ResponseCore
  1175. */
  1176. public function list_multipart_uploads($bucket, $options = null){
  1177. //options
  1178. $this->validate_options($options);
  1179. if(!$options){
  1180. $options = array();
  1181. }
  1182. //bucket
  1183. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1184. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1185. $options[self::OSS_BUCKET] = $bucket;
  1186. $options[self::OSS_OBJECT] = '/';
  1187. $options[self::OSS_SUB_RESOURCE] = 'uploads';
  1188. foreach (array('key-marker', 'max-uploads', 'upload-id-marker') as $param){
  1189. if (isset($options[$param])){
  1190. $options[self::OSS_QUERY_STRING][$param] = $options[$param];
  1191. unset($options[$param]);
  1192. }
  1193. }
  1194. return $this->auth($options);
  1195. }
  1196. /**
  1197. * multipart上传统一封装,从初始化到完成multipart,以及出错后中止动作
  1198. * @param unknown_type $bucket
  1199. * @param unknown_type $object
  1200. * @param unknown_type $options
  1201. * @author xiaobing.meng@alibaba-inc.com
  1202. * @since 2012-03-05
  1203. * @return ResponseCore
  1204. */
  1205. public function create_mpu_object($bucket, $object, $options = null){
  1206. //options
  1207. $this->validate_options($options);
  1208. if(!$options){
  1209. $options = array();
  1210. }
  1211. //bucket
  1212. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1213. //object
  1214. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  1215. if(isset($options[self::OSS_LENGTH])){
  1216. $options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH];
  1217. unset($options[self::OSS_LENGTH]);
  1218. }
  1219. if(isset($options[self::OSS_FILE_UPLOAD])){
  1220. if($this->chk_chinese($options[self::OSS_FILE_UPLOAD])){
  1221. $options[self::OSS_FILE_UPLOAD] = mb_convert_encoding($options[self::OSS_FILE_UPLOAD],'UTF-8');
  1222. }
  1223. }
  1224. if(!isset($options[self::OSS_FILE_UPLOAD])){
  1225. throw new OSS_Exception('The `fileUpload` option is required in ' . __FUNCTION__ . '().');
  1226. }elseif (is_resource($options[self::OSS_FILE_UPLOAD])){
  1227. $upload_position = isset($options[self::OSS_SEEK_TO]) ? (integer) $options[self::OSS_SEEK_TO] : ftell($options[self::OSS_FILE_UPLOAD]);
  1228. $upload_filesize = isset($options[self::OSS_CONTENT_LENGTH]) ? (integer) $options[self::OSS_CONTENT_LENGTH] : null;
  1229. if (!isset($upload_filesize) && $upload_position !== false){
  1230. $stats = fstat($options[self::OSS_FILE_UPLOAD]);
  1231. if ($stats && $stats[self::OSS_SIZE] >= 0){
  1232. $upload_filesize = $stats[self::OSS_SIZE] - $upload_position;
  1233. }
  1234. }
  1235. }else{
  1236. $upload_position = isset($options[self::OSS_SEEK_TO]) ? (integer) $options[self::OSS_SEEK_TO] : 0;
  1237. if (isset($options[self::OSS_CONTENT_TYPE])){
  1238. $upload_filesize = (integer) $options[self::OSS_CONTENT_TYPE];
  1239. }
  1240. else{
  1241. $upload_filesize = filesize($options[self::OSS_FILE_UPLOAD]);
  1242. if ($upload_filesize !== false){
  1243. $upload_filesize -= $upload_position;
  1244. }
  1245. }
  1246. }
  1247. if ($upload_position === false || !isset($upload_filesize) || $upload_filesize === false || $upload_filesize < 0){
  1248. throw new OSS_Exception('The size of `fileUpload` cannot be determined in ' . __FUNCTION__ . '().');
  1249. }
  1250. // 处理partSize
  1251. if (isset($options[self::OSS_PART_SIZE])){
  1252. // 小于5M
  1253. if ((integer) $options[self::OSS_PART_SIZE] <= 5242880){
  1254. $options[self::OSS_PART_SIZE] = 5242880; // 5 MB
  1255. }
  1256. // 大于500M
  1257. elseif ((integer) $options[self::OSS_PART_SIZE] > 524288000){
  1258. $options[self::OSS_PART_SIZE] = 524288000; // 500 MB
  1259. }
  1260. }
  1261. else{
  1262. $options[self::OSS_PART_SIZE] = 52428800; // 50 MB
  1263. }
  1264. // 如果上传的文件小于partSize,则直接使用普通方式上传
  1265. if ($upload_filesize < $options[self::OSS_PART_SIZE] && !isset($options['uploadId'])){
  1266. return $this->upload_file_by_file($bucket, $object, $options[self::OSS_FILE_UPLOAD]);
  1267. }
  1268. // 初始化multipart
  1269. if (isset($options['uploadId'])){
  1270. $upload_id = $options['uploadId'];
  1271. }else{
  1272. //初始化
  1273. $upload = $this->initiate_multipart_upload($bucket, $object);
  1274. if (!$upload->isOK()){
  1275. throw new OSS_Exception('Init multi-part upload failed...');
  1276. }
  1277. $xml = new SimpleXmlIterator($upload->body);
  1278. $uploadId = (string)$xml->UploadId;
  1279. }
  1280. // 或的分片
  1281. $pieces = $this->get_multipart_counts($upload_filesize, (integer) $options[self::OSS_PART_SIZE]);
  1282. $response_upload_part = array();
  1283. foreach ($pieces as $i => $piece){
  1284. $response_upload_part[] = $this->upload_part($bucket, $object, $uploadId, array(
  1285. //'expect' => '100-continue',
  1286. self::OSS_FILE_UPLOAD => $options[self::OSS_FILE_UPLOAD],
  1287. 'partNumber' => ($i + 1),
  1288. self::OSS_SEEK_TO => $upload_position + (integer) $piece[self::OSS_SEEK_TO],
  1289. self::OSS_LENGTH => (integer) $piece[self::OSS_LENGTH],
  1290. ));
  1291. }
  1292. $upload_parts = array();
  1293. $upload_part_result = true;
  1294. foreach ($response_upload_part as $i=>$response){
  1295. $upload_part_result = $upload_part_result && $response->isOk();
  1296. }
  1297. if(!$upload_part_result){
  1298. throw new OSS_Exception('any part upload failed...,pls try again');
  1299. }
  1300. foreach ($response_upload_part as $i=>$response){
  1301. $upload_parts[] = array(
  1302. 'PartNumber' => ($i + 1),
  1303. 'ETag' => (string) $response->header['etag']
  1304. );
  1305. }
  1306. return $this->complete_multipart_upload($bucket, $object, $uploadId, $upload_parts);
  1307. }
  1308. /**
  1309. * 通过Multi-Part方式上传整个目录,其中的object默认为文件名
  1310. * @param string $bucket (Required)
  1311. * @param string $dir (Required) 必选
  1312. * @param boolean $recursive (Optional) 是否递归,如果为true,则递归读取所有目录,默认为不递归读取
  1313. * @param string $exclude 需要过滤的文件
  1314. * @param array $options (Optional) 关联数组
  1315. * @author xiaobing.meng@alibaba-inc.com
  1316. * @since 2012-03-05
  1317. * @return ResponseCore
  1318. */
  1319. public function create_mtu_object_by_dir($bucket,$dir,$recursive = false,$exclude = ".|..|.svn",$options = null){
  1320. //options
  1321. $this->validate_options($options);
  1322. //bucket
  1323. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1324. if($this->chk_chinese($dir)){
  1325. $dir = iconv('utf-8','gbk',$dir);
  1326. }
  1327. //判断是否目录
  1328. if(!is_dir($dir)){
  1329. throw new OSS_Exception($dir.' is not a directory...,pls check it');
  1330. }
  1331. $file_list_array = $this->read_dir($dir,$exclude,$recursive);
  1332. if(!$file_list_array){
  1333. throw new OSS_Exception($dir.' is empty...');
  1334. }
  1335. $index = 1;
  1336. foreach ($file_list_array as $item){
  1337. $options = array(
  1338. self::OSS_FILE_UPLOAD => $item['path'],
  1339. self::OSS_PART_SIZE => 5242880,
  1340. );
  1341. echo $index++.". ";
  1342. $response = $this->create_mpu_object($bucket, $item['file'],$options);
  1343. if($response->isOK()){
  1344. echo "Upload file {".$item['path']." } successful..\n";
  1345. }else{
  1346. echo "Upload file {".$item['path']." } failed..\n";
  1347. continue;
  1348. }
  1349. }
  1350. }
  1351. /**
  1352. * 通过multi-part方式上传目录(优化版)
  1353. * $options = array(
  1354. * 'bucket' => (Required)
  1355. * 'object' => (Optional)
  1356. * 'directory' => (Required)
  1357. * 'exclude' => (Optional)
  1358. * 'recursive' => (Optional)
  1359. * )
  1360. */
  1361. public function batch_upload_file($options = NULL){
  1362. if((NULL == $options) || !isset($options['bucket']) || empty($options['bucket']) || !isset($options['directory']) ||empty($options['directory']) ) {
  1363. throw new OSS_Exception('Bad Request',400);
  1364. }
  1365. $bucket = $options['bucket']; unset($options['bucket']);
  1366. $directory = $options['directory']; unset($options['directory']);
  1367. if($this->chk_chinese($directory)){
  1368. $directory = iconv('utf-8','gbk',$directory);
  1369. }
  1370. //判断是否目录
  1371. if(!is_dir($directory)){
  1372. throw new OSS_Exception($dir.' is not a directory...,pls check it');
  1373. }
  1374. $object = '';
  1375. if(isset($options['object'])){
  1376. $object = $options['object'];
  1377. unset($options['object']);
  1378. }
  1379. $exclude = '.|..|.svn';
  1380. if (isset($options['exclude']) && !empty($options['exclude'])){
  1381. $exclude = $options['exclude'];
  1382. unset($options['exclude']);
  1383. }
  1384. $recursive = false;
  1385. if(isset($options['recursive']) && !empty($options['recursive'])){
  1386. if(in_array($options['recursive'],array(true,false))){
  1387. $recursive = $options['recursive'];
  1388. }
  1389. unset($options['recursive']);
  1390. }
  1391. //read directory
  1392. $file_list_array = $this->read_dir($directory,$exclude,$recursive);
  1393. if(!$file_list_array){
  1394. throw new OSS_Exception($directory.' is empty...');
  1395. }
  1396. $index = 1;
  1397. foreach ($file_list_array as $item){
  1398. $options = array(
  1399. self::OSS_FILE_UPLOAD => $item['path'],
  1400. self::OSS_PART_SIZE => 5242880,
  1401. );
  1402. echo $index++.". ";
  1403. $response = $this->create_mpu_object($bucket, (!empty($object)?$object.'/':'').$item['file'],$options);
  1404. if($response->isOK()){
  1405. echo "Upload file {".$item['path']." } successful..\n";
  1406. }else{
  1407. echo "Upload file {".$item['path']." } failed..\n";
  1408. continue;
  1409. }
  1410. }
  1411. }
  1412. /*%******************************************************************************************************%*/
  1413. //Object Group相关操作
  1414. /**
  1415. * 创建Object Group
  1416. * @param string $object_group (Required) Object Group名称
  1417. * @param string $bucket (Required) Bucket名称
  1418. * @param array $object_arry (Required) object数组,所有的object必须在同一个bucket下
  1419. * 其中$object 数组的格式如下:
  1420. * $object = array(
  1421. * $object1,
  1422. * $object2,
  1423. * ...
  1424. * )
  1425. * @param array $options (Optional)
  1426. * @author xiaobing.meng@alibaba-inc.com
  1427. * @since 2011-11-14
  1428. * @return ResponseCore
  1429. */
  1430. public function create_object_group($bucket,$object_group ,$object_arry,$options = NULL){
  1431. //options
  1432. $this->validate_options($options);
  1433. //bucket
  1434. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1435. //object group
  1436. $this->is_empty($object_group,OSS_OBJECT_GROUP_IS_NOT_ALLOWED_EMPTY);
  1437. $options[self::OSS_BUCKET] = $bucket;
  1438. $options[self::OSS_METHOD] = 'POST';
  1439. $options[self::OSS_OBJECT] = $object_group;
  1440. $options[self::OSS_CONTENT_TYPE] = 'txt/xml'; //重设Content-Type
  1441. $options[self::OSS_SUB_RESOURCE] = 'group'; //设置?group
  1442. $options[self::OSS_CONTENT] = $this->make_object_group_xml($bucket,$object_arry); //格式化xml
  1443. $response = $this->auth ( $options );
  1444. return $response;
  1445. }
  1446. /**
  1447. * 获取Object Group
  1448. * @param string $object_group (Required)
  1449. * @param string $bucket (Required)
  1450. * @param array $options (Optional)
  1451. * @author xiaobing.meng@alibaba-inc.com
  1452. * @since 2011-11-14
  1453. * @return ResponseCore
  1454. */
  1455. public function get_object_group($bucket,$object_group,$options = NULL){
  1456. //options
  1457. $this->validate_options($options);
  1458. //bucket
  1459. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1460. //object group
  1461. $this->is_empty($object_group,OSS_OBJECT_GROUP_IS_NOT_ALLOWED_EMPTY);
  1462. $options[self::OSS_BUCKET] = $bucket;
  1463. $options[self::OSS_METHOD] = 'GET';
  1464. $options[self::OSS_OBJECT] = $object_group;
  1465. //$options[self::OSS_OBJECT_GROUP] = true; //设置?group
  1466. //$options[self::OSS_CONTENT_TYPE] = 'txt/xml'; //重设Content-Type
  1467. $options[self::OSS_HEADERS] = array(self::OSS_OBJECT_GROUP => self::OSS_OBJECT_GROUP); //header中的x-oss-file-group不能为空,否则返回值错误
  1468. $response = $this->auth ( $options );
  1469. return $response;
  1470. }
  1471. /**
  1472. * 获取Object Group 的Object List信息
  1473. * @param string $object_group (Required)
  1474. * @param string $bucket (Required)
  1475. * @param array $options (Optional)
  1476. * @author xiaobing.meng@alibaba-inc.com
  1477. * @since 2011-11-14
  1478. * @return ResponseCore
  1479. */
  1480. public function get_object_group_index($bucket,$object_group,$options = NULL){
  1481. //options
  1482. $this->validate_options($options);
  1483. //bucket
  1484. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1485. //object group
  1486. $this->is_empty($object_group,OSS_OBJECT_GROUP_IS_NOT_ALLOWED_EMPTY);
  1487. $options[self::OSS_BUCKET] = $bucket;
  1488. $options[self::OSS_METHOD] = 'GET';
  1489. $options[self::OSS_OBJECT] = $object_group;
  1490. $options[self::OSS_CONTENT_TYPE] = 'application/xml'; //重设Content-Type
  1491. //$options[self::OSS_OBJECT_GROUP] = true; //设置?group
  1492. $options[self::OSS_HEADERS] = array(self::OSS_OBJECT_GROUP => self::OSS_OBJECT_GROUP);
  1493. $response = $this->auth ( $options );
  1494. return $response;
  1495. }
  1496. /**
  1497. * 获得object group的meta信息
  1498. * @param string $bucket (Required)
  1499. * @param string $object_group (Required)
  1500. * @param string $options (Optional)
  1501. * @author xiaobing.meng@alibaba-inc.com
  1502. * @since 2011-11-14
  1503. * @return ResponseCore
  1504. */
  1505. public function get_object_group_meta($bucket,$object_group,$options = NULL){
  1506. //options
  1507. $this->validate_options($options);
  1508. if(!$options){
  1509. $options = array();
  1510. }
  1511. //bucket
  1512. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1513. //object group
  1514. $this->is_empty($object_group,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  1515. $options[self::OSS_BUCKET] = $bucket;
  1516. $options[self::OSS_METHOD] = 'HEAD';
  1517. $options[self::OSS_OBJECT] = $object_group;
  1518. $options[self::OSS_CONTENT_TYPE] = 'application/xml'; //重设Content-Type
  1519. //$options[self::OSS_SUB_RESOURCE] = 'group'; //设置?group
  1520. $options[self::OSS_HEADERS] = array(self::OSS_OBJECT_GROUP => self::OSS_OBJECT_GROUP);
  1521. $response = $this->auth ( $options );
  1522. return $response;
  1523. }
  1524. /**
  1525. * 删除Object Group
  1526. * @param string $bucket(Required)
  1527. * @param string $object_group (Required)
  1528. * @param array $options (Optional)
  1529. * @author xiaobing.meng@alibaba-inc.com
  1530. * @since 2011-11-14
  1531. * @return ResponseCore
  1532. */
  1533. public function delete_object_group($bucket,$object_group,$options = NULL){
  1534. //options
  1535. $this->validate_options($options);
  1536. if(!$options){
  1537. $options = array();
  1538. }
  1539. //bucket
  1540. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1541. //object group
  1542. $this->is_empty($object_group,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  1543. $options[self::OSS_BUCKET] = $bucket;
  1544. $options[self::OSS_METHOD] = 'DELETE';
  1545. $options[self::OSS_OBJECT] = $object_group;
  1546. $response = $this->auth ( $options );
  1547. return $response;
  1548. }
  1549. /*%******************************************************************************************************%*/
  1550. //带签名的url相关
  1551. /**
  1552. * 获取带签名的url
  1553. * @param string $bucket (Required)
  1554. * @param string $object (Required)
  1555. * @param int $timeout (Optional)
  1556. * @param array $options (Optional)
  1557. * @author xiaobing.meng@alibaba-inc.com
  1558. * @since 2011-12-21
  1559. * @return string
  1560. */
  1561. public function get_sign_url($bucket,$object,$timeout = 60,$options = NULL){
  1562. //options
  1563. $this->validate_options($options);
  1564. if(!$options){
  1565. $options = array();
  1566. }
  1567. //bucket
  1568. $this->is_empty($bucket,OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
  1569. //object
  1570. $this->is_empty($object,OSS_OBJECT_IS_NOT_ALLOWED_EMPTY);
  1571. $options[self::OSS_BUCKET] = $bucket;
  1572. $options[self::OSS_OBJECT] = $object;
  1573. $options[self::OSS_METHOD] = self::OSS_HTTP_GET;
  1574. $options[self::OSS_CONTENT_TYPE] = '';
  1575. $timeout = time() + $timeout;
  1576. $options[self::OSS_PREAUTH] = $timeout;
  1577. $options[self::OSS_DATE] = $timeout;
  1578. return $this->auth($options);
  1579. }
  1580. /*%******************************************************************************************************%*/
  1581. //日志相关
  1582. /**
  1583. * 记录日志
  1584. * @param string $msg (Required)
  1585. * @throws OSS_Exception
  1586. * @author xiaobing.meng@alibaba-inc.com
  1587. * @since 2011-12-27
  1588. * @return void
  1589. */
  1590. private function log($msg){
  1591. if(defined('ALI_LOG_PATH') ){
  1592. $log_path = ALI_LOG_PATH;
  1593. if(empty($log_path) || !file_exists($log_path)){
  1594. throw new OSS_Exception($log_path.OSS_LOG_PATH_NOT_EXIST);
  1595. }
  1596. }else{
  1597. $log_path = dirname(__FILE__).DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR;
  1598. }
  1599. //检测日志目录是否存在
  1600. if(!file_exists($log_path)){
  1601. throw new OSS_Exception(OSS_LOG_PATH_NOT_EXIST);
  1602. }
  1603. $log_name = $log_path.'oss_sdk_php_'.date('Y-m-d').'.log';
  1604. if(ALI_DISPLAY_LOG){
  1605. echo $msg."\n<br/>";
  1606. }
  1607. if(ALI_LOG){
  1608. if(!error_log(date('Y-m-d H:i:s')." : ".$msg."\n", 3,$log_name)){
  1609. throw new OSS_Exception(OSS_WRITE_LOG_TO_FILE_FAILED);
  1610. }
  1611. }
  1612. }
  1613. /*%******************************************************************************************************%*/
  1614. //工具类相关
  1615. /**
  1616. * 生成query params
  1617. * @param array $array 关联数组
  1618. * @author xiaobing.meng@alibaba-inc.com
  1619. * @since 2012-03-04
  1620. * @return string 返回诸如 key1=value1&key2=value2
  1621. */
  1622. public function to_query_string($options = array()){
  1623. $temp = array();
  1624. foreach ($options as $key => $value){
  1625. if (is_string($key) && !is_array($value)){
  1626. $temp[] = rawurlencode($key) . '=' . rawurlencode($value);
  1627. }
  1628. }
  1629. return implode('&', $temp);
  1630. }
  1631. /**
  1632. * 转化十六进制的数据为base64
  1633. *
  1634. * @param string $str (Required) 要转化的字符串
  1635. * @author xiaobing.meng@alibaba-inc.com
  1636. * @since 2012-03-20
  1637. * @return string Base64-encoded string.
  1638. */
  1639. private function hex_to_base64($str){
  1640. $result = '';
  1641. for ($i = 0; $i < strlen($str); $i += 2){
  1642. $result .= chr(hexdec(substr($str, $i, 2)));
  1643. }
  1644. return base64_encode($result);
  1645. }
  1646. private function s_replace($subject){
  1647. $search = array('<','>','&','\'','"');
  1648. $replace = array('&lt;','&gt;','&amp;','&apos;','&quot;');
  1649. return str_replace($search, $replace, $subject);
  1650. }
  1651. /**
  1652. * 检测是否含有中文
  1653. * @param string $subject
  1654. * @author xiaobing.meng@alibaba-inc.com
  1655. * @since 2012-06-06
  1656. * @return boolean
  1657. */
  1658. private function chk_chinese($str){
  1659. return preg_match('/[\x80-\xff]./', $str);
  1660. }
  1661. /**
  1662. * 检测是否GB2312编码
  1663. * @param string $str
  1664. * @author xiaobing.meng@alibaba-inc.com
  1665. * @since 2012-03-20
  1666. * @return boolean false UTF-8编码 TRUE GB2312编码
  1667. */
  1668. function is_gb2312($str) {
  1669. for($i=0; $i<strlen($str); $i++) {
  1670. $v = ord( $str[$i] );
  1671. if( $v > 127) {
  1672. if( ($v >= 228) && ($v <= 233) ){
  1673. if( ($i+2) >= (strlen($str) - 1)) return true; // not enough characters
  1674. $v1 = ord( $str[$i+1] );
  1675. $v2 = ord( $str[$i+2] );
  1676. if( ($v1 >= 128) && ($v1 <=191) && ($v2 >=128) && ($v2 <= 191) )
  1677. return false; //UTF-8编码
  1678. else
  1679. return true; //GB编码
  1680. }
  1681. }
  1682. }
  1683. }
  1684. /**
  1685. * 检测是否GBK编码
  1686. * @param string $str
  1687. * @param boolean $gbk
  1688. * @author xiaobing.meng@alibaba-inc.com
  1689. * @since 2012-06-04
  1690. * @return boolean
  1691. */
  1692. private function check_char($str,$gbk = true){
  1693. for($i=0; $i<strlen($str); $i++) {
  1694. $v = ord( $str[$i] );
  1695. if( $v > 127){
  1696. if( ($v >= 228) && ($v <= 233) ){
  1697. if(($i+2)>= (strlen($str)-1)) return $gbk?true:FALSE; // not enough characters
  1698. $v1 = ord( $str[$i+1] ); $v2 = ord( $str[$i+2] );
  1699. if($gbk){
  1700. return (($v1 >= 128) && ($v1 <=191) && ($v2 >=128) && ($v2 <= 191))?FALSE:TRUE;//GBK
  1701. }else{
  1702. return (($v1 >= 128) && ($v1 <=191) && ($v2 >=128) && ($v2 <= 191))?TRUE:FALSE;
  1703. }
  1704. }
  1705. }
  1706. }
  1707. return $gbk?TRUE:FALSE;
  1708. }
  1709. /**
  1710. * 读取目录
  1711. * @param string $dir (Required) 目录名
  1712. * @param boolean $recursive (Optional) 是否递归,默认为false
  1713. * @author xiaobing.meng@alibaba-inc.com
  1714. * @since 2012-03-05
  1715. * @return array
  1716. */
  1717. private function read_dir($dir,$exclude = ".|..|.svn",$recursive = false){
  1718. static $file_list_array = array();
  1719. $exclude_array = explode("|", $exclude);
  1720. //读取目录
  1721. if($handle = opendir($dir)){
  1722. while ( false !== ($file = readdir($handle))){
  1723. if(!in_array(strtolower($file),$exclude_array)){
  1724. $new_file = $dir.'/'.$file;
  1725. if(is_dir($new_file) && $recursive){
  1726. $this->read_dir($new_file,$exclude,$recursive);
  1727. }else{
  1728. $file_list_array[] = array(
  1729. 'path' => $new_file,
  1730. 'file' => $file,
  1731. );
  1732. }
  1733. }
  1734. }
  1735. closedir($handle);
  1736. }
  1737. return $file_list_array;
  1738. }
  1739. /**
  1740. * 转化object数组为固定个xml格式
  1741. * @param string $bucket (Required)
  1742. * @param array $object_array (Required)
  1743. * @throws OSS_Exception
  1744. * @author xiaobing.meng@alibaba-inc.com
  1745. * @since 2011-12-27
  1746. * @return string
  1747. */
  1748. private function make_object_group_xml($bucket,$object_array){
  1749. $xml = '';
  1750. $xml .= '<CreateFileGroup>';
  1751. if($object_array){
  1752. if(count($object_array) > self::OSS_MAX_OBJECT_GROUP_VALUE){
  1753. throw new OSS_Exception(OSS_OBJECT_GROUP_TOO_MANY_OBJECT, '-401');
  1754. }
  1755. $index = 1;
  1756. foreach ($object_array as $key=>$value){
  1757. $object_meta = (array)$this->get_object_meta($bucket, $value);
  1758. if(isset($object_meta) && isset($object_meta['status']) && isset($object_meta['header']) && isset($object_meta['header']['etag']) && $object_meta['status'] == 200){
  1759. $xml .= '<Part>';
  1760. $xml .= '<PartNumber>'.intval($index).'</PartNumber>';
  1761. $xml .= '<PartName>'.$value.'</PartName>';
  1762. $xml .= '<ETag>'.$object_meta['header']['etag'].'</ETag>';
  1763. $xml .= '</Part>';
  1764. $index++;
  1765. }
  1766. }
  1767. }else{
  1768. throw new OSS_Exception(OSS_OBJECT_ARRAY_IS_EMPTY, '-400');
  1769. }
  1770. $xml .= '</CreateFileGroup>';
  1771. return $xml;
  1772. }
  1773. /**
  1774. * 检验bucket名称是否合法
  1775. * bucket的命名规范:
  1776. * 1. 只能包括小写字母,数字
  1777. * 2. 必须以小写字母或者数字开头
  1778. * 3. 长度必须在3-63字节之间
  1779. * @param string $bucket (Required)
  1780. * @author xiaobing.meng@alibaba-inc.com
  1781. * @since 2011-12-27
  1782. * @return boolean
  1783. */
  1784. private function validate_bucket($bucket){
  1785. $pattern = '/^[a-z0-9][a-z0-9-]{2,62}$/';
  1786. if (! preg_match ( $pattern, $bucket )) {
  1787. return false;
  1788. }
  1789. return true;
  1790. }
  1791. /**
  1792. * 检验object名称是否合法
  1793. * object命名规范:
  1794. * 1. 规则长度必须在1-1023字节之间
  1795. * 2. 使用UTF-8编码
  1796. * @param string $object (Required)
  1797. * @author xiaobing.meng@alibaba-inc.com
  1798. * @since 2011-12-27
  1799. * @return boolean
  1800. */
  1801. private function validate_object($object){
  1802. $pattern = '/^.{1,1023}$/';
  1803. if (empty ( $object ) || ! preg_match ( $pattern, $object )) {
  1804. return false;
  1805. }
  1806. return true;
  1807. }
  1808. /**
  1809. * 检验$options
  1810. * @param array $options (Optional)
  1811. * @throws OSS_Exception
  1812. * @author xiaobing.meng@alibaba-inc.com
  1813. * @since 2011-12-27
  1814. * @return boolean
  1815. */
  1816. private function validate_options($options){
  1817. //$options
  1818. if ($options != NULL && ! is_array ( $options )) {
  1819. throw new OSS_Exception ($options.':'.OSS_OPTIONS_MUST_BE_ARRAY);
  1820. }
  1821. }
  1822. /**
  1823. * 检测上传文件的内容
  1824. * @param array $options (Optional)
  1825. * @throws OSS_Exception
  1826. * @author xiaobing.meng@alibaba-inc.com
  1827. * @since 2011-12-27
  1828. * @return string
  1829. */
  1830. private function validate_content($options){
  1831. if(isset($options[self::OSS_CONTENT])){
  1832. if($options[self::OSS_CONTENT] == '' || !is_string($options[self::OSS_CONTENT])){
  1833. throw new OSS_Exception(OSS_INVALID_HTTP_BODY_CONTENT,'-600');
  1834. }
  1835. }else{
  1836. throw new OSS_Exception(OSS_NOT_SET_HTTP_CONTENT, '-601');
  1837. }
  1838. }
  1839. /**
  1840. * 验证文件长度
  1841. * @param array $options (Optional)
  1842. * @author xiaobing.meng@alibaba-inc.com
  1843. * @since 2011-12-27
  1844. * @return void
  1845. */
  1846. private function validate_content_length($options){
  1847. if(isset($options[self::OSS_LENGTH]) && is_numeric($options[self::OSS_LENGTH])){
  1848. if( ! $options[self::OSS_LENGTH] > 0){
  1849. throw new OSS_Exception(OSS_CONTENT_LENGTH_MUST_MORE_THAN_ZERO, '-602');
  1850. }
  1851. }else{
  1852. throw new OSS_Exception(OSS_INVALID_CONTENT_LENGTH, '-602');
  1853. }
  1854. }
  1855. /**
  1856. * 校验BUCKET/OBJECT/OBJECT GROUP是否为空
  1857. * @param string $name (Required)
  1858. * @param string $errMsg (Required)
  1859. * @throws OSS_Exception
  1860. * @author xiaobing.meng@alibaba-inc.com
  1861. * @since 2011-12-27
  1862. * @return void
  1863. */
  1864. private function is_empty($name,$errMsg){
  1865. if(empty($name)){
  1866. throw new OSS_Exception($errMsg);
  1867. }
  1868. }
  1869. /**
  1870. * 设置http header
  1871. * @param string $key (Required)
  1872. * @param string $value (Required)
  1873. * @param array $options (Required)
  1874. * @throws OSS_Exception
  1875. * @author xiaobing.meng@alibaba-inc.com
  1876. * @return void
  1877. */
  1878. private static function set_options_header($key, $value, &$options) {
  1879. if (isset ( $options [self::OSS_HEADERS] )) {
  1880. if (! is_array ( $options [self::OSS_HEADERS] )) {
  1881. throw new OSS_Exception(OSS_INVALID_OPTION_HEADERS, '-600');
  1882. }
  1883. } else {
  1884. $options [self::OSS_HEADERS] = array ();
  1885. }
  1886. $options [self::OSS_HEADERS] [$key] = $value;
  1887. }
  1888. }