laravel框架使用阿里云的oss
1执行comopser安装扩展包:
composer require johnlui/aliyun-oss
新建配置文件 config/alioss.php,填充下边的 code 。
(可以不加,但在OSS文件里要写死)
env('ALIOSS_KEYID', null), // key 'AccessKeySecret' => env('ALIOSS_KEYSECRET', null), // secret 'BucketName' => env('ALIOSS_BUCKETNAME', null) // bucket];
并且在 .env.example 中加入下边的配置。
(可以不加,但在OSS文件里要写死)
# OSS 配置ALIOSS_KEYID=******ALIOSS_KEYSECRET=******ALIOSS_BUCKETNAME=******
配置好了后,在app目录下建立:App\services\OSS.php ,并添加一下代码
networkType == 'VPC' && !$isInternal) { throw new Exception("VPC 网络下不提供外网上传、下载等功能"); } $this->ossClient = AliyunOSS::boot( $this->city, $this->networkType, $isInternal, $this->AccessKeyId, $this->AccessKeySecret ); } public static function publicUpload($bucketName, $ossKey, $filePath, $options = []) { $oss = new OSS(); #如果alioss.php配置了就不用传$bucketName #$oss->ossClient->setBucket(config('alioss.BucketName')); $oss->ossClient->setBucket($bucketName); return $oss->ossClient->uploadFile($ossKey, $filePath, $options); } public static function privateUpload($bucketName, $ossKey, $filePath, $options = []) { $oss = new OSS(true); $oss->ossClient->setBucket($bucketName); return $oss->ossClient->uploadFile($ossKey, $filePath, $options); } public static function publicUploadContent($bucketName, $ossKey, $content, $options = []) { $oss = new OSS(); $oss->ossClient->setBucket($bucketName); return $oss->ossClient->uploadContent($ossKey, $content, $options); } public static function privateUploadContent($bucketName, $ossKey, $content, $options = []) { $oss = new OSS(true); $oss->ossClient->setBucket($bucketName); return $oss->ossClient->uploadContent($ossKey, $content, $options); } public static function publicDeleteObject($bucketName, $ossKey) { $oss = new OSS(); $oss->ossClient->setBucket($bucketName); return $oss->ossClient->deleteObject($bucketName, $ossKey); } public static function privateDeleteObject($bucketName, $ossKey) { $oss = new OSS(true); $oss->ossClient->setBucket($bucketName); return $oss->ossClient->deleteObject($bucketName, $ossKey); } # 下面不再分公网内网出 API,也不注释了,大家自行体会吧 public function copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey) { $oss = new OSS(); return $oss->ossClient->copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey); } public function moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey) { $oss = new OSS(); return $oss->ossClient->moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey); } // 获取公开文件的 URL public static function getPublicObjectURL($bucketName, $ossKey) { $oss = new OSS(); $oss->ossClient->setBucket($bucketName); return $oss->ossClient->getPublicUrl($ossKey); } // 获取私有文件的URL,并设定过期时间,如 \DateTime('+1 day') public static function getPrivateObjectURLWithExpireTime($bucketName, $ossKey, DateTime $expire_time) { $oss = new OSS(); $oss->ossClient->setBucket($bucketName); return $oss->ossClient->getUrl($ossKey, $expire_time); } public static function createBucket($bucketName) { $oss = new OSS(); return $oss->ossClient->createBucket($bucketName); } public static function getAllObjectKey($bucketName) { $oss = new OSS(); return $oss->ossClient->getAllObjectKey($bucketName); } public static function getObjectMeta($bucketName, $ossKey) { $oss = new OSS(); return $oss->ossClient->getObjectMeta($bucketName, $ossKey); }}
控制器代码可以自己写,给个例子
file('file'); $path = $file->getRealPath();#得到文件的临时地址 $name = $file->getClientOriginalName();#得到文件的名称 #参数1,要上传的Bucket名称,参数2, OSS::publicUpload('privateyaoimage', "$name", $path); $Url=OSS::getPublicObjectURL('privateyaoimage',$name);#返回地址 return ['code' => 0, 'msg' => '上传成功', 'data' => ['class="lazy" data-src' => $Url]]; }}
安装网上的步骤,至此应该就可以了,但我遇到了一个问题,在调用的时候报错
Argument 1 passed to Symfony\\Component\\EventDispatcher\\EventDispatcher::dispatch() must be an object, string given, called in /www/memory/vendor/johnlui/aliyun-oss/class="lazy" data-src/oss/libs/guzzle/common/Guzzle/Common/AbstractHasDispatcher.php on line 40
查找发现是因为aliyun文件中在调用dispatch方法的时候,参数1和参数2位置和框架中dispatch方法是相反的,一共有三个地方调用。分别是
/vendor/johnlui/aliyun-oss/class="lazy" data-src/oss/libs/guzzle/common/Guzzle/Common/AbstractHasDispatcher.php
/vendor/johnlui/aliyun-oss/class="lazy" data-src/oss/libs/guzzle/common/Guzzle/Common/Request.php
/vendor/johnlui/aliyun-oss/class="lazy" data-src/oss/libs/guzzle/http/Guzzle/Http/Curl/CurlMulti.php
本来想着是改laravel框架中的dispatch方法的参数位置,但是改了之后,又报其他错误,应该和框架底层有关,所以我在aliyun文件里的调用dispatch方法的三个位置,把参数都调换了一下。
来源地址:https://blog.csdn.net/qq_41469037/article/details/128386471
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341