public function uploadFile(Request $request)
{
$stdData = array();
//接收临时文件路径
$pathName = $request->file('file')->getPathname();
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
$accessKeyId = "";
$accessKeySecret = "";
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "http://oss-cn-shanghai.aliyuncs.com";
// 存储空间名称
$bucket= "";
$fileName = md5(time() . $pathName . rand(1111,999999));
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg
$object = date('Y-m-d',time()) . '/' .$fileName . '.png';
// $content = file_get_contents($pathName);
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
// $ossClient->putObject($bucket, $object, $content);
//文件上传 $pathName临时文件路径
$url = $ossClient->uploadFile($bucket, $object, $pathName)['info']['url'];
//返回前台
$stdData = [
'code'=>200,
'msg'=>'操作成功',
'data'=>['url'=>$url]
];
return ($stdData);
} catch (OssException $e) {
print $e->getMessage();
}
}
|