diff --git a/README.md b/README.md index 8eda96a..c541b8e 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ SDK 包含如下功能 建议使用速度很快的国内[全量镜像](https://pkg.phpcomposer.com/#how-to-use-packagist-mirror)([又拍云赞助](https://pkg.phpcomposer.com/#donation)) ``` -composer require upyun/sdk +composer require jasonmann/upyun-sdk ``` 2.如果不适应 `composer` 管理,可以直接下载[压缩包](https://github.com/upyun/php-sdk/releases)(注意需要下载 `php-sdk-版本号.zip` 格式的 zip 压缩包,不是 Source code 源码压缩包),解压后,项目中添加如下代码: diff --git a/composer.json b/composer.json index 6431520..98e27cc 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "upyun/sdk", + "name": "jasonmann/upyun-sdk", "description": "UPYUN sdk for php", "keywords": ["UPYUN", "sdk"], "type": "library", @@ -7,9 +7,10 @@ "homepage": "https://github.com/upyun/php-sdk/", "license": "MIT", "require": { - "php": ">=5.5.0", + "php": "^7.2.5 || ^8.0", "ext-curl": "*", - "guzzlehttp/guzzle": "~6.0" + "guzzlehttp/guzzle": "~7.0", + "ext-json": "*" }, "require-dev": { "phpunit/phpunit": "~4.0", @@ -38,6 +39,10 @@ { "name": "sabakugaara", "email": "senellise@gmail.com" + }, + { + "name": "jasonmann", + "email": "793650314@qq.com" } ] } diff --git a/src/Upyun/Api/Rest.php b/src/Upyun/Api/Rest.php index be4b0a0..13f16b3 100644 --- a/src/Upyun/Api/Rest.php +++ b/src/Upyun/Api/Rest.php @@ -47,7 +47,7 @@ public function request($method, $storagePath) */ public function withFile($file) { - $stream = Psr7\stream_for($file); + $stream = Psr7\Utils::streamFor($file); $this->file = $stream; return $this; diff --git a/src/Upyun/Api/SyncVideo.php b/src/Upyun/Api/SyncVideo.php index 39d1972..c99732b 100644 --- a/src/Upyun/Api/SyncVideo.php +++ b/src/Upyun/Api/SyncVideo.php @@ -36,7 +36,7 @@ public function process($params, $path) { 'json' => $params ]); - $body = $response->getBody()->getContents(); + $body = (string)$response->getBody(); return json_decode($body, true); } -} \ No newline at end of file +} diff --git a/src/Upyun/Config.php b/src/Upyun/Config.php index 1b99b6c..97cf39e 100644 --- a/src/Upyun/Config.php +++ b/src/Upyun/Config.php @@ -50,7 +50,7 @@ class Config /** * @var int request timeout seconds */ - public $timeout = 60; + public $timeout = 600; /** diff --git a/src/Upyun/Signature.php b/src/Upyun/Signature.php index 0fb68fe..a177f4b 100644 --- a/src/Upyun/Signature.php +++ b/src/Upyun/Signature.php @@ -73,7 +73,7 @@ public static function getPurgeSignHeader(Config $serviceConfig, $urlString) * @param $policy * @param $contentMd5 请求 body 的 md5 * - * @return array + * @return string */ public static function getBodySignature(Config $serviceConfig, $method, $uri, $date = null, $policy = null, $contentMd5 = null) { diff --git a/src/Upyun/Uploader.php b/src/Upyun/Uploader.php index 95e6f2f..fb0b40b 100644 --- a/src/Upyun/Uploader.php +++ b/src/Upyun/Uploader.php @@ -24,7 +24,7 @@ public function __construct(Config $config) public function upload($path, $file, $params, $withAsyncProcess) { - $stream = Psr7\stream_for($file); + $stream = Psr7\Utils::streamFor($file); $size = $stream->getSize(); $useBlock = $this->needUseBlock($size); @@ -87,7 +87,7 @@ private function pointUpload($path, $stream, $params) 'X-Upyun-Multi-Uuid' => $uuid, 'X-Upyun-Part-Id' => $partId )) - ->withFile(Psr7\stream_for($fileBlock)) + ->withFile(Psr7\Utils::streamFor($fileBlock)) ->send(); if ($res->getStatusCode() !== 204) { @@ -110,6 +110,91 @@ private function pointUpload($path, $stream, $params) return $res; } + /** + * 初始化一个分片上传事件 + * @param $path + * @param $size + * @param array $params + * @return mixed + * @throws \Exception + */ + public function initiateMultipartUpload($path, $size, $params=[]) + { + $req = new Rest($this->config); + $headers = array(); + if (is_array($params)) { + foreach ($params as $key => $val) { + $headers['X-Upyun-Meta-' . $key] = $val; + } + } + + $res = $req->request('PUT', $path) + ->withHeaders(array_merge(array( + 'X-Upyun-Multi-Stage' => 'initiate', + 'X-Upyun-Multi-Type' => Psr7\mimetype_from_filename($path), + 'X-Upyun-Multi-Length' => $size, + ), $headers)) + ->send(); + if ($res->getStatusCode() !== 204) { + throw new \Exception('init request failed when poinit upload!'); + } + $init = Util::getHeaderParams($res->getHeaders()); + $uuid = $init['x-upyun-multi-uuid']; + return $uuid; + } + + /** + * 上传分片 + * @param $path + * @param $fileBlock + * @param $i + * @param $uuid + * @return mixed + * @throws \Exception + */ + public function uploadPart($path, $fileBlock, $i, $uuid) + { + $req = new Rest($this->config); + + $res = $req->request('PUT', $path) + ->withHeaders(array( + 'X-Upyun-Multi-Stage' => 'upload', + 'X-Upyun-Multi-Uuid' => $uuid, + 'X-Upyun-Part-Id' => $i + )) + ->withFile(Psr7\Utils::streamFor($fileBlock)) + ->send(); + if ($res->getStatusCode() !== 204) { + throw new \Exception('upload request failed when point upload!'); + } + $data = Util::getHeaderParams($res->getHeaders()); + $partId = $data['x-upyun-next-part-id']; + return $partId; + } + + /** + * 完成分片上传 + * @param $path + * @param $uuid + * @return array + * @throws \Exception + */ + public function completeMultipartUpload($path, $uuid) + { + $req = new Rest($this->config); + + $res = $req->request('PUT', $path) + ->withHeaders(array( + 'X-Upyun-Multi-Uuid' => $uuid, + 'X-Upyun-Multi-Stage' => 'complete' + )) + ->send(); + if ($res->getStatusCode() != 204 && $res->getStatusCode() != 201) { + throw new \Exception('end request failed when poinit upload!'); + } + return Util::getHeaderParams($res->getHeaders()); + } + private function needUseBlock($fileSize) { if ($this->config->uploadType === 'BLOCK' || @@ -146,7 +231,7 @@ private function concurrentPointUpload($path, $stream, $params) ->withHeaders(array_merge(array( 'X-Upyun-Multi-Disorder' => 'true', 'X-Upyun-Multi-Stage' => 'initiate', - 'X-Upyun-Multi-Type' => Psr7\mimetype_from_filename($path), + 'X-Upyun-Multi-Type' => Psr7\MimeType::fromFilename($path), 'X-Upyun-Multi-Length' => $stream->getSize(), ), $headers)) ->send(); @@ -167,7 +252,7 @@ private function concurrentPointUpload($path, $stream, $params) 'X-Upyun-Multi-Uuid' => $uuid, 'X-Upyun-Part-Id' => $i )) - ->withFile(Psr7\stream_for($fileBlock)) + ->withFile(Psr7\Utils::streamFor($fileBlock)) ->toRequest(); } }; diff --git a/src/Upyun/Upyun.php b/src/Upyun/Upyun.php index e74c6c7..fb8b396 100644 --- a/src/Upyun/Upyun.php +++ b/src/Upyun/Upyun.php @@ -127,6 +127,48 @@ public function write($path, $content, $params = array(), $withAsyncProcess = fa return Util::getHeaderParams($response->getHeaders()); } + /** + * 初始化一个分片上传事件 + * @param $path + * @param $size + * @param array $params + * @return mixed + * @throws \Exception + */ + public function initiateMultipartUpload($path, $size, $params=[]) + { + $upload = new Uploader($this->config); + return $upload->initiateMultipartUpload($path, $size, $params); + } + + /** + * 上传分片 + * @param $path + * @param $fileBlock + * @param $i + * @param $uuid + * @return mixed + * @throws \Exception + */ + public function uploadPart($path, $fileBlock, $i, $uuid) + { + $upload = new Uploader($this->config); + return $upload->uploadPart($path, $fileBlock, $i, $uuid); + } + + /** + * 完成分片上传 + * @param $path + * @param $uuid + * @return array + * @throws \Exception + */ + public function completeMultipartUpload($path, $uuid) + { + $upload = new Uploader($this->config); + return $upload->completeMultipartUpload($path, $uuid); + } + /** * 读取云存储文件/目录内容 * @@ -154,7 +196,7 @@ public function read($path, $saveHandler = null, $params = array()) if (! isset($params['x-upyun-list-iter'])) { if (is_resource($saveHandler)) { - Psr7\copy_to_stream($response->getBody(), Psr7\stream_for($saveHandler)); + Psr7\Utils::copyToStream($response->getBody(), Psr7\stream_for($saveHandler)); return true; } else { return $response->getBody()->getContents(); @@ -351,7 +393,7 @@ public function purge($urls) 'headers' => Signature::getPurgeSignHeader($this->config, $urlString), 'form_params' => ['purge' => $urlString] ]); - $result = json_decode($response->getBody()->getContents(), true); + $result = json_decode((string)$response->getBody(), true); return $result['invalid_domain_of_url']; }