面对视频直播源代码数据日益需求量增加的时代,本地数据存储在便利性和数据访问实时性上已经面对着巨大的考验,因此,各大互联网运营商纷纷推出了云存储,方便了数据管理和迁移,可以实现数据共享等优点。下面我们就来解析下阿里云存储PHP的实现过程。
官方参考文档:
https:// help.aliyun. com/document_detail/52834.html?spm=5176.7933691.1309860.2.979f2a660hPSzz
我们要实现的是PHP的直播源代码文件上传,因此,我们需要参考的是文档中关于文件上传的部分。
首先需要下载sdk文件,放置在合适的路径下。
其次,在直播源代码块中,我们需要将sdk文件引入项目中,参考代码如下:
require_once __DIR__ . '/../autoload.php';
注意路径要根据自己实际需求进行变更
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录
https:// ram.console.aliyun. com 创建RAM账号。
$accessKeyId = "";
$accessKeySecret = "";// Endpoint
以杭州为例,其它Region请按实际情况填写。
$endpoint = "http:// oss-cn-hangzhou.aliyuncs. com";// 存储空间名称
$bucket= "";// 文件名称
$object = "";// 文件名,如myfile.txt
$filePath = ""; //文件本地存放路径
try{
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$ossClient->uploadFile($bucket, $object, $filePath);
} catch(OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
注意:$filePath需要本地的文件路径,如果要实现直播源代码文件的即时上传。我们就需要先把文件上传到本地,然后再实现云存储上传。
以下是整理的ThinkPHP5实现阿里云存储的方法,作为参考:
function upload_albumpic($upload_path, $file_name = 'file', $save_name){
//判断是否上传图片
if (!empty($_FILES[$file_name]['name'])) {
//先执行本地上传
$file_object = request()->file($file_name);
if($file_object==null){
return array('code' => '10001', 'message' => '', 'result' => '');
exit;
}
$upload_path = BASE_UPLOAD_PATH . DS . $upload_path; // /data/wwwroot/shoptest.yunbaozb.com/public/uploads/ home/store/goods
$info = $file_object->rule('uniqid')->validate(['ext' => ALLOW_IMG_EXT])->move($upload_path, $save_name);
if ($info) {
$img_path = $upload_path . '/' . $info->getFilename();
// $img_path = $info->getFilename();
}else {
$error = $file_object->getError();
$data['code'] = '10001';
$data['message'] = $error;
$data['result'] = $_FILES[$file_name]['name'];
return $data;
}
$upload_type = config('upload_type');
if($upload_type=="local"){
create_albumpic_thumb($upload_path,$info->getFilename()); //生成相册缩略图
$url= $info->getFilename();
return array('code' => '10000', 'message' => '', 'result' => $img_path,"url"=>$url,"type"=>"local","img_info"=>array());
}else if($upload_type=="alioss"){
$accessId = config('alioss_accessid');
$accessSecret = config('alioss_accesssecret');
$bucket = config('alioss_bucket');
$endpoint = config('alioss_endpoint');
$aliendpoint_type = config('aliendpoint_type') == '1' ? true : false;
if (!strpos($save_name, '.')) {
$save_name .= '.' . pathinfo($_FILES[$file_name]['name'], PATHINFO_EXTENSION);
}
$object = $save_name;
require_once VENDOR_PATH.'aliyuncs/oss-sdk-php/autoload.php';
$OssClient = new \OSS\OssClient($accessId, $accessSecret, $endpoint);
try {
$fileinfo = $OssClient->uploadFile($bucket, $object, $img_path);
$code=$fileinfo['info']['http_code'];
if($code==200){
$url=$fileinfo['info']['url'];
return array('code' => '10000', 'message' => '', 'result' =>$url,"url"=>$url,"type"=>"alioss","img_info"=>getimagesize($img_path) );
}else{
return array('code' => '10001', 'message' => '文件上传失败', 'result' => '');
}
unlink($img_path);
} catch (OssException $e) {
return array('code' => '10001', 'message' => $e->getMessage(), 'result' => '');
}
}
}
else {
return array('code' => '10001', 'message' => '', 'result' => '');
}
}
在视频直播源代码中存储空间的拥有者和授权用户有该存储空间内的文件的读写权限,其他用户只有该存储空间内的文件的读权限。谨慎使用该权限,云豹科技在这一块运用还是比较合理和人性化的。