腾讯cos安装,上传图片实际使用

1.安装腾讯cos依赖包
composer require freyo/flysystem-qcloud-cos-v5
2..env文件中增加配置
#腾讯cos
COS_APPID=你自己的appid
COS_SECRETID=你自己的配置
COS_SECRETKEY=你自己的key
3.新建控制器cosController.php
<?php

namespace App\Http\Controllers\Common;
use Qcloud\Cos\Api;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\Pictures;
/**
 * 腾讯cos
 */
class CosController extends Controller
{

    /**
     * cos上传方法
     * @param  [type] $fileName [description]
     * @param  [type] $realPath [description]
     * @return [type]           [description]
     */
    public function uploadImg($fileName, $realPath)
    {   
        $cosClient = new \Qcloud\Cos\Client([
            // 区域,这里的区域要写对,不然也会有错误
            'region' => "ap-chongqing",
            'schema' => 'http',
            'credentials' => [
                'appId' => env('COS_APPID'),
                'secretId'    => env('COS_SECRETID'),
                'secretKey' => env('COS_SECRETKEY')
            ]
        ]);

        try {

            $result = $cosClient->putObject([
                // 存储桶名字,注意没有-appid,腾讯文档是错的
                'Bucket'    => "ceshi", // 存储桶-appid
                'Key'         => $fileName, // 键名==文件名
                'Body'         => fopen( $realPath , 'rb' ), // 图片
                'ServerSideEncryption' => 'AES256' // 加密
            ]);

            return $result['Location'];

            // 公有私密访问需要获取访问地址
            // $bucket = "gzc"; //存储桶,格式:BucketName-APPID
            // $key = $fileName; //对象在存储桶中的位置,即称对象键
            // $signedUrl = $cosClient->getObjectUrl($bucket, $key, '+10 minutes');
            // 上传成功
            // return $signedUrl;
        } catch (\Exception $e) {
            Log::error('cos exception:'.$e->getMessage());
            return false;
        }
    }

    /**
     * 上传图片
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function uploadImage( Request $request ) {

        try {
            if( $request->hasFile('img') ){

                $file = $request->file('img'); // 接受图片
                if( $file->isValid() ){
                    // 上传成功
                    $ext = $file->getClientOriginalExtension(); // 扩展名
                    $allow = ['jpg', 'gif', 'png', 'jpeg']; // 支持的类型
                    if( !in_array($ext, $allow) ){
                        return $this->fail( 30013 );
                    }
                    // 判断大小
                    $size = $file->getSize();
                    $size = $size/1024/1024;
                    if($size > 3){
                        return $this->fail( 30007 );
                    }
                    // 进行上传
                    $realPath = $file->getRealPath(); // 文件的绝对路径
                    $filename = date('Ymd').'/'.date('Ymdhis').'-'.mt_rand(100,999).'.'.$ext; // 定义文件名

                    // 腾讯 cos 存储
                    $url = $this->uploadImg($filename, $realPath);
                    if( $url == false ){
                        // 上传失败
                        return $this->fail( 30007 );
                    }else{

                        // 存储图片,注意我这里是存到了数据库,你可以自由选择存储方式
                        $pictures = new Pictures; 
                        $picturesId = $pictures->store( $filename , $url );

                        $data = [
                            'key' => $filename,
                            'url' => $url,
                            'id' => $picturesId
                        ];

                        return $this->success( $data );
                    }

                }else{
                    return $this->fail( 30007 );
                }
            }
        } catch (Exception $e) {
            echo $e->getMessage(); 

        }
    }
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
马江川@13753441707
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 3

不错就是代码有点烂

3年前 评论
mjc123456 (楼主) 3年前
小李世界 3年前
mjc123456 (楼主) 3年前

这不是官方文档?

3年前 评论
mjc123456 (楼主) 3年前
佛布朗斯基 (作者) 3年前
mjc123456 (楼主) 3年前
佛布朗斯基 (作者) 3年前
mjc123456 (楼主) 3年前

既然都用了flysystem的适配包了,框架也用的laravel,那为什么不试试直接用laravel提供的文件存储模块做上传呢,只要简单配置一下就可以了。

3年前 评论
mjc123456 (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!