如何引入 obs 包,请看这里obs/esdk-obs-php
/**
* obs 二进制流上传
*
* @return \Illuminate\Http\JsonResponse
*/
public function uploadStream(Request $request)
{
// 文件名
$params['file_name'] = $request->header('filename') ? $request->header('filename') : md5 ( mt_rand () . uniqid () ) . '.dat';
// 流信息
$params['stream_info'] = file_get_contents('php://input', 'r');
//obs 配置信息['accessKeyId'=>'','accessKeySecret'=>'','endpoint'=>'','bucket'=>'','host'=>'']
$obsInfo = Common::getObsInfo();
// obs 保存路径
$params['file_path'] = 'client_info' .DIRECTORY_SEPARATOR. date('Ymd') .DIRECTORY_SEPARATOR. $params['file_name'];
$response = Common::fileUploadStream($params, $obsInfo);
}
/**
* 二进制流文件上传
* @param $file_info 上传文件信息 file_path:文件上传路径,stream_info:流信息
* @param $obsConfig obs 相关配置信息
*/
static public function fileUploadStream($file_info, $obsConfig)
{
//别忘了use Obs\ObsClient;use obs\ObsException;
$client = ObsClient::factory([
'key' => $obsConfig['accessKeyId'],
'secret' => $obsConfig['accessKeySecret'],
'endpoint' => $obsConfig['endpoint'],
'socket_timeout' => 300,
'connect_timeout' => 60
]);
try {
$client->putObject([
'Bucket' => $obsConfig['bucket'],
'Key' => $file_info['file_path'],
'Body' => $file_info['stream_info']
]);
} catch(ObsException $e) {
return false;
}
// 返回 obs 上传地址
return ['file_url'=>$obsConfig['host'] . DIRECTORY_SEPARATOR . $file_info['file_path']];
}
如有侵权请邮件通知