Laravel5.6如何上传图片值阿里云OSS中 - Go语言中文社区

Laravel5.6如何上传图片值阿里云OSS中


第一步:我们得下载阿里云存储的laravel插件  “jacobcyl/ali-oss-storage”,这里我们用composer安装其插件,如果不知道怎么安装composer的,请自行百度。安装方式有两种:

  1. 在composer.json文件的require项中添加如下代码
    "jacobcyl/ali-oss-storage": "^2.1"

    并执行命令 composer update

  2. 在命令行执行如下命令

    "composer require jacobcyl/ali-oss-storage:^2.1"

    安装完成后就可以在vendor目录中看到如下文件,至此,插件安装就完成了。

     

第二步:需要在config/app.php的providers下添加

//aliyunOSS
 JacobcylAliOSSAliOssServiceProvider::class,

如果有需要,可以在config/app.php的aliases下添加,这一项不是必须 的。

 'AliYunOss' => JacobcylAliOSSAliOssServiceProvider::class,

第三步:在config/filesystems.php文件中添加如下代码

  'disks'=>[
        ...
        'oss' => [
                'driver'        => 'oss',
                'access_id'     => '<Your Aliyun OSS AccessKeyId>',
                'access_key'    => '<Your Aliyun OSS AccessKeySecret>',
                'bucket'        => '<OSS bucket name>',
                'endpoint'      => '<the endpoint of OSS, E.g: oss-cn-hangzhou.aliyuncs.com | custom domain, E.g:img.abc.com>', // OSS 外网节点或自定义外部域名
                //'endpoint_internal' => '<internal endpoint [OSS内网节点] 如:oss-cn-shenzhen-internal.aliyuncs.com>', // v2.0.4 新增配置属性,如果为空,则默认使用 endpoint 配置(由于内网上传有点小问题未解决,请大家暂时不要使用内网节点上传,正在与阿里技术沟通中)
                'cdnDomain'     => '<CDN domain, cdn域名>', // 如果isCName为true, getUrl会判断cdnDomain是否设定来决定返回的url,如果cdnDomain未设置,则使用endpoint来生成url,否则使用cdn
                'ssl'           => <true|false> // true to use 'https://' and false to use 'http://'. default is false,
                'isCName'       => <true|false> // 是否使用自定义域名,true: 则Storage.url()会使用自定义的cdn或域名生成文件url, false: 则使用外部节点生成url
                'debug'         => <true|false>
                 'ad_upload_dir' => env("AD_UPLOAD_DIR","upload"),  //上传地址
                  ...其他地址自行添加
        ],
       
    ]

在app/filesystems.php把  'default' => env('FILESYSTEM_DRIVER', 'local'),修改为

'default' => env('FILESYSTEM_DRIVER', 'oss'),  //可修改也可不改

附:官网地址https://packagist.org/packages/jacobcyl/ali-oss-storage#2.1.0

第四步:上传,这里我用的上传插件是百度上传插件,这里不过多介绍,将在下篇做详细介绍。

路由如下:

Route::post("xxx/upload/{file}","文件夹xxxController@upload")

在业务需要的控制器中添加如下代码

 public function upload($file){
        $fileObj = $this -> request->$file;
        $remoteDir = config("filesystems.disks.oss.ad_upload_dir");
        $res = $this->uploadService->doUpload($fileObj,$remoteDir);
        return $res ;

    }

在Models层中新建Service层(根据自己项目来建),并在其新建UploadService.php文件

<?php


namespace AppModelsService;


use IlluminateSupportFacadesStorage;

class UploadService
{
    //上传文件
    public function doUpload($fileObj,$remoteDir){
        $path = $fileObj->store($remoteDir,'oss');
        $imgUrl = "http://".config("filesystems.disks.oss.bucket").".".config("filesystems.disks.oss.endpoint").$path;
//        $imgUrl = config("filesystems.disks.oss.cdnDomain").$path;
//        $imgUrl = Storage::url($path);
//        dump($imgUrl);die;
        if ( $path ){
            return ["status" => "SUCCESS","fileUrl" => $imgUrl];
        }
        return ["status" => "ERROR","fileUrl" => ""];
    }
    //删除文件
    public function deleteImg($imgUrl){
        $path = str_replace("http://","","$imgUrl");
        $pos = strpos($path,"/");
        $imgUrl = substr($path,$pos+1);
        $isExist = Storage::exists($imgUrl);
        if (!$isExist){
            return true;
        }
        $res = Storage::delete($imgUrl);
        return $res ;
        if(!$res){
            return false;
        }
        return true;

    }

}

到这里,laravel5.6的阿里云上传已完成,如有什么问题,请留言指出,我会一一作答!

新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
https://cloud.tencent.com/redirect.php?redirect=1040&cps_key=8ee0f9c89dfe0958071ea9b77e110670&from=console

淘宝优惠券: http://tq.xinrtd.com
京东优惠券: http://jp.xinrtd.com

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_35770969/article/details/86539794
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-08 16:06:14
  • 阅读 ( 1398 )
  • 分类:职场

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢