1. ホーム
  2. Web プログラミング
  3. PHP プログラミング
  4. phpの例

オンデマンドアップロードを実現するLaravelフレームワーク Aliyun機能

2022-01-15 04:50:35

1. サードパーティのalibabacoudコントロールがフレームワークにインストールされていることを確認する

以下のようにコードを実装します。

アップロード中にタスクに問題が発生した場合、メッセージを残すことができます。

<?php
 
namespace App\Api\xxxx\Controllers;
use DB;
use Illuminate\Http\Request;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\SDK\Vod\V20170321\Vod;
use AlibabaCloud\SDK\Vod\V20170321\Models\DeleteImageRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\GetPlayInfoRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\DeleteVideoRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\GetImageInfoRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadVideoRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadImageRequest;
use Darabonba\OpenApi\Models\Config;
use OSS\OssClient;
use AlibabaCloud\Tea\Tea;
class CommonController extends XXController
{
    //video-on-demand upload
    public function uploadVideo($fileName,$title)
    {
        $title = explode('.' , $param['title']);
        $fileName = $param['fileName'];
        if($title[1] == 'mp4') {
            $cateId = 3479;//category ID, AliCloud VOD can set
         }
        $client = self::createClient("xxx", "xxx");//put in the value according to the application
        $createUploadVideoRequest = new CreateUploadVideoRequest([
            'Action' => 'CreateUploadVideo',//the requested upload function
            "fileName" => $fileName,//file name
            "cateId" => $cateId,//type name
            'title' => $param['title'],/        ]);
        $createRes = $client-> createUploadVideo($createUploadVideoRequest);
        $videoId = $createRes->body->videoId; //get the parameters needed for the video playback address
        $formats = $title[1];
        //base64 decoding
        $uploadAddress = json_decode(base64_decode($createRes->body->uploadAddress), true);
        $uploadAuth = json_decode(base64_decode($createRes->body->uploadAuth), true);
 
        // Initialize the OSS client with UploadAuth and UploadAddress
        $ossClient = $this->init_oss_client($uploadAuth, $uploadAddress);
        $localFile = $fileName; // the full path of the local video file that needs to be uploaded to VOD Change this to have an effect
       
 
        // upload the file, note that it is synchronous upload will block waiting, time consuming and file size and network uplink bandwidth related
        $result = $this->upload_local_file($ossClient, $uploadAddress, $localFile);
        //$result = multipart_upload_file($ossClient, $uploadAddress, $localFile);
        $arr = $this->playinfo($videoId,$formats);
        $data = Utils::toArray(Tea::merge($arr->body));
        $brr = array();
        $brr['VideoId'] = $data['VideoBase']['VideoId'];//video id
        $brr['PlayURL'] = $data['PlayInfoList']['PlayInfo'][0]['PlayURL'];//returned play address
        return $brr;
    }
 
    public function init_oss_client($uploadAuth, $uploadAddress)
    {
        $ossClient = new OssClient($uploadAuth['AccessKeyId'], $uploadAuth['AccessKeySecret'], $uploadAddress['Endpoint'],
            false, $uploadAuth['SecurityToken']);
        $ossClient->setTimeout(86400*7); // set the request timeout in seconds, the default is 5184000 seconds, it is recommended not to set it too small, if the upload file is very large, the consumption time will be longer
        $ossClient->setConnectTimeout(10); // set the connection timeout in seconds, the default is 10 seconds
        return $ossClient;
    }
}

オンデマンドアップロードAliun機能を実現するためのLaravelフレームワークに関するこの記事はここで紹介されて、より関連するLaravelオンデマンドアップロードAliunコンテンツは、スクリプトの家の前の記事を検索してくださいまたは次の関連記事を閲覧し続けることは、よりスクリプトの家をサポートします願っています!...