Object.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use OSS\OssClient;
  4. use OSS\Core\OssException;
  5. $bucket = Common::getBucketName();
  6. $ossClient = Common::getOssClient();
  7. if (is_null($ossClient)) exit(1);
  8. //******************************* Simple usage ***************************************************************
  9. // Upload the in-memory string (hi, oss) to an OSS file
  10. $result = $ossClient->putObject($bucket, "b.file", "hi, oss");
  11. Common::println("b.file is created");
  12. Common::println($result['x-oss-request-id']);
  13. Common::println($result['etag']);
  14. Common::println($result['content-md5']);
  15. Common::println($result['body']);
  16. // Uploads a local file to an OSS file
  17. $result = $ossClient->uploadFile($bucket, "c.file", __FILE__);
  18. Common::println("c.file is created");
  19. Common::println("b.file is created");
  20. Common::println($result['x-oss-request-id']);
  21. Common::println($result['etag']);
  22. Common::println($result['content-md5']);
  23. Common::println($result['body']);
  24. // Download an oss object as an in-memory variable
  25. $content = $ossClient->getObject($bucket, "b.file");
  26. Common::println("b.file is fetched, the content is: " . $content);
  27. // Add a symlink to an object
  28. $content = $ossClient->putSymlink($bucket, "test-symlink", "b.file");
  29. Common::println("test-symlink is created");
  30. Common::println($result['x-oss-request-id']);
  31. Common::println($result['etag']);
  32. // Get a symlink
  33. $content = $ossClient->getSymlink($bucket, "test-symlink");
  34. Common::println("test-symlink refer to : " . $content[OssClient::OSS_SYMLINK_TARGET]);
  35. // Download an object to a local file.
  36. $options = array(
  37. OssClient::OSS_FILE_DOWNLOAD => "./c.file.localcopy",
  38. );
  39. $ossClient->getObject($bucket, "c.file", $options);
  40. Common::println("b.file is fetched to the local file: c.file.localcopy");
  41. Common::println("b.file is created");
  42. // Copy an object
  43. $result = $ossClient->copyObject($bucket, "c.file", $bucket, "c.file.copy");
  44. Common::println("lastModifiedTime: " . $result[0]);
  45. Common::println("ETag: " . $result[1]);
  46. // Check whether an object exists
  47. $doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy");
  48. Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no"));
  49. // Delete an object
  50. $result = $ossClient->deleteObject($bucket, "c.file.copy");
  51. Common::println("c.file.copy is deleted");
  52. Common::println("b.file is created");
  53. Common::println($result['x-oss-request-id']);
  54. // Check whether an object exists
  55. $doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy");
  56. Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no"));
  57. // Delete multiple objects in batch
  58. $result = $ossClient->deleteObjects($bucket, array("b.file", "c.file"));
  59. foreach($result as $object)
  60. Common::println($object);
  61. sleep(2);
  62. unlink("c.file.localcopy");
  63. //******************************* For complete usage, see the following functions ****************************************************
  64. listObjects($ossClient, $bucket);
  65. listAllObjects($ossClient, $bucket);
  66. createObjectDir($ossClient, $bucket);
  67. putObject($ossClient, $bucket);
  68. uploadFile($ossClient, $bucket);
  69. getObject($ossClient, $bucket);
  70. getObjectToLocalFile($ossClient, $bucket);
  71. copyObject($ossClient, $bucket);
  72. modifyMetaForObject($ossClient, $bucket);
  73. getObjectMeta($ossClient, $bucket);
  74. deleteObject($ossClient, $bucket);
  75. deleteObjects($ossClient, $bucket);
  76. doesObjectExist($ossClient, $bucket);
  77. getSymlink($ossClient, $bucket);
  78. putSymlink($ossClient, $bucket);
  79. /**
  80. * Create a 'virtual' folder
  81. *
  82. * @param OssClient $ossClient OssClient instance
  83. * @param string $bucket bucket name
  84. * @return null
  85. */
  86. function createObjectDir($ossClient, $bucket)
  87. {
  88. try {
  89. $ossClient->createObjectDir($bucket, "dir");
  90. } catch (OssException $e) {
  91. printf(__FUNCTION__ . ": FAILED\n");
  92. printf($e->getMessage() . "\n");
  93. return;
  94. }
  95. print(__FUNCTION__ . ": OK" . "\n");
  96. }
  97. /**
  98. * Upload in-memory data to oss
  99. *
  100. * Simple upload---upload specified in-memory data to an OSS object
  101. *
  102. * @param OssClient $ossClient OssClient instance
  103. * @param string $bucket bucket name
  104. * @return null
  105. */
  106. function putObject($ossClient, $bucket)
  107. {
  108. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  109. $content = file_get_contents(__FILE__);
  110. $options = array();
  111. try {
  112. $ossClient->putObject($bucket, $object, $content, $options);
  113. } catch (OssException $e) {
  114. printf(__FUNCTION__ . ": FAILED\n");
  115. printf($e->getMessage() . "\n");
  116. return;
  117. }
  118. print(__FUNCTION__ . ": OK" . "\n");
  119. }
  120. /**
  121. * Uploads a local file to OSS
  122. *
  123. * @param OssClient $ossClient OssClient instance
  124. * @param string $bucket bucket name
  125. * @return null
  126. */
  127. function uploadFile($ossClient, $bucket)
  128. {
  129. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  130. $filePath = __FILE__;
  131. $options = array();
  132. try {
  133. $ossClient->uploadFile($bucket, $object, $filePath, $options);
  134. } catch (OssException $e) {
  135. printf(__FUNCTION__ . ": FAILED\n");
  136. printf($e->getMessage() . "\n");
  137. return;
  138. }
  139. print(__FUNCTION__ . ": OK" . "\n");
  140. }
  141. /**
  142. * Lists all files and folders in the bucket.
  143. * Note if there's more items than the max-keys specified, the caller needs to use the nextMarker returned as the value for the next call's maker paramter.
  144. * Loop through all the items returned from ListObjects.
  145. *
  146. * @param OssClient $ossClient OssClient instance
  147. * @param string $bucket bucket name
  148. * @return null
  149. */
  150. function listObjects($ossClient, $bucket)
  151. {
  152. $prefix = 'oss-php-sdk-test/';
  153. $delimiter = '/';
  154. $nextMarker = '';
  155. $maxkeys = 1000;
  156. $options = array(
  157. 'delimiter' => $delimiter,
  158. 'prefix' => $prefix,
  159. 'max-keys' => $maxkeys,
  160. 'marker' => $nextMarker,
  161. );
  162. try {
  163. $listObjectInfo = $ossClient->listObjects($bucket, $options);
  164. } catch (OssException $e) {
  165. printf(__FUNCTION__ . ": FAILED\n");
  166. printf($e->getMessage() . "\n");
  167. return;
  168. }
  169. print(__FUNCTION__ . ": OK" . "\n");
  170. $objectList = $listObjectInfo->getObjectList(); // object list
  171. $prefixList = $listObjectInfo->getPrefixList(); // directory list
  172. if (!empty($objectList)) {
  173. print("objectList:\n");
  174. foreach ($objectList as $objectInfo) {
  175. print($objectInfo->getKey() . "\n");
  176. }
  177. }
  178. if (!empty($prefixList)) {
  179. print("prefixList: \n");
  180. foreach ($prefixList as $prefixInfo) {
  181. print($prefixInfo->getPrefix() . "\n");
  182. }
  183. }
  184. }
  185. /**
  186. * Lists all folders and files under the bucket. Use nextMarker repeatedly to get all objects.
  187. *
  188. * @param OssClient $ossClient OssClient instance
  189. * @param string $bucket bucket name
  190. * @return null
  191. */
  192. function listAllObjects($ossClient, $bucket)
  193. {
  194. // Create dir/obj 'folder' and put some files into it.
  195. for ($i = 0; $i < 100; $i += 1) {
  196. $ossClient->putObject($bucket, "dir/obj" . strval($i), "hi");
  197. $ossClient->createObjectDir($bucket, "dir/obj" . strval($i));
  198. }
  199. $prefix = 'dir/';
  200. $delimiter = '/';
  201. $nextMarker = '';
  202. $maxkeys = 30;
  203. while (true) {
  204. $options = array(
  205. 'delimiter' => $delimiter,
  206. 'prefix' => $prefix,
  207. 'max-keys' => $maxkeys,
  208. 'marker' => $nextMarker,
  209. );
  210. var_dump($options);
  211. try {
  212. $listObjectInfo = $ossClient->listObjects($bucket, $options);
  213. } catch (OssException $e) {
  214. printf(__FUNCTION__ . ": FAILED\n");
  215. printf($e->getMessage() . "\n");
  216. return;
  217. }
  218. // Get the nextMarker, and it would be used as the next call's marker parameter to resume from the last call
  219. $nextMarker = $listObjectInfo->getNextMarker();
  220. $listObject = $listObjectInfo->getObjectList();
  221. $listPrefix = $listObjectInfo->getPrefixList();
  222. var_dump(count($listObject));
  223. var_dump(count($listPrefix));
  224. if ($nextMarker === '') {
  225. break;
  226. }
  227. }
  228. }
  229. /**
  230. * Get the content of an object.
  231. *
  232. * @param OssClient $ossClient OssClient instance
  233. * @param string $bucket bucket name
  234. * @return null
  235. */
  236. function getObject($ossClient, $bucket)
  237. {
  238. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  239. $options = array();
  240. try {
  241. $content = $ossClient->getObject($bucket, $object, $options);
  242. } catch (OssException $e) {
  243. printf(__FUNCTION__ . ": FAILED\n");
  244. printf($e->getMessage() . "\n");
  245. return;
  246. }
  247. print(__FUNCTION__ . ": OK" . "\n");
  248. if (file_get_contents(__FILE__) === $content) {
  249. print(__FUNCTION__ . ": FileContent checked OK" . "\n");
  250. } else {
  251. print(__FUNCTION__ . ": FileContent checked FAILED" . "\n");
  252. }
  253. }
  254. /**
  255. * Put symlink
  256. *
  257. * @param OssClient $ossClient The Instance of OssClient
  258. * @param string $bucket bucket name
  259. * @return null
  260. */
  261. function putSymlink($ossClient, $bucket)
  262. {
  263. $symlink = "test-samples-symlink";
  264. $object = "test-samples-object";
  265. try {
  266. $ossClient->putObject($bucket, $object, 'test-content');
  267. $ossClient->putSymlink($bucket, $symlink, $object);
  268. $content = $ossClient->getObject($bucket, $symlink);
  269. } catch (OssException $e) {
  270. printf(__FUNCTION__ . ": FAILED\n");
  271. printf($e->getMessage() . "\n");
  272. return;
  273. }
  274. print(__FUNCTION__ . ": OK" . "\n");
  275. if ($content == 'test-content') {
  276. print(__FUNCTION__ . ": putSymlink checked OK" . "\n");
  277. } else {
  278. print(__FUNCTION__ . ": putSymlink checked FAILED" . "\n");
  279. }
  280. }
  281. /**
  282. * Get symlink
  283. *
  284. * @param OssClient $ossClient OssClient instance
  285. * @param string $bucket bucket name
  286. * @return null
  287. */
  288. function getSymlink($ossClient, $bucket)
  289. {
  290. $symlink = "test-samples-symlink";
  291. $object = "test-samples-object";
  292. try {
  293. $ossClient->putObject($bucket, $object, 'test-content');
  294. $ossClient->putSymlink($bucket, $symlink, $object);
  295. $content = $ossClient->getSymlink($bucket, $symlink);
  296. } catch (OssException $e) {
  297. printf(__FUNCTION__ . ": FAILED\n");
  298. printf($e->getMessage() . "\n");
  299. return;
  300. }
  301. print(__FUNCTION__ . ": OK" . "\n");
  302. if ($content[OssClient::OSS_SYMLINK_TARGET]) {
  303. print(__FUNCTION__ . ": getSymlink checked OK" . "\n");
  304. } else {
  305. print(__FUNCTION__ . ": getSymlink checked FAILED" . "\n");
  306. }
  307. }
  308. /**
  309. * Get_object_to_local_file
  310. *
  311. * Get object
  312. * Download object to a specified file.
  313. *
  314. * @param OssClient $ossClient OssClient instance
  315. * @param string $bucket bucket name
  316. * @return null
  317. */
  318. function getObjectToLocalFile($ossClient, $bucket)
  319. {
  320. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  321. $localfile = "upload-test-object-name.txt";
  322. $options = array(
  323. OssClient::OSS_FILE_DOWNLOAD => $localfile,
  324. );
  325. try {
  326. $ossClient->getObject($bucket, $object, $options);
  327. } catch (OssException $e) {
  328. printf(__FUNCTION__ . ": FAILED\n");
  329. printf($e->getMessage() . "\n");
  330. return;
  331. }
  332. print(__FUNCTION__ . ": OK, please check localfile: 'upload-test-object-name.txt'" . "\n");
  333. if (file_get_contents($localfile) === file_get_contents(__FILE__)) {
  334. print(__FUNCTION__ . ": FileContent checked OK" . "\n");
  335. } else {
  336. print(__FUNCTION__ . ": FileContent checked FAILED" . "\n");
  337. }
  338. if (file_exists($localfile)) {
  339. unlink($localfile);
  340. }
  341. }
  342. /**
  343. * Copy object
  344. * When the source object is same as the target one, copy operation will just update the metadata.
  345. *
  346. * @param OssClient $ossClient OssClient instance
  347. * @param string $bucket bucket name
  348. * @return null
  349. */
  350. function copyObject($ossClient, $bucket)
  351. {
  352. $fromBucket = $bucket;
  353. $fromObject = "oss-php-sdk-test/upload-test-object-name.txt";
  354. $toBucket = $bucket;
  355. $toObject = $fromObject . '.copy';
  356. $options = array();
  357. try {
  358. $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $options);
  359. } catch (OssException $e) {
  360. printf(__FUNCTION__ . ": FAILED\n");
  361. printf($e->getMessage() . "\n");
  362. return;
  363. }
  364. print(__FUNCTION__ . ": OK" . "\n");
  365. }
  366. /**
  367. * Update Object Meta
  368. * it leverages the feature of copyObject: when the source object is just the target object, the metadata could be updated via copy
  369. *
  370. * @param OssClient $ossClient OssClient instance
  371. * @param string $bucket bucket name
  372. * @return null
  373. */
  374. function modifyMetaForObject($ossClient, $bucket)
  375. {
  376. $fromBucket = $bucket;
  377. $fromObject = "oss-php-sdk-test/upload-test-object-name.txt";
  378. $toBucket = $bucket;
  379. $toObject = $fromObject;
  380. $copyOptions = array(
  381. OssClient::OSS_HEADERS => array(
  382. 'Cache-Control' => 'max-age=60',
  383. 'Content-Disposition' => 'attachment; filename="xxxxxx"',
  384. ),
  385. );
  386. try {
  387. $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);
  388. } catch (OssException $e) {
  389. printf(__FUNCTION__ . ": FAILED\n");
  390. printf($e->getMessage() . "\n");
  391. return;
  392. }
  393. print(__FUNCTION__ . ": OK" . "\n");
  394. }
  395. /**
  396. * Get object meta, that is, getObjectMeta
  397. *
  398. * @param OssClient $ossClient OssClient instance
  399. * @param string $bucket bucket name
  400. * @return null
  401. */
  402. function getObjectMeta($ossClient, $bucket)
  403. {
  404. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  405. try {
  406. $objectMeta = $ossClient->getObjectMeta($bucket, $object);
  407. } catch (OssException $e) {
  408. printf(__FUNCTION__ . ": FAILED\n");
  409. printf($e->getMessage() . "\n");
  410. return;
  411. }
  412. print(__FUNCTION__ . ": OK" . "\n");
  413. if (isset($objectMeta[strtolower('Content-Disposition')]) &&
  414. 'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]
  415. ) {
  416. print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");
  417. } else {
  418. print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");
  419. }
  420. }
  421. /**
  422. * Delete an object
  423. *
  424. * @param OssClient $ossClient OssClient instance
  425. * @param string $bucket bucket name
  426. * @return null
  427. */
  428. function deleteObject($ossClient, $bucket)
  429. {
  430. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  431. try {
  432. $ossClient->deleteObject($bucket, $object);
  433. } catch (OssException $e) {
  434. printf(__FUNCTION__ . ": FAILED\n");
  435. printf($e->getMessage() . "\n");
  436. return;
  437. }
  438. print(__FUNCTION__ . ": OK" . "\n");
  439. }
  440. /**
  441. * Delete multiple objects in batch
  442. *
  443. * @param OssClient $ossClient OssClient instance
  444. * @param string $bucket bucket name
  445. * @return null
  446. */
  447. function deleteObjects($ossClient, $bucket)
  448. {
  449. $objects = array();
  450. $objects[] = "oss-php-sdk-test/upload-test-object-name.txt";
  451. $objects[] = "oss-php-sdk-test/upload-test-object-name.txt.copy";
  452. try {
  453. $ossClient->deleteObjects($bucket, $objects);
  454. } catch (OssException $e) {
  455. printf(__FUNCTION__ . ": FAILED\n");
  456. printf($e->getMessage() . "\n");
  457. return;
  458. }
  459. print(__FUNCTION__ . ": OK" . "\n");
  460. }
  461. /**
  462. * Check whether an object exists
  463. *
  464. * @param OssClient $ossClient OssClient instance
  465. * @param string $bucket bucket name
  466. * @return null
  467. */
  468. function doesObjectExist($ossClient, $bucket)
  469. {
  470. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  471. try {
  472. $exist = $ossClient->doesObjectExist($bucket, $object);
  473. } catch (OssException $e) {
  474. printf(__FUNCTION__ . ": FAILED\n");
  475. printf($e->getMessage() . "\n");
  476. return;
  477. }
  478. print(__FUNCTION__ . ": OK" . "\n");
  479. var_dump($exist);
  480. }