阿里云OSS对象存储的实现

使用了OSS,就可以实现数据和业务逻辑分离了,近期我用了一下阿里云的对象存储,感觉效果还不错,所以把常用的功能封装成了接口,方便调用,下面是代码,和大家分享。

getOssSdk();
        try{
            $result=$this->success(
                $ossClient->createBucket($bucketName)
            );
        }catch(OssException $e){
            $result=$this->error(
                $e->getMessage()
            );
        }
        return $result;
    }

    /**
     * @param $bucketName(存储空间名称)
     * @return array
     * @throws Exception
     * 删除存储空间
     */
    public function deleteBucketByName($bucketName){
        $ossClient=$this->getOssSdk();
        try{
            $result=$this->success(
                $ossClient->deleteBucket($bucketName)
            );
        }catch(OssException $e){
            $result=$this->error(
                $e->getMessage()
            );
        }
        return $result;
    }

    /**
     * @param $dirName(目录名)
     * @param $bucketName(存储空间名)
     * @return array
     * @throws Exception
     * 创建目录
     */
    public function createDirByName($dirName,$bucketName){
        $ossClient=$this->getOssSdk();
        try{
            $result=$this->success(
                $ossClient->createObjectDir($bucketName,$dirName)
            );
        }catch(OssException $e){
            $result=$this->error(
                $e->getMessage()
            );
        }
        return $result;
    }
    /**
     * @param $dirName(目录名)
     * @param $bucketName(存储空间名)
     * @return array
     * @throws Exception
     * 删除目录
     */
    public function deleteDirByName($dirName,$bucketName){
        $ossClient=$this->getOssSdk();
        try{
            $result=$this->success(
                $ossClient->deleteObject($bucketName,$dirName)
            );
        }catch(OssException $e){
            $result=$this->error(
                $e->getMessage()
            );
        }
        return $result;
    }

    /**
     * @param $fileName(文件名)
     * @param $filePath(文件真实路径)
     * @param $bucketName(存储空间名)
     * @return array
     * @throws Exception
     * 文件上传
     */
    public function uploadFileByFileName($fileName,$filePath,$bucketName){
        $ossClient=$this->getOssSdk();
        try{
            $result=$this->success(
                $ossClient->uploadFile($bucketName,$fileName,$filePath)
            );
        }catch(OssException $e){
            $result=$this->error(
                $e->getMessage()
            );
        }
        return $result;
    }

    /**
     * @param $fileName(文件名)
     * @param $bucketName(存储空间名)
     * @param $type(输出类型,文件:file,二进制流:stream)
     * @return array
     * @throws Exception
     * 文件下载
     */
    public function downloadFileByFileName($fileName,$bucketName,$type){
        $ossClient=$this->getOssSdk();
        if($type=="file"){
            header("Content-Type: application/octet-stream");
            header("Accept-Ranges: bytes");
            header("Content-Disposition: attachment; filename=".$fileName);
        }
        try{
            die($ossClient->getObject($bucketName,$fileName));
        }catch(OssException $e){
            $result=$this->error(
                $e->getMessage()
            );
        }
        return $result;
    }

    /**
     * @param $bucketName(存储空间名)
     * @return array
     * @throws Exception
     * 枚举文件列表
     */
    public function getFileListByBucketname($bucketName){
        $ossClient=$this->getOssSdk();
        $fileList=array();//准备文件列表
        try{
            $listObjectInfo=$ossClient->listObjects($bucketName);
            $objectList = $listObjectInfo->getObjectList();
            foreach($objectList as $value){
                $fileList[]=[
                    "fileName"=>$value->getKey(),
                    "size"=>$value->getSize(),
                    "lastModified"=>$value->getLastModified(),
                    "eTag"=>$value->getETag(),
                    "type"=>$value->getType(),
                    "storageClass"=>$value->getStorageClass()
                ];
            }
            $result=$this->success(
                $fileList
            );
        }catch(OssException $e){
            $result=$this->error(
                $e->getMessage()
            );
        }
        return $result;
    }
    /**
     * @param $dirName(文件名,多文件请传入数组)
     * @param $bucketName(存储空间名)
     * @return array
     * @throws Exception
     * 删除文件(支持删除多个)
     */
    public function deleteFileByName($fileName,$bucketName){
        if(!is_array($fileName)){
            $fileName=[$fileName];//兼容多文件上传
        }
        $ossClient=$this->getOssSdk();
        try{
            $result=$this->success(
                $ossClient->deleteObjects($bucketName,$fileName)
            );
        }catch(OssException $e){
            $result=$this->error(
                $e->getMessage()
            );
        }
        return $result;
    }

    /**
     * @param $fileName(文件名)
     * @param $bucketName(存储空间名)
     * @return array
     * @throws Exception
     * 文件是否存在
     */
    public function fileExist($fileName,$bucketName){
        $ossClient=$this->getOssSdk();
        try{
            $result=$this->success(
                $ossClient->doesObjectExist($bucketName,$fileName)
            );
        }catch(OssException $e){
            $result=$this->error(
                $e->getMessage()
            );
        }
        return $result;
    }

    /**
     * @param $fromBucketName(原存储空间名)
     * @param $fromFileName(原文件名)
     * @param $toBucketName(新存储空间名)
     * @param $toFileName(新文件名)
     * @return array
     * @throws Exception
     * 复制文件
     */
    public function copyFile($fromBucketName,$fromFileName,$toBucketName,$toFileName){
        $ossClient=$this->getOssSdk();
        try{
            $result=$this->success(
                $ossClient->copyObject($fromBucketName,$fromFileName,$toBucketName,$toFileName)
            );
        }catch(OssException $e){
            $result=$this->error(
                $e->getMessage()
            );
        }
        return $result;
    }

    /**
     * @param $fileName(文件名)
     * @param $bucketName(存储空间名)
     * @return array
     * @throws Exception
     * 取得文件属性
     */
    public function getFileMeta($fileName,$bucketName){
        $ossClient=$this->getOssSdk();
        try{
            $result=$this->success(
                $ossClient->getObjectMeta($bucketName,$fileName)
            );
        }catch(OssException $e){
            $result=$this->error(
                $e->getMessage()
            );
        }
        return $result;
    }
    /////////////////////////////////////////////////
    //内部方法
    /**
     * 取得SDK句柄
     */
    private function getOssSdk(){
        require_once __DIR__."/autoload.php";
        try {
            $ossClient = new OssClient($this->accessId, $this->accessKey, $this->endpoint);
        } catch (OssException $e) {
            throw new Exception($e->getMessage());
        }
        return $ossClient;
    }

    /**
     * @param $data
     * @return array
     * 执行成功
     */
    private function success($data){
        return [
            "status"=>"success",
            "data"=>$data
        ];
    }

    /**
     * @param $data
     * @return array
     * 执行失败
     */
    private function error($data){
        return [
            "status"=>"error",
            "data"=>$data
        ];
    }
}


你可能感兴趣的:(php开发)