123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- <?php
- namespace app\common\service\Oss;
- use Exception;
- use OSS\Core\OssException;
- use OSS\OssClient;
- use RuntimeException;
- class Object extends Common {
- /**
- * 创建虚拟目录
- *
- * @param $dir 虚拟目录名
- * @return Boolean
- */
- public function createObjectDir($dir) {
- try {
- $this->_ossClient->createObjectDir($this->getBucketName(), $dir);
- } catch (OssException $e) {
- throw_exception($e->getMessage());
- return false;
- }
- return true;
- }
- /**
- * 把本地变量的内容到文件
- * 简单上传,上传指定变量的内存值作为object的内容
- * @param string $fileName 存储的文件名:photo1.png | 201602/photo1.png
- * @param string $content 文件流
- * @return Boolean
- */
- public function putObject(string $fileName, string &$content) {
- try {
- $this->_ossClient->putObject($this->getBucketName(), $fileName, $content, []);
- } catch (Exception $e) {
- throw new RuntimeException($e);
- }
- return true;
- }
- /**
- * 上传指定的本地文件内容
- *
- * @param string $fileName 存储的文件名:photo1.png | 201602/photo1.png
- * @param string $filePath 本地文件路径
- * @return Boolean
- */
- public function uploadFile($fileName, $filePath) {
- $options = array();
- try {
- $this->_ossClient->uploadFile($this->getBucketName(), $fileName, $filePath, $options);
- } catch (OssException $e) {
- throw_exception($e->getMessage());
- return false;
- }
- return true;
- }
- /**
- * @param $object 文件相对地址
- * @param string $acl 读写权限,可选值 ['default', 'private', 'public-read', 'public-read-write']
- * @return bool
- * @throws \think\Exception
- */
- public function putObjectAcl( $object, $acl = 'private') {
- $options = array();
- try {
- $this->_ossClient->putObjectAcl($this->getBucketName(), $object,$acl);
- } catch (OssException $e) {
- throw_exception($e->getMessage());
- return false;
- }
- return true;
- }
- /**
- * 列出Bucket内所有目录和文件, 注意如果符合条件的文件数目超过设置的max-keys, 用户需要使用返回的nextMarker作为入参,通过
- * 循环调用ListObjects得到所有的文件,具体操作见下面的 listAllObjects 示例
- *
- * @param OssClient $this->_ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- public function listObjects($prefix="" ,$maxkeys=1000) {
- $delimiter = '/';
- $nextMarker = '';
- $options = array(
- 'delimiter' => $delimiter,
- 'prefix' => $prefix,
- 'max-keys' => $maxkeys,
- 'marker' => $nextMarker,
- );
- try {
- $listObjectInfo = $this->_ossClient->listObjects($this->getBucketName(), $options);
- } catch (OssException $e) {
- throw_exception($e->getMessage());
- return false;
- }
- $data = [];
- $objectList = $listObjectInfo->getObjectList(); // 文件列表
- $prefixList = $listObjectInfo->getPrefixList(); // 目录列表
- if (!empty($objectList)) {
- foreach ($objectList as $objectInfo) {
- print($objectInfo->getKey() . "\n");
- $data['file'][] = $objectInfo->getKey();
- }
- }
- if (!empty($prefixList)) {
- foreach ($prefixList as $prefixInfo) {
- print($prefixInfo->getPrefix() . "\n");
- $data['dir'][] = $objectInfo->getPrefix();
- }
- }
- return $data;
- }
- /**
- * 列出Bucket内所有目录和文件, 根据返回的nextMarker循环得到所有Objects
- *
- * @param OssClient $this->_ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- public function listAllObjects($prefix = "") {
- $delimiter = '/';
- $nextMarker = '';
- $maxkeys = 30;
- while (true) {
- $options = array(
- 'delimiter' => $delimiter,
- 'prefix' => $prefix,
- 'max-keys' => $maxkeys,
- 'marker' => $nextMarker,
- );
- var_dump($options);
- try {
- $listObjectInfo = $this->_ossClient->listObjects($this->getBucketName(), $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 string $objectPath 对象路径
- * @return string
- */
- public function getObject(string $objectPath) {
- try {
- $content = $this->_ossClient->getObject($this->getBucketName(), $objectPath, []);
- } catch (Exception $e) {
- throw new RuntimeException($e);
- }
- return $content;
- }
- /**
- * get_object_to_local_file
- *
- * 获取object
- * 将object下载到指定的文件
- *
- * @param OssClient $this->_ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- public function getObjectToLocalFile()
- {
- $object = "oss-php-sdk-test/upload-test-object-name.txt";
- $localfile = "upload-test-object-name.txt";
- $options = array(
- OssClient::OSS_FILE_DOWNLOAD => $localfile,
- );
- try {
- $this->_ossClient->getObject($this->getBucketName(), $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 $this->_ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- public function copyObject()
- {
- $fromBucket = $this->getBucketName();
- $fromObject = "oss-php-sdk-test/upload-test-object-name.txt";
- $toBucket = $bucket;
- $toObject = $fromObject . '.copy';
- $options = array();
- try {
- $this->_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 $this->_ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- public function modifyMetaForObject()
- {
- $fromBucket = $this->getBucketName();
- $fromObject = "oss-php-sdk-test/upload-test-object-name.txt";
- $toBucket = $this->getBucketName();
- $toObject = $fromObject;
- $copyOptions = array(
- OssClient::OSS_HEADERS => array(
- 'Cache-Control' => 'max-age=60',
- 'Content-Disposition' => 'attachment; filename="xxxxxx"',
- ),
- );
- try {
- $this->_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 $this->_ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- public function getObjectMeta()
- {
- $object = "oss-php-sdk-test/upload-test-object-name.txt";
- try {
- $objectMeta = $this->_ossClient->getObjectMeta($this->getBucketName(), $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 $object
- * @return bool
- */
- public function deleteObject($object) {
- try {
- $this->_ossClient->deleteObject($this->getBucketName(), $object);
- } catch (Exception $e) {
- throw new RuntimeException($e);
- }
- return true;
- }
- /**
- * 批量删除object
- *
- * @param OssClient $this->_ossClient OssClient实例
- * @param string $bucket 存储空间名称
- * @return null
- */
- public function deleteObjects()
- {
- $objects = array();
- $objects[] = "oss-php-sdk-test/upload-test-object-name.txt";
- $objects[] = "oss-php-sdk-test/upload-test-object-name.txt.copy";
- try {
- $this->_ossClient->deleteObjects($this->getBucketName(), $objects);
- } catch (OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- }
- /**
- * 判断object是否存在
- * @param string $object 存储空间名称
- * @return bool
- */
- public function doesObjectExist(string $object) {
- try {
- $exist = $this->_ossClient->doesObjectExist($this->getBucketName(), $object);
- } catch (Exception $e) {
- throw new RuntimeException($e);
- }
- return $exist;
- }
- }
|