详情参见阿里云手册 首先需要安装SDK(可以composer安装)
composer require aliyuncs/oss-sdk-php
然后引入
use OSS\Core\OssException;
use OSS\OssClient;
代码
public function uploadFile(Request $request)
{
$file = $request->file('file')->store('avatars');
$KeyId = "AccessKeyId";
$KeySecret = "AccessKeySecret";
$EndPoint = "";
$Bucket = "";
try {
$ossClient = new OssClient($KeyId, $KeySecret, $EndPoint);
$result = $ossClient->uploadFile($Bucket, '0310.jpg', $file);
$url = $result['info']['url'];
return $url;
} catch (OssException $e) {
return $e->getMessage();
}
}
注意:这里是先上传到本地再传到OSS上的,不然OSS上传不了,但不建议这样,建议先上传到电脑中后再上传到OSS,这样等项目上线后不会给服务器带来消耗。 (做的笔记,自己看的)
|