语言:php 框架:laravel 简介:将项目上所有的 图片、pdf文件、视频等上传到华为云 并获取到华为云的文件地址进行访问
准备工作
composer准备 1.composer.json里添加需要的require 并 composer update 2.如果要新增命名空间Obs 需要在 autoload 的 psr-4 里添加
{
"name" : "obs/esdk-obs-php",
"description" : "OBS PHP SDK",
"license":"Apache-2.0",
"version":"3.20.5",
"require" : {
"php" : ">=5.6.0",
"guzzlehttp/guzzle" : "6.3.0",
"guzzlehttp/psr7" : "1.4.2",
"guzzlehttp/promises" : "1.3.1",
"psr/http-message" : "1.0.1",
"monolog/monolog" : "1.23.0",
"psr/log" : "~1.0"
},
"keywords" :["obs", "php"],
"autoload": {
"psr-4": {
"Obs\\": "Obs/"
}
}
}
下载SDK
地址:https://developer.huaweicloud.com/sdk?OBS 1.下载PHP示例 解压 得到Obs文件 2.将Obs、obs-autoloader.php 放到和app文件夹同级目录 如图:
创建上传控制器
控制器名称:HuaWeiYunController.php
<?php
namespace App\Http\Controllers;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Obs\ObsClient;
use Obs\ObsException;
use GuzzleHttp\Exception\ClientException;
require_once __DIR__.'/../../../obs-autoloader.php';
class HuaWeiYunController extends Controller
{
public $obsClient;
public $bucketName = 'bucket-uploads';
public $expires = 3600;
public function __construct()
{
parent::__construct();
$ak = config('obs.AK');
$sk = config('obs.SK');
$endpoint = '华为云桶的访问域名(这里使用IPv4地址):443';
$this->obsClient = ObsClient::factory(array (
'key' => $ak,
'secret' => $sk,
'endpoint' => $endpoint,
));
}
public function uploadImgAndFile(Request $request){
$file = $request->file('file');
$maxSize = 20;
$allowed_extensions = ["png", "jpg", "gif","pdf"];
$data = ['code' => 200, 'msg' => '上传文件失败', 'data' => ''];
if ($file->isValid()) {
$ext = $file->getClientOriginalExtension();
if (!in_array(strtolower($ext), $allowed_extensions)) {
$data['msg'] = "请上传" . implode(",", $allowed_extensions) . "格式的文件";
return response()->json($data);
}
if ($file->getClientSize() > $maxSize * 1024 * 1024) {
$data['msg'] = "文件大小限制" . $maxSize . "M";
return response()->json($data);
}
} else {
$data['msg'] = $file->getErrorMessage();
return response()->json($data);
}
$newFile = date('Ymd')."/" .time() .".".$file->getClientOriginalExtension();
try {
$resp = $this->obsClient->putObject([
'Bucket' => $this->bucketName,
'Key' => $newFile,
'Body' => fopen($file,'r'),
'ACL' => ObsClient::AclPublicRead,
]);
\Log::info('华为云-上传文件:'.print_r($resp,true));
if($resp ['HttpStatusCode']==200){
$data = [
'code' => 0,
'msg' => '上传文件成功',
'url' => $resp ['ObjectURL'],
"data" => [
"src" => $resp ['ObjectURL']
]
];
}else{
$data['data'] = $resp ['HttpStatusCode'].':'.$resp['Reason'];
}
return response()->json($data);
} catch ( ObsException $e ) {
\Log::info('华为云--上传文件错误信息:'.print_r($e,true));
return response()->json($data);
}
}
public function uploadVideo(Request $request)
{
$allowed_extensions = ["mp4", "wmv", "wav"];
$data = ['code' => 200, 'msg' => '视频上传失败', 'data' => ''];
$file = $request->file('file');
if ($file->isValid()) {
$ext = $file->getClientOriginalExtension();
if (!in_array(strtolower($ext), $allowed_extensions)) {
$data['msg'] = "请上传" . implode(",", $allowed_extensions) . "格式的视频";
return response()->json($data);
}
} else {
$data['msg'] = $file->getErrorMessage();
return response()->json($data);
}
$newFile = date('Ymd')."/".time().".".$file->getClientOriginalExtension();
try {
$resp = $this->obsClient->putObject([
'Bucket' => $this->bucketName,
'Key' => $newFile,
'Body' => fopen($file,'r'),
'ACL' => ObsClient::AclPublicRead,
]);
\Log::info('华为云-上传视频:'.print_r($resp,true));
if($resp ['HttpStatusCode']==200){
$data = [
'code' => 0,
'msg' => '上传成功',
'url' => $resp ['ObjectURL'],
"data" => [
"src" => $resp ['ObjectURL']
]
];
} else {
$data['data'] = $file->getErrorMessage();
}
}catch ( ObsException $e ) {
\Log::info('华为云--上传视频错误信息:'.print_r($e,true));
return response([
'code' => 1,
'message' => '上传视频错误 请重新上传']);
}
return response()->json($data);
}
华为云-桶的-访问域名-获取
1.在OBS管理控制台左侧导航栏选择“对象存储服务 OBS”。 2.在桶列表单击待操作的桶,进入“概览”页面。 3.在“概览”页面,查看“基本信息”。 4.“基本信息” 下的 “访问域名”。
得到的华为云上的文件地址如果访问不到 则需要配置匿名权限
|