123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- <?php
- require_once __DIR__ . '/Common.php';
- use OSS\OssClient;
- use OSS\Core\OssException;
- $bucket = Common::getBucketName();
- $ossClient = Common::getOssClient();
- if (is_null($ossClient)) exit(1);
- //*******************************简单使用***************************************************************
- // 简单上传变量的内容到oss文件
- $result = $ossClient->putObject($bucket, "b.file", "hi, oss");
- Common::println("b.file is created");
- Common::println($result['x-oss-request-id']);
- Common::println($result['etag']);
- Common::println($result['content-md5']);
- Common::println($result['body']);
- // 上传本地文件
- $result = $ossClient->uploadFile($bucket, "c.file", __FILE__);
- Common::println("c.file is created");
- Common::println("b.file is created");
- Common::println($result['x-oss-request-id']);
- Common::println($result['etag']);
- Common::println($result['content-md5']);
- Common::println($result['body']);
- // 下载object到本地变量
- $content = $ossClient->getObject($bucket, "b.file");
- Common::println("b.file is fetched, the content is: " . $content);
- // 给object添加symlink
- $content = $ossClient->putSymlink($bucket, "test-symlink", "b.file");
- Common::println("test-symlink is created");
- Common::println($result['x-oss-request-id']);
- Common::println($result['etag']);
- // 获取symlink
- $content = $ossClient->getSymlink($bucket, "test-symlink");
- Common::println("test-symlink refer to : " . $content[OssClient::OSS_SYMLINK_TARGET]);
- // 下载object到本地文件
- $options = array(
- OssClient::OSS_FILE_DOWNLOAD => "./c.file.localcopy",
- );
- $ossClient->getObject($bucket, "c.file", $options);
- Common::println("b.file is fetched to the local file: c.file.localcopy");
- Common::println("b.file is created");
- // 拷贝object
- $result = $ossClient->copyObject($bucket, "c.file", $bucket, "c.file.copy");
- Common::println("lastModifiedTime: " . $result[0]);
- Common::println("ETag: " . $result[1]);
- // 判断object是否存在
- $doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy");
- Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no"));
- // 删除object
- $result = $ossClient->deleteObject($bucket, "c.file.copy");
- Common::println("c.file.copy is deleted");
- Common::println("b.file is created");
- Common::println($result['x-oss-request-id']);
- // 判断object是否存在
- $doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy");
- Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no"));
- // 批量删除object
- $result = $ossClient->deleteObjects($bucket, array("b.file", "c.file"));
- foreach($result as $object)
- Common::println($object);
- sleep(2);
- unlink("c.file.localcopy");
- //******************************* 完整用法参考下面函数 ****************************************************
- listObjects($ossClient, $bucket);
- listAllObjects($ossClient, $bucket);
- createObjectDir($ossClient, $bucket);
- putObject($ossClient, $bucket);
- uploadFile($ossClient, $bucket);
- getObject($ossClient, $bucket);
- getObjectToLocalFile($ossClient, $bucket);
- copyObject($ossClient, $bucket);
- modifyMetaForObject($ossClient, $bucket);
- getObjectMeta($ossClient, $bucket);
- deleteObject($ossClient, $bucket);
- deleteObjects($ossClient, $bucket);
- doesObjectExist($ossClient, $bucket);
- getSymlink($ossClient, $bucket);
- putSymlink($ossClient, $bucket);
- /**
- * 创建虚拟目录
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function createObjectDir($ossClient, $bucket)
- {
- try {
- $ossClient->createObjectDir($bucket, "dir");
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- }
- /**
- * 把本地变量的内容到文件
- *
- * 简单上传,上传指定变量的内存值作为object的内容
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function putObject($ossClient, $bucket)
- {
- $object = "oss-php-sdk-test/upload-test-object-name.txt";
- $content = file_get_contents(__FILE__);
- $options = array();
- try {
- $ossClient->putObject($bucket, $object, $content, $options);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- }
- /**
- * 上传指定的本地文件内容
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function uploadFile($ossClient, $bucket)
- {
- $object = "oss-php-sdk-test/upload-test-object-name.txt";
- $filePath = __FILE__;
- $options = array();
- try {
- $ossClient->uploadFile($bucket, $object, $filePath, $options);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- }
- /**
- * 列出Bucket内所有目录和文件, 注意如果符合条件的文件数目超过设置的max-keys, 用户需要使用返回的nextMarker作为入参,通过
- * 循环调用ListObjects得到所有的文件,具体操作见下面的 listAllObjects 示例
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function listObjects($ossClient, $bucket)
- {
- $prefix = 'oss-php-sdk-test/';
- $delimiter = '/';
- $nextMarker = '';
- $maxkeys = 1000;
- $options = array(
- 'delimiter' => $delimiter,
- 'prefix' => $prefix,
- 'max-keys' => $maxkeys,
- 'marker' => $nextMarker,
- );
- try {
- $listObjectInfo = $ossClient->listObjects($bucket, $options);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- $objectList = $listObjectInfo->getObjectList(); // 文件列表
- $prefixList = $listObjectInfo->getPrefixList(); // 目录列表
- if (!empty($objectList)) {
- print("objectList:\n");
- foreach ($objectList as $objectInfo) {
- print($objectInfo->getKey() . "\n");
- }
- }
- if (!empty($prefixList)) {
- print("prefixList: \n");
- foreach ($prefixList as $prefixInfo) {
- print($prefixInfo->getPrefix() . "\n");
- }
- }
- }
- /**
- * 列出Bucket内所有目录和文件, 根据返回的nextMarker循环得到所有Objects
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function listAllObjects($ossClient, $bucket)
- {
- //构造dir下的文件和虚拟目录
- for ($i = 0; $i < 100; $i += 1) {
- $ossClient->putObject($bucket, "dir/obj" . strval($i), "hi");
- $ossClient->createObjectDir($bucket, "dir/obj" . strval($i));
- }
- $prefix = 'dir/';
- $delimiter = '/';
- $nextMarker = '';
- $maxkeys = 30;
- while (true) {
- $options = array(
- 'delimiter' => $delimiter,
- 'prefix' => $prefix,
- 'max-keys' => $maxkeys,
- 'marker' => $nextMarker,
- );
- var_dump($options);
- try {
- $listObjectInfo = $ossClient->listObjects($bucket, $options);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- // 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表
- $nextMarker = $listObjectInfo->getNextMarker();
- $listObject = $listObjectInfo->getObjectList();
- $listPrefix = $listObjectInfo->getPrefixList();
- var_dump(count($listObject));
- var_dump(count($listPrefix));
- if ($nextMarker === '') {
- break;
- }
- }
- }
- /**
- * 获取object的内容
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function getObject($ossClient, $bucket)
- {
- $object = "oss-php-sdk-test/upload-test-object-name.txt";
- $options = array();
- try {
- $content = $ossClient->getObject($bucket, $object, $options);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- if (file_get_contents(__FILE__) === $content) {
- print(__FUNCTION__ . ": FileContent checked OK" . "\n");
- } else {
- print(__FUNCTION__ . ": FileContent checked FAILED" . "\n");
- }
- }
- /**
- * put symlink
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function putSymlink($ossClient, $bucket)
- {
- $symlink = "test-samples-symlink";
- $object = "test-samples-object";
- try {
- $ossClient->putObject($bucket, $object, 'test-content');
- $ossClient->putSymlink($bucket, $symlink, $object);
- $content = $ossClient->getObject($bucket, $symlink);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- if ($content == 'test-content') {
- print(__FUNCTION__ . ": putSymlink checked OK" . "\n");
- } else {
- print(__FUNCTION__ . ": putSymlink checked FAILED" . "\n");
- }
- }
- /**
- * 获取symlink
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function getSymlink($ossClient, $bucket)
- {
- $symlink = "test-samples-symlink";
- $object = "test-samples-object";
- try {
- $ossClient->putObject($bucket, $object, 'test-content');
- $ossClient->putSymlink($bucket, $symlink, $object);
- $content = $ossClient->getSymlink($bucket, $symlink);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- if ($content[OssClient::OSS_SYMLINK_TARGET]) {
- print(__FUNCTION__ . ": getSymlink checked OK" . "\n");
- } else {
- print(__FUNCTION__ . ": getSymlink checked FAILED" . "\n");
- }
- }
- /**
- * get_object_to_local_file
- *
- * 获取object
- * 将object下载到指定的文件
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function getObjectToLocalFile($ossClient, $bucket)
- {
- $object = "oss-php-sdk-test/upload-test-object-name.txt";
- $localfile = "upload-test-object-name.txt";
- $options = array(
- OssClient::OSS_FILE_DOWNLOAD => $localfile,
- );
- try {
- $ossClient->getObject($bucket, $object, $options);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK, please check localfile: 'upload-test-object-name.txt'" . "\n");
- if (file_get_contents($localfile) === file_get_contents(__FILE__)) {
- print(__FUNCTION__ . ": FileContent checked OK" . "\n");
- } else {
- print(__FUNCTION__ . ": FileContent checked FAILED" . "\n");
- }
- if (file_exists($localfile)) {
- unlink($localfile);
- }
- }
- /**
- * 拷贝object
- * 当目的object和源object完全相同时,表示修改object的meta信息
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function copyObject($ossClient, $bucket)
- {
- $fromBucket = $bucket;
- $fromObject = "oss-php-sdk-test/upload-test-object-name.txt";
- $toBucket = $bucket;
- $toObject = $fromObject . '.copy';
- $options = array();
- try {
- $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $options);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- }
- /**
- * 修改Object Meta
- * 利用copyObject接口的特性:当目的object和源object完全相同时,表示修改object的meta信息
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function modifyMetaForObject($ossClient, $bucket)
- {
- $fromBucket = $bucket;
- $fromObject = "oss-php-sdk-test/upload-test-object-name.txt";
- $toBucket = $bucket;
- $toObject = $fromObject;
- $copyOptions = array(
- OssClient::OSS_HEADERS => array(
- 'Cache-Control' => 'max-age=60',
- 'Content-Disposition' => 'attachment; filename="xxxxxx"',
- ),
- );
- try {
- $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- }
- /**
- * 获取object meta, 也就是getObjectMeta接口
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function getObjectMeta($ossClient, $bucket)
- {
- $object = "oss-php-sdk-test/upload-test-object-name.txt";
- try {
- $objectMeta = $ossClient->getObjectMeta($bucket, $object);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- if (isset($objectMeta[strtolower('Content-Disposition')]) &&
- 'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]
- ) {
- print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");
- } else {
- print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");
- }
- }
- /**
- * 删除object
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function deleteObject($ossClient, $bucket)
- {
- $object = "oss-php-sdk-test/upload-test-object-name.txt";
- try {
- $ossClient->deleteObject($bucket, $object);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- }
- /**
- * 批量删除object
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function deleteObjects($ossClient, $bucket)
- {
- $objects = array();
- $objects[] = "oss-php-sdk-test/upload-test-object-name.txt";
- $objects[] = "oss-php-sdk-test/upload-test-object-name.txt.copy";
- try {
- $ossClient->deleteObjects($bucket, $objects);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- }
- /**
- * 判断object是否存在
- *
- * @param OssClient $ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- function doesObjectExist($ossClient, $bucket)
- {
- $object = "oss-php-sdk-test/upload-test-object-name.txt";
- try {
- $exist = $ossClient->doesObjectExist($bucket, $object);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- var_dump($exist);
- }
|