# @Date: 2022-05-20T23:27:09+08:00 # @Email: fm453@lukegzs.com # @Last modified by: fm453 # @Last modified time: 2022-05-21T08:36:17+08:00 # @Copyright: www.hiluker.cn namespace vendor\huawei\obsclient; // 引入依赖库 require('vendor/autoload.php'); require('obs-autoloader.php'); use Obs\ObsClient; use Obs\ObsException; use GuzzleHttp\Exception\ClientException; class PostObject extends Common { public $boundary; public function init($signature) { $obsClient = new ObsClient([ 'key' => $this->ak, 'secret' => $this->sk, 'endpoint' => $this->endpoint, 'socket_timeout' => 30, 'connect_timeout' => 10, 'signature' => $signature ]); $obsClient->initLog([ 'FilePath' => './logs', 'FileName' => 'eSDK-OBS-PHP.log', 'MaxFiles' => 10, 'Level' => WARN ]); $this->obsClient = $obsClient; $this->boundary = '9431149156168'; $this->signature = $signature; } //$sampleFilePath = '/temp/text.txt'; public function createSampleFile($filePath) { if (file_exists($filePath)) { return; } $filePath = iconv('UTF-8', 'GBK', $filePath); if (is_string($filePath) && $filePath !== '') { $fp = null; $dir = dirname($filePath); try { if (!is_dir($dir)) { mkdir($dir, 0755, true); } if (($fp = fopen($filePath, 'w+'))) { fwrite($fp, uniqid() . "\n"); fwrite($fp, uniqid() . "\n"); } } finally { if ($fp) { fclose($fp); } } } } public function claimPost($bucketName, $objectKey) { $this->objectKey = $objectKey ? $objectKey : $this->objectKey; /* * Claim a post object request */ $signature = $this->signature; $formParams = []; if (strcasecmp($signature, 'obs') === 0) { $formParams['x-obs-acl'] = ObsClient::AclPublicRead; } else { $formParams['acl'] = ObsClient::AclPublicRead; } // $formParams['content-type'] = 'text/plain'; $formParams['content-type'] = 'multipart/form-data'; $res = $this->obsClient -> createPostSignature(['Bucket' => $bucketName, 'Key' => $this->objectKey, 'Expires' => 3600, 'FormParams' => $formParams]); $formParams['key'] = $this->objectKey; $formParams['policy'] = $res['Policy']; if (strcasecmp($signature, 'obs') === 0) { $formParams['Accesskeyid'] = $this->ak; } else { $formParams['AWSAccesskeyid'] = $this->ak; } $formParams['signature'] = $res['Signature']; return $formParams; } public function buffers($formParams) { $buffers = []; $contentLength = 0; /* * Construct form data */ $buffer = []; $first = true; foreach ($formParams as $key => $val) { if (!$first) { $buffer[] = "\r\n"; } else { $first = false; } $buffer[] = "--"; $buffer[] = $this->boundary; $buffer[] = "\r\n"; $buffer[] = "Content-Disposition: form-data; name=\""; $buffer[] = strval($key); $buffer[] = "\"\r\n\r\n"; $buffer[] = strval($val); } $buffer = implode('', $buffer); $contentLength += strlen($buffer); $buffers[] = $buffer; } public function fileDesc() { /* * Construct file description */ $buffer = []; $buffer[] = "\r\n"; $buffer[] = "--"; $buffer[] = $boundary; $buffer[] = "\r\n"; $buffer[] = "Content-Disposition: form-data; name=\"file\"; filename=\""; $buffer[] = "myfile"; $buffer[] = "\"\r\n"; $buffer[] = "Content-Type: text/plain"; $buffer[] = "\r\n\r\n"; $buffer = implode('', $buffer); $contentLength += strlen($buffer); $buffers[] = $buffer; } public function fileData($filePath) { /* * Construct file data */ $buffer = []; $fp = fopen($filePath, 'r'); if ($fp) { while (!feof($fp)) { $buffer[] = fgetc($fp); } fclose($fp); } $buffer = implode('', $buffer); $contentLength += strlen($buffer); $buffers[] = $buffer; } public function endData() { /* * Contruct end data */ $buffer = []; $buffer[] = "\r\n--"; $buffer[] = $boundary; $buffer[] = "--\r\n"; $buffer = implode('', $buffer); $contentLength += strlen($buffer); $buffers[] = $buffer; } public function do() { $httpClient = new Client(['verify' => false]); $host = parse_url($endpoint)['host']; $host = $bucketName . '.' . $host; $url = 'https://' . $host . ':443'; $headers = ['Content-Length' => strval($contentLength), 'Content-Type' => 'multipart/form-data; boundary=' . $boundary]; try { $response = $httpClient -> request('POST', $url, ['body' => implode('', $buffers), 'headers'=> $headers]); printf('Post object successfully!'); $response -> getBody()-> close(); } catch (ClientException $ex) { printf('Exception message:%s', $ex ->getMessage()); } if (file_exists($sampleFilePath)) { unlink($sampleFilePath); } } }