技术饭

PHP SDK上传文件到阿里云oss文件存储Bucket

copylian    0 评论    11224 浏览    2020.09.03

对象存储服务(Object Storage Service,OSS)是一种海量、安全、低成本、高可靠的云存储服务,适合存放任意类型的文件。容量和处理能力弹性扩展,多种存储类型供选择,全面优化存储成本。最近业务需求做了PHP SDK上传文件到阿里云oss文件存储Bucket的相关工作。

OSS的PHP SDK文档:https://help.aliyun.com/document_detail/32099.html?spm=a2c4g.11186623.6.1103.108b5a674vBTCh

OssClient初始化:https://help.aliyun.com/document_detail/32100.html?spm=a2c4g.11186623.6.1106.108b5a67FU2PaU

文件追加上传:https://help.aliyun.com/document_detail/88476.html?spm=a2c4g.11186623.6.1124.6aff56d2a9oKoI

1、创建Bucket:在阿里oss控制器创建Bucke,配置好读写权限、跨域权限

1.png

2、如果想自定义域名来访问oss存储上的文件,那么则需要解析cname到oss云存储,如果需要加上cdn则需要吧域名解析cname到cdn域名,然后cdn域名回源oss域名就好了

2.png

3、获取配置的accessKeyId、accessKeySecret等信息,其中如果使用了自定义的域名Endpoint上传,上传速度可能会慢很多

image.png

image.png

4、上传关键代码:重点在try {} catch(){} 部分,一个是实例化 OssClient,一个是批量追加文件上传 appendFile,当需要OssClient的第四个参数配置为true时是自定义域名,这个时候需要将Endpoint改为自定义域名

/**
     * [CreateUploadImage 上传图片]
     */
    public function CreateUploadImage() {
        if($this->request->isPost()){
            //获取参数
            $upload_images = $_FILES['images'];

            //验证上传文件数量
            $upload_images_count = count($upload_images['name']);
            if($upload_images_count <= 0 || $upload_images_count > 15){
                //返回数据
                $rdata['code'] = 0;
                $rdata['msg'] = '上传文件数量上限15个';
                return json($rdata);
            }

            //处理组装数组
            $upload_images_list = [];

            //类型数组
            $img_type = ['image/jpeg', 'image/png', 'image/gif', 'image/jpg'];

            //类型对应拓展名数组
            $ext_type = ['image/jpeg' => 'jpg', 'image/png' => 'png', 'image/gif' => 'gif', 'image/jpg' => 'jpg'];

            //处理数据
            for ($i = 0; $i < $upload_images_count; $i++) {

                //验证如果有文件异常则直接报错
                if($upload_images['error'][$i] != 0){
                    break;
                    //返回数据
                    $rdata['code'] = 0;
                    $rdata['msg'] = '文件:'.$upload_images['name'][$i].'上传错误';
                    return json($rdata);
                }

                //验证拓展名称
                if(!in_array($upload_images['type'][$i], $img_type)){
                    break;
                    //返回数据
                    $rdata['code'] = 0;
                    $rdata['msg'] = '文件:'.$upload_images['name'][$i].'不是图片类型';
                    return json($rdata);
                }

                //名称
                $upload_images_list[$i]['name'] = $upload_images['name'][$i];

                //类型
                $upload_images_list[$i]['type'] = $upload_images['type'][$i];

                //临时名
                $upload_images_list[$i]['tmp_name'] = $upload_images['tmp_name'][$i];

                //错误
                $upload_images_list[$i]['error'] = $upload_images['error'][$i];

                //大小
                $upload_images_list[$i]['size'] = $upload_images['size'][$i];

                //获取拓展名称
                $upload_images_list[$i]['ext_name'] = $ext_type[$upload_images['type'][$i]];
            }

            //验证上传文件数量
            if(empty($upload_images_list)){
                //返回数据
                $rdata['code'] = 0;
                $rdata['msg'] = '上传文件数量上限15个';
                return json($rdata);
            }

            //oss上传配置信息
            $this->accessKeyId = config('img.accessKeyId');
            $this->accessKeySecret = config('img.accessKeySecret');
            $this->endpoint = config('img.endpoint');
            $this->bucket = config('img.bucket');
            $this->domain = config('img.domain');
            $this->dir = 'qingdaoshizhuangzhou2020';

            //加载自动加载文件
            require_once env('extend_path') . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';

            //上传成功文件列表
            $upload_images_success = [];

            try {
                //实力话oss存储类
                //$ossClient = new \OSS\OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint, true);
                $ossClient = new \OSS\OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);

                //循环上传文件
                foreach ($upload_images_list as $key => $value) {
                    //设置文件名称
                    $object = $this->dir ."/". create_order_sn(1, 'qdszg2020') . "." . $value['ext_name'];
                    $position = $ossClient->appendFile($this->bucket, $object, $value['tmp_name'], 0);
                    if($position > 0){
                        $upload_images_success[$key]['name']           = $value['name'];
                        $upload_images_success[$key]['sort']           = $position;
                        $upload_images_success[$key]['thumb']          = $this->domain . $object;
                        $upload_images_success[$key]['photo']          = $this->domain . $object;
                        $upload_images_success[$key]['type']           = 'images';
                        $upload_images_success[$key]['oringinal_type'] = 'images';
                        $upload_images_success[$key]['location']       = 'upload';
                    }
                }

                //返回数据
                $rdata['code'] = 1;
                $rdata['msg'] = '上传图片成功';
                $rdata['data'] = !empty($upload_images_success) ? $upload_images_success : '';
                return json($rdata);

            } catch(\OSS\Core\OssException $e) {
                //返回数据
                $rdata['code'] = 0;
                $rdata['msg'] = $e->getMessage();
                return json($rdata);
            }
        }
    }

5、总结:在过程中可能会遇一些小问题,有时候会出现https不安全,图片不显示等问题,这可能是配置出了错。

只袄早~~~
感谢你的支持,我会继续努力!
扫码打赏,感谢您的支持!

文明上网理性发言!

  • 还没有评论,沙发等你来抢