From 278f2b57fd30f9dc04bd3e2786b2a89282800f75 Mon Sep 17 00:00:00 2001 From: Jason mann <793650314@qq.com> Date: Wed, 12 Jan 2022 13:21:09 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20guzzle7=20=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 9 +++++++-- src/Upyun/Api/Rest.php | 2 +- src/Upyun/Api/SyncVideo.php | 4 ++-- src/Upyun/Signature.php | 2 +- src/Upyun/Uploader.php | 8 ++++---- src/Upyun/Upyun.php | 4 ++-- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index 6431520..34a8ec2 100644 --- a/composer.json +++ b/composer.json @@ -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/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..ab3b91c 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) { @@ -146,7 +146,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 +167,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..e301c54 100644 --- a/src/Upyun/Upyun.php +++ b/src/Upyun/Upyun.php @@ -154,7 +154,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 +351,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']; } From 2b67ce685ee6ccce5d4d59c0f1d9d120de275679 Mon Sep 17 00:00:00 2001 From: Jason mann <793650314@qq.com> Date: Wed, 12 Jan 2022 13:26:24 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8C=85=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 34a8ec2..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", From b72e74ea7082e77be5ff7f455401522e0d831392 Mon Sep 17 00:00:00 2001 From: Jason mann <793650314@qq.com> Date: Wed, 12 Jan 2022 16:52:55 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=86=E7=89=87?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Upyun/Uploader.php | 85 ++++++++++++++++++++++++++++++++++++++++++ src/Upyun/Upyun.php | 42 +++++++++++++++++++++ 2 files changed, 127 insertions(+) diff --git a/src/Upyun/Uploader.php b/src/Upyun/Uploader.php index ab3b91c..fb0b40b 100644 --- a/src/Upyun/Uploader.php +++ b/src/Upyun/Uploader.php @@ -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' || diff --git a/src/Upyun/Upyun.php b/src/Upyun/Upyun.php index e301c54..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); + } + /** * 读取云存储文件/目录内容 * From 13a8f2984fcc4def56db80719ef3be48e46e6e98 Mon Sep 17 00:00:00 2001 From: lihongcheng <517423234@qq.com> Date: Mon, 25 Apr 2022 16:12:02 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=8F=88=E6=8B=8D=E4=BA=91=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Upyun/Config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; /**